2023-11-17 20:51:13 +03:00
|
|
|
/**
|
|
|
|
* Module: API for backend communications.
|
|
|
|
*/
|
|
|
|
|
2023-09-25 14:17:52 +03:00
|
|
|
import axios, { AxiosError, AxiosRequestConfig } from 'axios';
|
|
|
|
import { toast } from 'react-toastify';
|
|
|
|
|
2024-03-20 15:27:32 +03:00
|
|
|
import { type ErrorData } from '@/components/info/InfoError';
|
2023-12-28 14:04:44 +03:00
|
|
|
import { ILexemeData, IResolutionData, ITextRequest, ITextResult, IWordFormPlain } from '@/models/language';
|
2023-09-25 14:17:52 +03:00
|
|
|
import {
|
2023-12-28 14:04:44 +03:00
|
|
|
ICurrentUser,
|
|
|
|
ILibraryItem,
|
|
|
|
ILibraryUpdateData,
|
2024-02-25 20:55:30 +03:00
|
|
|
IPasswordTokenData,
|
|
|
|
IRequestPasswordData,
|
|
|
|
IResetPasswordData as IResetPasswordData,
|
2023-12-28 14:04:44 +03:00
|
|
|
IUserInfo,
|
|
|
|
IUserLoginData,
|
|
|
|
IUserProfile,
|
|
|
|
IUserSignupData,
|
|
|
|
IUserUpdateData,
|
2024-03-04 19:22:22 +03:00
|
|
|
IUserUpdatePassword,
|
|
|
|
IVersionData
|
2023-12-13 14:32:57 +03:00
|
|
|
} from '@/models/library';
|
2023-07-26 23:11:00 +03:00
|
|
|
import {
|
2023-12-28 14:04:44 +03:00
|
|
|
IConstituentaList,
|
|
|
|
IConstituentaMeta,
|
|
|
|
ICstCreateData,
|
|
|
|
ICstCreatedResponse,
|
|
|
|
ICstMovetoData,
|
|
|
|
ICstRenameData,
|
2024-03-01 18:19:33 +03:00
|
|
|
ICstSubstituteData,
|
2024-03-21 21:05:12 +03:00
|
|
|
ICstTarget,
|
2023-12-28 14:04:44 +03:00
|
|
|
ICstUpdateData,
|
2024-03-15 12:34:41 +03:00
|
|
|
IProduceStructureResponse,
|
2023-12-28 14:04:44 +03:00
|
|
|
IRSFormCreateData,
|
|
|
|
IRSFormData,
|
2024-03-04 19:22:22 +03:00
|
|
|
IRSFormUploadData,
|
|
|
|
IVersionCreatedResponse
|
2023-12-28 14:04:44 +03:00
|
|
|
} from '@/models/rsform';
|
2023-12-13 14:32:57 +03:00
|
|
|
import { IExpressionParse, IRSExpression } from '@/models/rslang';
|
|
|
|
|
2023-12-13 15:24:10 +03:00
|
|
|
import { buildConstants } from './constants';
|
2023-08-01 11:44:33 +03:00
|
|
|
|
2023-11-10 17:19:58 +03:00
|
|
|
const defaultOptions = {
|
|
|
|
xsrfCookieName: 'csrftoken',
|
|
|
|
xsrfHeaderName: 'x-csrftoken',
|
2023-12-13 15:24:10 +03:00
|
|
|
baseURL: `${buildConstants.backend}`,
|
2023-11-10 17:19:58 +03:00
|
|
|
withCredentials: true
|
2023-12-28 14:04:44 +03:00
|
|
|
};
|
2023-11-10 17:19:58 +03:00
|
|
|
|
|
|
|
const axiosInstance = axios.create(defaultOptions);
|
2023-12-28 14:04:44 +03:00
|
|
|
axiosInstance.interceptors.request.use(config => {
|
|
|
|
const token = document.cookie
|
|
|
|
.split('; ')
|
|
|
|
.find(row => row.startsWith('csrftoken='))
|
2023-11-10 17:19:58 +03:00
|
|
|
?.split('=')[1];
|
|
|
|
if (token) {
|
|
|
|
config.headers['x-csrftoken'] = token;
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
});
|
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> {
|
2023-12-28 14:04:44 +03:00
|
|
|
data?: RequestData;
|
|
|
|
onSuccess?: DataCallback<ResponseData>;
|
|
|
|
onError?: (error: ErrorData) => void;
|
|
|
|
setLoading?: (loading: boolean) => void;
|
|
|
|
showError?: boolean;
|
2023-07-15 17:57:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export interface FrontPush<DataType> extends IFrontRequest<DataType, undefined> {
|
2023-12-28 14:04:44 +03:00
|
|
|
data: DataType;
|
2023-07-26 23:11:00 +03:00
|
|
|
}
|
2023-12-28 14:04:44 +03:00
|
|
|
export interface FrontPull<DataType> extends IFrontRequest<undefined, DataType> {
|
|
|
|
onSuccess: DataCallback<DataType>;
|
2023-07-15 17:57:25 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +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-12-28 14:04:44 +03:00
|
|
|
export interface FrontAction extends IFrontRequest<undefined, undefined> {}
|
2023-07-26 23:11:00 +03:00
|
|
|
|
|
|
|
interface IAxiosRequest<RequestData, ResponseData> {
|
2023-12-28 14:04:44 +03:00
|
|
|
endpoint: string;
|
|
|
|
request: IFrontRequest<RequestData, ResponseData>;
|
|
|
|
title: string;
|
|
|
|
options?: AxiosRequestConfig;
|
2023-07-26 23:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ==================== 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({
|
2024-02-25 20:55:30 +03:00
|
|
|
title: 'Update password',
|
2023-08-10 13:53:19 +03:00
|
|
|
endpoint: '/users/api/change-password',
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:55:30 +03:00
|
|
|
export function postRequestPasswordReset(request: FrontPush<IRequestPasswordData>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: 'Request password reset',
|
|
|
|
endpoint: '/users/api/password-reset',
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function postValidatePasswordToken(request: FrontPush<IPasswordTokenData>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: 'Validate password token',
|
|
|
|
endpoint: '/users/api/password-reset/validate',
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function postResetPassword(request: FrontPush<IResetPasswordData>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: 'Reset password',
|
|
|
|
endpoint: '/users/api/password-reset/confirm',
|
|
|
|
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-25 22:51:20 +03:00
|
|
|
export function getLibrary(request: FrontPull<ILibraryItem[]>) {
|
2023-07-26 23:11:00 +03:00
|
|
|
AxiosGet({
|
2023-08-28 10:50:10 +03:00
|
|
|
title: 'Available LibraryItems list',
|
2023-08-26 17:26:49 +03:00
|
|
|
endpoint: '/api/library/active',
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-01 13:41:32 +03:00
|
|
|
export function getTemplates(request: FrontPull<ILibraryItem[]>) {
|
|
|
|
AxiosGet({
|
|
|
|
title: 'Available LibraryItems list',
|
|
|
|
endpoint: '/api/library/templates',
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:51:20 +03:00
|
|
|
export function postNewRSForm(request: FrontExchange<IRSFormCreateData, ILibraryItem>) {
|
2023-07-26 23:11:00 +03:00
|
|
|
AxiosPost({
|
2023-07-25 20:27:29 +03:00
|
|
|
title: 'New RSForm',
|
2023-08-26 17:26:49 +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-08-25 22:51:20 +03:00
|
|
|
export function postCloneLibraryItem(target: string, request: FrontExchange<IRSFormCreateData, IRSFormData>) {
|
2023-07-28 01:37:26 +03:00
|
|
|
AxiosPost({
|
2023-08-28 10:50:10 +03:00
|
|
|
title: 'Clone RSForm',
|
2023-08-26 17:26:49 +03:00
|
|
|
endpoint: `/api/library/${target}/clone`,
|
2023-07-28 01:37:26 +03:00
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-04 19:22:22 +03:00
|
|
|
export function getRSFormDetails(target: string, version: string, request: FrontPull<IRSFormData>) {
|
|
|
|
if (!version) {
|
|
|
|
AxiosGet({
|
|
|
|
title: `RSForm details for id=${target}`,
|
|
|
|
endpoint: `/api/rsforms/${target}/details`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
AxiosGet({
|
2024-03-08 18:39:08 +03:00
|
|
|
title: `RSForm details for id=${target} version=${version}`,
|
|
|
|
endpoint: `/api/rsforms/${target}/versions/${version}`,
|
2024-03-04 19:22:22 +03:00
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
2023-07-15 17:57:25 +03:00
|
|
|
}
|
|
|
|
|
2023-08-25 22:51:20 +03:00
|
|
|
export function patchLibraryItem(target: string, request: FrontExchange<ILibraryUpdateData, ILibraryItem>) {
|
2023-07-26 23:11:00 +03:00
|
|
|
AxiosPatch({
|
2023-08-28 10:50:10 +03:00
|
|
|
title: `LibraryItem id=${target}`,
|
2023-08-26 17:26:49 +03:00
|
|
|
endpoint: `/api/library/${target}`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-18 14:55:40 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:51:20 +03:00
|
|
|
export function deleteLibraryItem(target: string, request: FrontAction) {
|
2023-07-26 23:11:00 +03:00
|
|
|
AxiosDelete({
|
2023-08-28 10:50:10 +03:00
|
|
|
title: `LibraryItem id=${target}`,
|
2023-08-26 17:26:49 +03:00
|
|
|
endpoint: `/api/library/${target}`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:51:20 +03:00
|
|
|
export function postClaimLibraryItem(target: string, request: FrontPull<ILibraryItem>) {
|
2023-07-26 23:11:00 +03:00
|
|
|
AxiosPost({
|
2023-12-27 19:34:39 +03:00
|
|
|
title: `Claim on LibraryItem id=${target}`,
|
2023-08-26 17:26:49 +03:00
|
|
|
endpoint: `/api/library/${target}/claim`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function postSubscribe(target: string, request: FrontAction) {
|
|
|
|
AxiosPost({
|
2023-12-27 19:34:39 +03:00
|
|
|
title: `Subscribe to LibraryItem id=${target}`,
|
2023-08-26 17:26:49 +03:00
|
|
|
endpoint: `/api/library/${target}/subscribe`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteUnsubscribe(target: string, request: FrontAction) {
|
|
|
|
AxiosDelete({
|
2023-08-28 10:50:10 +03:00
|
|
|
title: `Unsubscribe from LibraryItem id=${target}`,
|
2023-08-26 17:26:49 +03:00
|
|
|
endpoint: `/api/library/${target}/unsubscribe`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
2023-07-15 17:57:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-08 18:39:08 +03:00
|
|
|
export function getTRSFile(target: string, version: string, request: FrontPull<Blob>) {
|
|
|
|
if (!version) {
|
|
|
|
AxiosGet({
|
|
|
|
title: `RSForm TRS file for id=${target}`,
|
|
|
|
endpoint: `/api/rsforms/${target}/export-trs`,
|
|
|
|
request: request,
|
|
|
|
options: { responseType: 'blob' }
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
AxiosGet({
|
|
|
|
title: `RSForm TRS file for id=${target} version=${version}`,
|
|
|
|
endpoint: `/api/versions/${version}/export-file`,
|
|
|
|
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-26 17:26:49 +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({
|
2023-09-22 23:26:22 +03:00
|
|
|
title: `Delete Constituents for RSForm id=${schema}: ${request.data.items.map(item => String(item)).join(' ')}`,
|
2023-12-27 19:34:39 +03:00
|
|
|
endpoint: `/api/rsforms/${schema}/cst-delete-multiple`,
|
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-26 17:26:49 +03:00
|
|
|
endpoint: `/api/constituents/${target}`,
|
2023-07-26 23:11:00 +03:00
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:15:16 +03:00
|
|
|
export function patchRenameConstituenta(schema: string, request: FrontExchange<ICstRenameData, ICstCreatedResponse>) {
|
2023-08-23 01:36:17 +03:00
|
|
|
AxiosPatch({
|
2024-03-21 21:05:12 +03:00
|
|
|
title: `Renaming constituenta id=${request.data.target} for schema id=${schema}`,
|
2023-08-26 17:26:49 +03:00
|
|
|
endpoint: `/api/rsforms/${schema}/cst-rename`,
|
2023-08-23 01:36:17 +03:00
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-21 21:05:12 +03:00
|
|
|
export function patchProduceStructure(schema: string, request: FrontExchange<ICstTarget, IProduceStructureResponse>) {
|
2024-03-15 12:34:41 +03:00
|
|
|
AxiosPatch({
|
2024-03-21 21:05:12 +03:00
|
|
|
title: `Producing structure constituenta id=${request.data.target} for schema id=${schema}`,
|
2024-03-15 12:34:41 +03:00
|
|
|
endpoint: `/api/rsforms/${schema}/cst-produce-structure`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-01 18:19:33 +03:00
|
|
|
export function patchSubstituteConstituenta(schema: string, request: FrontExchange<ICstSubstituteData, IRSFormData>) {
|
|
|
|
AxiosPatch({
|
|
|
|
title: `Substitution for constituenta id=${request.data.original} for schema id=${schema}`,
|
|
|
|
endpoint: `/api/rsforms/${schema}/cst-substitute`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
export function patchMoveConstituenta(schema: string, request: FrontExchange<ICstMovetoData, IRSFormData>) {
|
|
|
|
AxiosPatch({
|
2023-12-28 14:04:44 +03:00
|
|
|
title: `Moving Constituents for RSForm id=${schema}: ${JSON.stringify(request.data.items)} to ${
|
|
|
|
request.data.move_to
|
|
|
|
}`,
|
2023-08-26 17:26:49 +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({
|
2023-12-28 14:04:44 +03:00
|
|
|
title: `Check expression for RSForm id=${schema}: ${request.data.expression}`,
|
2023-08-26 17:26:49 +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-26 17:26:49 +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-26 17:26:49 +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-09-25 14:17:52 +03:00
|
|
|
export function postResolveText(schema: string, request: FrontExchange<ITextRequest, IResolutionData>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: `Resolve text references for RSForm id=${schema}: ${request.data.text}`,
|
|
|
|
endpoint: `/api/rsforms/${schema}/resolve`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function postInflectText(request: FrontExchange<IWordFormPlain, ITextResult>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: `Inflect text ${request.data.text} to ${request.data.grams}`,
|
|
|
|
endpoint: `/api/cctext/inflect`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function postParseText(request: FrontExchange<ITextRequest, ITextResult>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: `Parse text ${request.data.text}`,
|
|
|
|
endpoint: `/api/cctext/parse`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function postGenerateLexeme(request: FrontExchange<ITextRequest, ILexemeData>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: `Parse text ${request.data.text}`,
|
|
|
|
endpoint: `/api/cctext/generate-lexeme`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-04 19:22:22 +03:00
|
|
|
export function postCreateVersion(target: string, request: FrontExchange<IVersionData, IVersionCreatedResponse>) {
|
|
|
|
AxiosPost({
|
|
|
|
title: `Create version for RSForm id=${target}`,
|
|
|
|
endpoint: `/api/rsforms/${target}/versions/create`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function patchVersion(target: string, request: FrontPush<IVersionData>) {
|
|
|
|
AxiosPatch({
|
|
|
|
title: `Version id=${target}`,
|
|
|
|
endpoint: `/api/versions/${target}`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteVersion(target: string, request: FrontAction) {
|
|
|
|
AxiosDelete({
|
|
|
|
title: `Version id=${target}`,
|
|
|
|
endpoint: `/api/versions/${target}`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-12-28 14:04:44 +03:00
|
|
|
axiosInstance
|
|
|
|
.get<ResponseData>(endpoint, options)
|
2023-08-23 18:11:42 +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
|
|
|
})
|
2023-08-23 18:11:42 +03:00
|
|
|
.catch((error: Error | AxiosError) => {
|
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-12-28 14:04:44 +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);
|
2023-12-28 14:04:44 +03:00
|
|
|
axiosInstance
|
|
|
|
.post<ResponseData>(endpoint, request.data, options)
|
2023-08-23 18:11:42 +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
|
|
|
})
|
2023-08-23 18:11:42 +03:00
|
|
|
.catch((error: Error | AxiosError) => {
|
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-12-28 14:04:44 +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);
|
2023-12-28 14:04:44 +03:00
|
|
|
axiosInstance
|
|
|
|
.delete<ResponseData>(endpoint, options)
|
2023-08-23 18:11:42 +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
|
|
|
})
|
2023-08-23 18:11:42 +03:00
|
|
|
.catch((error: Error | AxiosError) => {
|
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-12-28 14:04:44 +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);
|
2023-12-28 14:04:44 +03:00
|
|
|
axiosInstance
|
|
|
|
.patch<ResponseData>(endpoint, request.data, options)
|
2023-08-23 18:11:42 +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;
|
|
|
|
})
|
2023-08-23 18:11:42 +03:00
|
|
|
.catch((error: Error | AxiosError) => {
|
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-12-28 14:04:44 +03:00
|
|
|
}
|