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';
|
|
|
|
|
|
|
|
import { type ErrorInfo } from '../components/BackendError';
|
|
|
|
import {
|
|
|
|
ILexemeData,
|
|
|
|
IResolutionData, ITextRequest,
|
|
|
|
ITextResult, IWordFormPlain
|
|
|
|
} from '../models/language';
|
|
|
|
import {
|
|
|
|
ICurrentUser, ILibraryItem, ILibraryUpdateData,
|
|
|
|
IUserInfo, IUserLoginData, IUserProfile, IUserSignupData,
|
|
|
|
IUserUpdateData, IUserUpdatePassword
|
|
|
|
} from '../models/library';
|
2023-07-26 23:11:00 +03:00
|
|
|
import {
|
2023-08-01 11:44:33 +03:00
|
|
|
IConstituentaList, IConstituentaMeta,
|
2023-08-23 01:36:17 +03:00
|
|
|
ICstCreateData, ICstCreatedResponse, ICstMovetoData, ICstRenameData, ICstUpdateData,
|
2023-09-25 14:17:52 +03:00
|
|
|
IRSFormCreateData, IRSFormData, IRSFormUploadData} from '../models/rsform';
|
|
|
|
import { IExpressionParse, IRSExpression } from '../models/rslang';
|
2023-11-17 20:51:13 +03:00
|
|
|
import { buidConstants } 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-11-17 20:51:13 +03:00
|
|
|
baseURL: `${buidConstants.backend}`,
|
2023-11-10 17:19:58 +03:00
|
|
|
withCredentials: true
|
|
|
|
}
|
|
|
|
|
|
|
|
const axiosInstance = axios.create(defaultOptions);
|
|
|
|
axiosInstance.interceptors.request.use(
|
|
|
|
(config) => {
|
|
|
|
const token = document.cookie.split('; ')
|
|
|
|
.find((row) => row.startsWith('csrftoken='))
|
|
|
|
?.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> {
|
|
|
|
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-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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-26 17:26:49 +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-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-08-28 10:50:10 +03:00
|
|
|
title: `Claim on LibrartyItem 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-08-28 10:50:10 +03:00
|
|
|
title: `Subscribe to LibrartyItem 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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-26 17:26:49 +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-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-08-26 17:26:49 +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-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({
|
|
|
|
title: `Renaming constituenta id=${request.data.id} 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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 23:11:00 +03:00
|
|
|
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-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({
|
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-11-10 17:19:58 +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-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);
|
2023-11-10 17:19:58 +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-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);
|
2023-11-10 17:19:58 +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-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);
|
2023-11-10 17:19:58 +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
|
|
|
});
|
|
|
|
}
|