2023-07-26 23:11:00 +03:00
|
|
|
import axios, { AxiosRequestConfig } from 'axios'
|
2023-07-15 17:57:25 +03:00
|
|
|
import { toast } from 'react-toastify'
|
2023-07-25 20:27:29 +03:00
|
|
|
|
|
|
|
import { type ErrorInfo } from '../components/BackendError'
|
|
|
|
import { config } from './constants'
|
2023-07-26 23:11:00 +03:00
|
|
|
import {
|
2023-08-01 11:44:33 +03:00
|
|
|
IConstituentaList, IConstituentaMeta,
|
2023-07-27 22:04:25 +03:00
|
|
|
ICstCreateData, ICstCreatedResponse, ICstMovetoData, ICstUpdateData,
|
2023-08-01 11:44:33 +03:00
|
|
|
ICurrentUser, IExpressionParse, IRSExpression,
|
|
|
|
IRSFormCreateData, IRSFormData,
|
2023-07-27 22:04:25 +03:00
|
|
|
IRSFormMeta, IRSFormUpdateData, IRSFormUploadData, IUserInfo,
|
2023-08-10 13:53:19 +03:00
|
|
|
IUserLoginData, IUserProfile, IUserSignupData, IUserUpdateData, IUserUpdatePassword
|
2023-08-01 11:44:33 +03:00
|
|
|
} from './models'
|
|
|
|
|
|
|
|
export function initBackend() {
|
|
|
|
axios.defaults.withCredentials = true;
|
|
|
|
axios.defaults.xsrfCookieName = 'csrftoken';
|
|
|
|
axios.defaults.xsrfHeaderName = 'x-csrftoken';
|
|
|
|
axios.defaults.baseURL = `${config.backend}`;
|
|
|
|
|
|
|
|
}
|
2023-07-15 17:57:25 +03:00
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
// ================ Data transfer types ================
|
|
|
|
export type DataCallback<ResponseData = undefined> = (data: ResponseData) => void;
|
2023-07-15 17:57:25 +03:00
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
interface IFrontRequest<RequestData, ResponseData> {
|
|
|
|
data?: RequestData
|
|
|
|
onSuccess?: DataCallback<ResponseData>
|
2023-07-15 17:57:25 +03:00
|
|
|
onError?: (error: ErrorInfo) => void
|
|
|
|
setLoading?: (loading: boolean) => void
|
|
|
|
showError?: boolean
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export interface FrontPush<DataType> extends IFrontRequest<DataType, undefined> {
|
|
|
|
data: DataType
|
|
|
|
}
|
|
|
|
export interface FrontPull<DataType> extends IFrontRequest<undefined, DataType>{
|
|
|
|
onSuccess: DataCallback<DataType>
|
2023-07-15 17:57:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export interface FrontExchange<RequestData, ResponseData> extends IFrontRequest<RequestData, ResponseData>{
|
|
|
|
data: RequestData
|
|
|
|
onSuccess: DataCallback<ResponseData>
|
2023-07-15 17:57:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export interface FrontAction extends IFrontRequest<undefined, undefined>{}
|
|
|
|
|
|
|
|
interface IAxiosRequest<RequestData, ResponseData> {
|
|
|
|
endpoint: string
|
|
|
|
request: IFrontRequest<RequestData, ResponseData>
|
|
|
|
title: string
|
|
|
|
options?: AxiosRequestConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==================== API ====================
|
|
|
|
export function getAuth(request: FrontPull<ICurrentUser>) {
|
|
|
|
AxiosGet({
|
2023-07-15 17:57:25 +03:00
|
|
|
title: 'Current user',
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/users/api/auth`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function postLogin(request: FrontPush<IUserLoginData>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: 'Login',
|
2023-08-01 20:14:03 +03:00
|
|
|
endpoint: '/users/api/login',
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function postLogout(request: FrontAction) {
|
|
|
|
AxiosPost({
|
2023-07-15 17:57:25 +03:00
|
|
|
title: 'Logout',
|
2023-08-01 20:14:03 +03:00
|
|
|
endpoint: '/users/api/logout',
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
2023-07-26 23:11:00 +03:00
|
|
|
}
|
2023-07-15 17:57:25 +03:00
|
|
|
|
2023-07-31 22:38:58 +03:00
|
|
|
export function postSignup(request: FrontExchange<IUserSignupData, IUserProfile>) {
|
2023-07-26 23:11:00 +03:00
|
|
|
AxiosPost({
|
2023-07-15 17:57:25 +03:00
|
|
|
title: 'Register user',
|
2023-08-01 20:14:03 +03:00
|
|
|
endpoint: '/users/api/signup',
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function getProfile(request: FrontPull<IUserProfile>) {
|
|
|
|
AxiosGet({
|
|
|
|
title: 'Current user profile',
|
2023-08-01 20:14:03 +03:00
|
|
|
endpoint: '/users/api/profile',
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-31 21:45:27 +03:00
|
|
|
export function patchProfile(request: FrontExchange<IUserUpdateData, IUserProfile>) {
|
|
|
|
AxiosPatch({
|
|
|
|
title: 'Current user profile',
|
2023-08-01 20:14:03 +03:00
|
|
|
endpoint: '/users/api/profile',
|
2023-07-31 21:45:27 +03:00
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-10 13:53:19 +03:00
|
|
|
export function patchPassword(request: FrontPush<IUserUpdatePassword>) {
|
|
|
|
AxiosPatch({
|
|
|
|
title: 'Update Password',
|
|
|
|
endpoint: '/users/api/change-password',
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function getActiveUsers(request: FrontPull<IUserInfo[]>) {
|
|
|
|
AxiosGet({
|
2023-07-15 17:57:25 +03:00
|
|
|
title: 'Active users list',
|
2023-08-01 20:14:03 +03:00
|
|
|
endpoint: '/users/api/active-users',
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:14:03 +03:00
|
|
|
export function getLibrary(request: FrontPull<IRSFormMeta[]>) {
|
2023-07-26 23:11:00 +03:00
|
|
|
AxiosGet({
|
2023-08-01 20:14:03 +03:00
|
|
|
title: 'Available RSForms (Library) list',
|
|
|
|
endpoint: '/api/library/',
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function postNewRSForm(request: FrontExchange<IRSFormCreateData, IRSFormMeta>) {
|
|
|
|
AxiosPost({
|
2023-07-25 20:27:29 +03:00
|
|
|
title: 'New RSForm',
|
2023-08-01 20:14:03 +03:00
|
|
|
endpoint: '/api/rsforms/create-detailed/',
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request,
|
|
|
|
options: {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'multipart/form-data'
|
|
|
|
}
|
|
|
|
}
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-28 01:37:26 +03:00
|
|
|
export function postCloneRSForm(schema: string, request: FrontExchange<IRSFormCreateData, IRSFormData>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: 'clone RSForm',
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${schema}/clone/`,
|
2023-07-28 01:37:26 +03:00
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function getRSFormDetails(target: string, request: FrontPull<IRSFormData>) {
|
|
|
|
AxiosGet({
|
2023-07-15 17:57:25 +03:00
|
|
|
title: `RSForm details for id=${target}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${target}/details/`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function patchRSForm(target: string, request: FrontExchange<IRSFormUpdateData, IRSFormMeta>) {
|
|
|
|
AxiosPatch({
|
2023-07-15 17:57:25 +03:00
|
|
|
title: `RSForm id=${target}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${target}/`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-18 14:55:40 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function deleteRSForm(target: string, request: FrontAction) {
|
|
|
|
AxiosDelete({
|
2023-07-15 17:57:25 +03:00
|
|
|
title: `RSForm id=${target}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${target}/`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function postClaimRSForm(target: string, request: FrontPull<IRSFormMeta>) {
|
|
|
|
AxiosPost({
|
2023-07-15 17:57:25 +03:00
|
|
|
title: `Claim on RSForm id=${target}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${target}/claim/`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function getTRSFile(target: string, request: FrontPull<Blob>) {
|
|
|
|
AxiosGet({
|
|
|
|
title: `RSForm TRS file for id=${target}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${target}/export-trs/`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request,
|
|
|
|
options: { responseType: 'blob' }
|
2023-07-20 17:11:03 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function postNewConstituenta(schema: string, request: FrontExchange<ICstCreateData, ICstCreatedResponse>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: `New Constituenta for RSForm id=${schema}: ${request.data.alias}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${schema}/cst-create/`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-23 21:38:04 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function patchDeleteConstituenta(schema: string, request: FrontExchange<IConstituentaList, IRSFormData>) {
|
|
|
|
AxiosPatch({
|
|
|
|
title: `Delete Constituents for RSForm id=${schema}: ${request.data.items.map(item => String(item.id)).join(' ')}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${schema}/cst-multidelete/`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-23 15:23:01 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function patchConstituenta(target: string, request: FrontExchange<ICstUpdateData, IConstituentaMeta>) {
|
|
|
|
AxiosPatch({
|
|
|
|
title: `Constituenta id=${target}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/constituents/${target}/`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function patchMoveConstituenta(schema: string, request: FrontExchange<ICstMovetoData, IRSFormData>) {
|
|
|
|
AxiosPatch({
|
|
|
|
title: `Moving Constituents for RSForm id=${schema}: ${JSON.stringify(request.data.items)} to ${request.data.move_to}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${schema}/cst-moveto/`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-24 22:34:03 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-29 03:31:21 +03:00
|
|
|
export function postCheckExpression(schema: string, request: FrontExchange<IRSExpression, IExpressionParse>) {
|
2023-07-26 23:11:00 +03:00
|
|
|
AxiosPost({
|
|
|
|
title: `Check expression for RSForm id=${schema}: ${request.data.expression }`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${schema}/check/`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
|
|
|
});
|
2023-07-25 20:27:29 +03:00
|
|
|
}
|
|
|
|
|
2023-07-27 22:04:25 +03:00
|
|
|
export function patchResetAliases(target: string, request: FrontPull<IRSFormData>) {
|
|
|
|
AxiosPatch({
|
|
|
|
title: `Reset alias for RSForm id=${target}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${target}/reset-aliases/`,
|
2023-07-27 22:04:25 +03:00
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function patchUploadTRS(target: string, request: FrontExchange<IRSFormUploadData, IRSFormData>) {
|
|
|
|
AxiosPatch({
|
|
|
|
title: `Replacing data with trs file for RSForm id=${target}`,
|
2023-08-01 11:44:33 +03:00
|
|
|
endpoint: `/api/rsforms/${target}/load-trs/`,
|
2023-07-27 22:04:25 +03:00
|
|
|
request: request,
|
|
|
|
options: {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'multipart/form-data'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
// ============ Helper functions =============
|
|
|
|
function AxiosGet<ResponseData>({ endpoint, request, title, options }: IAxiosRequest<undefined, ResponseData>) {
|
2023-07-27 22:04:25 +03:00
|
|
|
console.log(`REQUEST: [[${title}]]`);
|
2023-07-28 18:23:37 +03:00
|
|
|
if (request.setLoading) request.setLoading(true);
|
2023-07-26 23:11:00 +03:00
|
|
|
axios.get<ResponseData>(endpoint, options)
|
2023-07-25 20:27:29 +03:00
|
|
|
.then((response) => {
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(false);
|
|
|
|
if (request.onSuccess) request.onSuccess(response.data);
|
2023-07-25 20:27:29 +03:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(false);
|
|
|
|
if (request.showError) toast.error(error.message);
|
|
|
|
if (request.onError) request.onError(error);
|
2023-07-25 20:27:29 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
function AxiosPost<RequestData, ResponseData>(
|
|
|
|
{ endpoint, request, title, options }: IAxiosRequest<RequestData, ResponseData>
|
|
|
|
) {
|
2023-07-27 22:04:25 +03:00
|
|
|
console.log(`POST: [[${title}]]`);
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(true);
|
|
|
|
axios.post<ResponseData>(endpoint, request.data, options)
|
2023-07-25 20:27:29 +03:00
|
|
|
.then((response) => {
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(false);
|
|
|
|
if (request.onSuccess) request.onSuccess(response.data);
|
2023-07-25 20:27:29 +03:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(false);
|
|
|
|
if (request.showError) toast.error(error.message);
|
|
|
|
if (request.onError) request.onError(error);
|
2023-07-25 20:27:29 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
function AxiosDelete<RequestData, ResponseData>(
|
|
|
|
{ endpoint, request, title, options }: IAxiosRequest<RequestData, ResponseData>
|
|
|
|
) {
|
2023-07-27 22:04:25 +03:00
|
|
|
console.log(`DELETE: [[${title}]]`);
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(true);
|
|
|
|
axios.delete<ResponseData>(endpoint, options)
|
2023-07-25 20:27:29 +03:00
|
|
|
.then((response) => {
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(false);
|
|
|
|
if (request.onSuccess) request.onSuccess(response.data);
|
2023-07-25 20:27:29 +03:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(false);
|
|
|
|
if (request.showError) toast.error(error.message);
|
|
|
|
if (request.onError) request.onError(error);
|
2023-07-25 20:27:29 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
function AxiosPatch<RequestData, ResponseData>(
|
|
|
|
{ endpoint, request, title, options }: IAxiosRequest<RequestData, ResponseData>
|
|
|
|
) {
|
2023-07-27 22:04:25 +03:00
|
|
|
console.log(`PATCH: [[${title}]]`);
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(true);
|
|
|
|
axios.patch<ResponseData>(endpoint, request.data, options)
|
2023-07-25 20:27:29 +03:00
|
|
|
.then((response) => {
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(false);
|
|
|
|
if (request.onSuccess) request.onSuccess(response.data);
|
2023-07-25 20:27:29 +03:00
|
|
|
return response.data;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-07-26 23:11:00 +03:00
|
|
|
if (request.setLoading) request.setLoading(false);
|
|
|
|
if (request.showError) toast.error(error.message);
|
|
|
|
if (request.onError) request.onError(error);
|
2023-07-25 20:27:29 +03:00
|
|
|
});
|
|
|
|
}
|