mirror of
https://github.com/IRBorisov/ConceptPortal.git
synced 2025-06-26 04:50:36 +03:00
Refactor backend url setup
This commit is contained in:
parent
ae8b4afa88
commit
f4af39e62e
|
@ -1,6 +1,5 @@
|
||||||
import './index.css'
|
import './index.css'
|
||||||
|
|
||||||
import axios from 'axios';
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import ReactDOM from 'react-dom/client'
|
import ReactDOM from 'react-dom/client'
|
||||||
import { ErrorBoundary } from 'react-error-boundary';
|
import { ErrorBoundary } from 'react-error-boundary';
|
||||||
|
@ -12,10 +11,9 @@ import ErrorFallback from './components/ErrorFallback.tsx';
|
||||||
import { AuthState } from './context/AuthContext.tsx';
|
import { AuthState } from './context/AuthContext.tsx';
|
||||||
import { ThemeState } from './context/ThemeContext.tsx';
|
import { ThemeState } from './context/ThemeContext.tsx';
|
||||||
import { UsersState } from './context/UsersContext.tsx';
|
import { UsersState } from './context/UsersContext.tsx';
|
||||||
|
import { initBackend } from './utils/backendAPI.ts';
|
||||||
|
|
||||||
axios.defaults.withCredentials = true;
|
initBackend();
|
||||||
axios.defaults.xsrfCookieName = 'csrftoken';
|
|
||||||
axios.defaults.xsrfHeaderName = 'x-csrftoken';
|
|
||||||
|
|
||||||
const resetState = () => {
|
const resetState = () => {
|
||||||
console.log('Resetting state after error fallback')
|
console.log('Resetting state after error fallback')
|
||||||
|
|
|
@ -5,14 +5,21 @@ import { type ErrorInfo } from '../components/BackendError'
|
||||||
import { FilterType, RSFormsFilter } from '../hooks/useRSForms'
|
import { FilterType, RSFormsFilter } from '../hooks/useRSForms'
|
||||||
import { config } from './constants'
|
import { config } from './constants'
|
||||||
import {
|
import {
|
||||||
IConstituentaList,
|
IConstituentaList, IConstituentaMeta,
|
||||||
IConstituentaMeta,
|
|
||||||
ICstCreateData, ICstCreatedResponse, ICstMovetoData, ICstUpdateData,
|
ICstCreateData, ICstCreatedResponse, ICstMovetoData, ICstUpdateData,
|
||||||
ICurrentUser, IExpressionParse,
|
ICurrentUser, IExpressionParse, IRSExpression,
|
||||||
IRSExpression,
|
IRSFormCreateData, IRSFormData,
|
||||||
IRSFormCreateData, IRSFormData,
|
|
||||||
IRSFormMeta, IRSFormUpdateData, IRSFormUploadData, IUserInfo,
|
IRSFormMeta, IRSFormUpdateData, IRSFormUploadData, IUserInfo,
|
||||||
IUserLoginData, IUserProfile, IUserSignupData, IUserUpdateData} from './models'
|
IUserLoginData, IUserProfile, IUserSignupData, IUserUpdateData
|
||||||
|
} from './models'
|
||||||
|
|
||||||
|
export function initBackend() {
|
||||||
|
axios.defaults.withCredentials = true;
|
||||||
|
axios.defaults.xsrfCookieName = 'csrftoken';
|
||||||
|
axios.defaults.xsrfHeaderName = 'x-csrftoken';
|
||||||
|
axios.defaults.baseURL = `${config.backend}`;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// ================ Data transfer types ================
|
// ================ Data transfer types ================
|
||||||
export type DataCallback<ResponseData = undefined> = (data: ResponseData) => void;
|
export type DataCallback<ResponseData = undefined> = (data: ResponseData) => void;
|
||||||
|
@ -50,7 +57,7 @@ interface IAxiosRequest<RequestData, ResponseData> {
|
||||||
export function getAuth(request: FrontPull<ICurrentUser>) {
|
export function getAuth(request: FrontPull<ICurrentUser>) {
|
||||||
AxiosGet({
|
AxiosGet({
|
||||||
title: 'Current user',
|
title: 'Current user',
|
||||||
endpoint: `${config.url.AUTH}auth`,
|
endpoint: `/users/api/auth`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -58,7 +65,7 @@ export function getAuth(request: FrontPull<ICurrentUser>) {
|
||||||
export function postLogin(request: FrontPush<IUserLoginData>) {
|
export function postLogin(request: FrontPush<IUserLoginData>) {
|
||||||
AxiosPost({
|
AxiosPost({
|
||||||
title: 'Login',
|
title: 'Login',
|
||||||
endpoint: `${config.url.AUTH}login`,
|
endpoint: `/users/api/login`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -66,7 +73,7 @@ export function postLogin(request: FrontPush<IUserLoginData>) {
|
||||||
export function postLogout(request: FrontAction) {
|
export function postLogout(request: FrontAction) {
|
||||||
AxiosPost({
|
AxiosPost({
|
||||||
title: 'Logout',
|
title: 'Logout',
|
||||||
endpoint: `${config.url.AUTH}logout`,
|
endpoint: `/users/api/logout`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -74,7 +81,7 @@ export function postLogout(request: FrontAction) {
|
||||||
export function postSignup(request: FrontExchange<IUserSignupData, IUserProfile>) {
|
export function postSignup(request: FrontExchange<IUserSignupData, IUserProfile>) {
|
||||||
AxiosPost({
|
AxiosPost({
|
||||||
title: 'Register user',
|
title: 'Register user',
|
||||||
endpoint: `${config.url.AUTH}signup`,
|
endpoint: `/users/api/signup`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -82,7 +89,7 @@ export function postSignup(request: FrontExchange<IUserSignupData, IUserProfile>
|
||||||
export function getProfile(request: FrontPull<IUserProfile>) {
|
export function getProfile(request: FrontPull<IUserProfile>) {
|
||||||
AxiosGet({
|
AxiosGet({
|
||||||
title: 'Current user profile',
|
title: 'Current user profile',
|
||||||
endpoint: `${config.url.AUTH}profile`,
|
endpoint: `/users/api/profile`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -90,7 +97,7 @@ export function getProfile(request: FrontPull<IUserProfile>) {
|
||||||
export function patchProfile(request: FrontExchange<IUserUpdateData, IUserProfile>) {
|
export function patchProfile(request: FrontExchange<IUserUpdateData, IUserProfile>) {
|
||||||
AxiosPatch({
|
AxiosPatch({
|
||||||
title: 'Current user profile',
|
title: 'Current user profile',
|
||||||
endpoint: `${config.url.AUTH}profile`,
|
endpoint: `/users/api/profile`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -98,7 +105,7 @@ export function patchProfile(request: FrontExchange<IUserUpdateData, IUserProfil
|
||||||
export function getActiveUsers(request: FrontPull<IUserInfo[]>) {
|
export function getActiveUsers(request: FrontPull<IUserInfo[]>) {
|
||||||
AxiosGet({
|
AxiosGet({
|
||||||
title: 'Active users list',
|
title: 'Active users list',
|
||||||
endpoint: `${config.url.AUTH}active-users`,
|
endpoint: `/users/api/active-users`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -106,8 +113,8 @@ export function getActiveUsers(request: FrontPull<IUserInfo[]>) {
|
||||||
export function getRSForms(filter: RSFormsFilter, request: FrontPull<IRSFormMeta[]>) {
|
export function getRSForms(filter: RSFormsFilter, request: FrontPull<IRSFormMeta[]>) {
|
||||||
const endpoint =
|
const endpoint =
|
||||||
filter.type === FilterType.PERSONAL
|
filter.type === FilterType.PERSONAL
|
||||||
? `${config.url.BASE}rsforms?owner=${filter.data as number}`
|
? `/api/rsforms?owner=${filter.data as number}`
|
||||||
: `${config.url.BASE}rsforms?is_common=true`;
|
: `/api/rsforms?is_common=true`;
|
||||||
AxiosGet({
|
AxiosGet({
|
||||||
title: 'RSForms list',
|
title: 'RSForms list',
|
||||||
endpoint: endpoint,
|
endpoint: endpoint,
|
||||||
|
@ -118,7 +125,7 @@ export function getRSForms(filter: RSFormsFilter, request: FrontPull<IRSFormMeta
|
||||||
export function postNewRSForm(request: FrontExchange<IRSFormCreateData, IRSFormMeta>) {
|
export function postNewRSForm(request: FrontExchange<IRSFormCreateData, IRSFormMeta>) {
|
||||||
AxiosPost({
|
AxiosPost({
|
||||||
title: 'New RSForm',
|
title: 'New RSForm',
|
||||||
endpoint: `${config.url.BASE}rsforms/create-detailed/`,
|
endpoint: `/api/rsforms/create-detailed/`,
|
||||||
request: request,
|
request: request,
|
||||||
options: {
|
options: {
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -131,7 +138,7 @@ export function postNewRSForm(request: FrontExchange<IRSFormCreateData, IRSFormM
|
||||||
export function postCloneRSForm(schema: string, request: FrontExchange<IRSFormCreateData, IRSFormData>) {
|
export function postCloneRSForm(schema: string, request: FrontExchange<IRSFormCreateData, IRSFormData>) {
|
||||||
AxiosPost({
|
AxiosPost({
|
||||||
title: 'clone RSForm',
|
title: 'clone RSForm',
|
||||||
endpoint: `${config.url.BASE}rsforms/${schema}/clone/`,
|
endpoint: `/api/rsforms/${schema}/clone/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -139,7 +146,7 @@ export function postCloneRSForm(schema: string, request: FrontExchange<IRSFormCr
|
||||||
export function getRSFormDetails(target: string, request: FrontPull<IRSFormData>) {
|
export function getRSFormDetails(target: string, request: FrontPull<IRSFormData>) {
|
||||||
AxiosGet({
|
AxiosGet({
|
||||||
title: `RSForm details for id=${target}`,
|
title: `RSForm details for id=${target}`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${target}/details/`,
|
endpoint: `/api/rsforms/${target}/details/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -147,7 +154,7 @@ export function getRSFormDetails(target: string, request: FrontPull<IRSFormData>
|
||||||
export function patchRSForm(target: string, request: FrontExchange<IRSFormUpdateData, IRSFormMeta>) {
|
export function patchRSForm(target: string, request: FrontExchange<IRSFormUpdateData, IRSFormMeta>) {
|
||||||
AxiosPatch({
|
AxiosPatch({
|
||||||
title: `RSForm id=${target}`,
|
title: `RSForm id=${target}`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${target}/`,
|
endpoint: `/api/rsforms/${target}/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -155,7 +162,7 @@ export function patchRSForm(target: string, request: FrontExchange<IRSFormUpdate
|
||||||
export function deleteRSForm(target: string, request: FrontAction) {
|
export function deleteRSForm(target: string, request: FrontAction) {
|
||||||
AxiosDelete({
|
AxiosDelete({
|
||||||
title: `RSForm id=${target}`,
|
title: `RSForm id=${target}`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${target}/`,
|
endpoint: `/api/rsforms/${target}/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -163,7 +170,7 @@ export function deleteRSForm(target: string, request: FrontAction) {
|
||||||
export function postClaimRSForm(target: string, request: FrontPull<IRSFormMeta>) {
|
export function postClaimRSForm(target: string, request: FrontPull<IRSFormMeta>) {
|
||||||
AxiosPost({
|
AxiosPost({
|
||||||
title: `Claim on RSForm id=${target}`,
|
title: `Claim on RSForm id=${target}`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${target}/claim/`,
|
endpoint: `/api/rsforms/${target}/claim/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -171,7 +178,7 @@ export function postClaimRSForm(target: string, request: FrontPull<IRSFormMeta>)
|
||||||
export function getTRSFile(target: string, request: FrontPull<Blob>) {
|
export function getTRSFile(target: string, request: FrontPull<Blob>) {
|
||||||
AxiosGet({
|
AxiosGet({
|
||||||
title: `RSForm TRS file for id=${target}`,
|
title: `RSForm TRS file for id=${target}`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${target}/export-trs/`,
|
endpoint: `/api/rsforms/${target}/export-trs/`,
|
||||||
request: request,
|
request: request,
|
||||||
options: { responseType: 'blob' }
|
options: { responseType: 'blob' }
|
||||||
});
|
});
|
||||||
|
@ -180,7 +187,7 @@ export function getTRSFile(target: string, request: FrontPull<Blob>) {
|
||||||
export function postNewConstituenta(schema: string, request: FrontExchange<ICstCreateData, ICstCreatedResponse>) {
|
export function postNewConstituenta(schema: string, request: FrontExchange<ICstCreateData, ICstCreatedResponse>) {
|
||||||
AxiosPost({
|
AxiosPost({
|
||||||
title: `New Constituenta for RSForm id=${schema}: ${request.data.alias}`,
|
title: `New Constituenta for RSForm id=${schema}: ${request.data.alias}`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${schema}/cst-create/`,
|
endpoint: `/api/rsforms/${schema}/cst-create/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -188,7 +195,7 @@ export function postNewConstituenta(schema: string, request: FrontExchange<ICstC
|
||||||
export function patchDeleteConstituenta(schema: string, request: FrontExchange<IConstituentaList, IRSFormData>) {
|
export function patchDeleteConstituenta(schema: string, request: FrontExchange<IConstituentaList, IRSFormData>) {
|
||||||
AxiosPatch({
|
AxiosPatch({
|
||||||
title: `Delete Constituents for RSForm id=${schema}: ${request.data.items.map(item => String(item.id)).join(' ')}`,
|
title: `Delete Constituents for RSForm id=${schema}: ${request.data.items.map(item => String(item.id)).join(' ')}`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${schema}/cst-multidelete/`,
|
endpoint: `/api/rsforms/${schema}/cst-multidelete/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -196,7 +203,7 @@ export function patchDeleteConstituenta(schema: string, request: FrontExchange<I
|
||||||
export function patchConstituenta(target: string, request: FrontExchange<ICstUpdateData, IConstituentaMeta>) {
|
export function patchConstituenta(target: string, request: FrontExchange<ICstUpdateData, IConstituentaMeta>) {
|
||||||
AxiosPatch({
|
AxiosPatch({
|
||||||
title: `Constituenta id=${target}`,
|
title: `Constituenta id=${target}`,
|
||||||
endpoint: `${config.url.BASE}constituents/${target}/`,
|
endpoint: `/api/constituents/${target}/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -204,7 +211,7 @@ export function patchConstituenta(target: string, request: FrontExchange<ICstUpd
|
||||||
export function patchMoveConstituenta(schema: string, request: FrontExchange<ICstMovetoData, IRSFormData>) {
|
export function patchMoveConstituenta(schema: string, request: FrontExchange<ICstMovetoData, IRSFormData>) {
|
||||||
AxiosPatch({
|
AxiosPatch({
|
||||||
title: `Moving Constituents for RSForm id=${schema}: ${JSON.stringify(request.data.items)} to ${request.data.move_to}`,
|
title: `Moving Constituents for RSForm id=${schema}: ${JSON.stringify(request.data.items)} to ${request.data.move_to}`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${schema}/cst-moveto/`,
|
endpoint: `/api/rsforms/${schema}/cst-moveto/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -212,7 +219,7 @@ export function patchMoveConstituenta(schema: string, request: FrontExchange<ICs
|
||||||
export function postCheckExpression(schema: string, request: FrontExchange<IRSExpression, IExpressionParse>) {
|
export function postCheckExpression(schema: string, request: FrontExchange<IRSExpression, IExpressionParse>) {
|
||||||
AxiosPost({
|
AxiosPost({
|
||||||
title: `Check expression for RSForm id=${schema}: ${request.data.expression }`,
|
title: `Check expression for RSForm id=${schema}: ${request.data.expression }`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${schema}/check/`,
|
endpoint: `/api/rsforms/${schema}/check/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -220,7 +227,7 @@ export function postCheckExpression(schema: string, request: FrontExchange<IRSEx
|
||||||
export function patchResetAliases(target: string, request: FrontPull<IRSFormData>) {
|
export function patchResetAliases(target: string, request: FrontPull<IRSFormData>) {
|
||||||
AxiosPatch({
|
AxiosPatch({
|
||||||
title: `Reset alias for RSForm id=${target}`,
|
title: `Reset alias for RSForm id=${target}`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${target}/reset-aliases/`,
|
endpoint: `/api/rsforms/${target}/reset-aliases/`,
|
||||||
request: request
|
request: request
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -228,7 +235,7 @@ export function patchResetAliases(target: string, request: FrontPull<IRSFormData
|
||||||
export function patchUploadTRS(target: string, request: FrontExchange<IRSFormUploadData, IRSFormData>) {
|
export function patchUploadTRS(target: string, request: FrontExchange<IRSFormUploadData, IRSFormData>) {
|
||||||
AxiosPatch({
|
AxiosPatch({
|
||||||
title: `Replacing data with trs file for RSForm id=${target}`,
|
title: `Replacing data with trs file for RSForm id=${target}`,
|
||||||
endpoint: `${config.url.BASE}rsforms/${target}/load-trs/`,
|
endpoint: `/api/rsforms/${target}/load-trs/`,
|
||||||
request: request,
|
request: request,
|
||||||
options: {
|
options: {
|
||||||
headers: {
|
headers: {
|
||||||
|
|
|
@ -1,16 +1,10 @@
|
||||||
// Constants
|
// Constants
|
||||||
const prod = {
|
const prod = {
|
||||||
url: {
|
backend: 'http://rs.acconcept.ru:8000',
|
||||||
BASE: 'http://rs.acconcept.ru:8000/api/',
|
|
||||||
AUTH: 'http://rs.acconcept.ru:8000/users/api/'
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const dev = {
|
const dev = {
|
||||||
url: {
|
backend: 'http://localhost:8000',
|
||||||
BASE: 'http://localhost:8000/api/',
|
|
||||||
AUTH: 'http://localhost:8000/users/api/'
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const config = process.env.NODE_ENV === 'production' ? prod : dev;
|
export const config = process.env.NODE_ENV === 'production' ? prod : dev;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user