ConceptPortal-public/rsconcept/frontend/src/utils/backendAPI.ts

254 lines
7.8 KiB
TypeScript
Raw Normal View History

2023-07-25 20:27:29 +03:00
import axios, { type AxiosResponse } from 'axios'
import { toast } from 'react-toastify'
2023-07-25 20:27:29 +03:00
import { type ErrorInfo } from '../components/BackendError'
import { FilterType, type RSFormsFilter } from '../hooks/useRSForms'
import { config } from './constants'
import { type ICurrentUser, type IRSForm, type IUserInfo, type IUserProfile } from './models'
export type BackendCallback = (response: AxiosResponse) => void;
export interface IFrontRequest {
2023-07-26 10:59:55 +03:00
onSuccess?: BackendCallback
onError?: (error: ErrorInfo) => void
setLoading?: (loading: boolean) => void
showError?: boolean
data?: any
}
interface IAxiosRequest {
endpoint: string
request?: IFrontRequest
title?: string
}
// ================= Export API ==============
export async function postLogin(request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPost({
title: 'Login',
endpoint: `${config.url.AUTH}login`,
2023-07-25 20:27:29 +03:00
request
});
}
export async function getAuth(request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosGet<ICurrentUser>({
title: 'Current user',
endpoint: `${config.url.AUTH}auth`,
2023-07-25 20:27:29 +03:00
request
});
}
export async function getProfile(request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosGet<IUserProfile>({
title: 'Current user profile',
endpoint: `${config.url.AUTH}profile`,
2023-07-25 20:27:29 +03:00
request
});
}
export async function postLogout(request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPost({
title: 'Logout',
endpoint: `${config.url.AUTH}logout`,
2023-07-25 20:27:29 +03:00
request
});
};
export async function postSignup(request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPost({
title: 'Register user',
endpoint: `${config.url.AUTH}signup`,
2023-07-25 20:27:29 +03:00
request
});
}
export async function getActiveUsers(request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosGet<IUserInfo>({
title: 'Active users list',
endpoint: `${config.url.AUTH}active-users`,
2023-07-25 20:27:29 +03:00
request
});
}
export async function getRSForms(filter: RSFormsFilter, request?: IFrontRequest) {
let endpoint: string = ''
if (filter.type === FilterType.PERSONAL) {
2023-07-25 20:27:29 +03:00
endpoint = `${config.url.BASE}rsforms?owner=${filter.data as number}`
} else {
endpoint = `${config.url.BASE}rsforms?is_common=true`
}
2023-07-25 20:27:29 +03:00
await AxiosGet<IRSForm[]>({
title: 'RSForms list',
endpoint,
request
});
}
export async function postNewRSForm(request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPost({
title: 'New RSForm',
endpoint: `${config.url.BASE}rsforms/create-detailed/`,
2023-07-25 20:27:29 +03:00
request
});
}
export async function getRSFormDetails(target: string, request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosGet<IRSForm>({
title: `RSForm details for id=${target}`,
endpoint: `${config.url.BASE}rsforms/${target}/details/`,
2023-07-25 20:27:29 +03:00
request
});
}
export async function patchRSForm(target: string, request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPatch({
title: `RSForm id=${target}`,
endpoint: `${config.url.BASE}rsforms/${target}/`,
2023-07-25 20:27:29 +03:00
request
});
}
2023-07-18 14:55:40 +03:00
export async function patchConstituenta(target: string, request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPatch({
2023-07-18 14:55:40 +03:00
title: `Constituenta id=${target}`,
endpoint: `${config.url.BASE}constituents/${target}/`,
2023-07-25 20:27:29 +03:00
request
2023-07-18 14:55:40 +03:00
});
}
export async function deleteRSForm(target: string, request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosDelete({
title: `RSForm id=${target}`,
endpoint: `${config.url.BASE}rsforms/${target}/`,
2023-07-25 20:27:29 +03:00
request
});
}
2023-07-16 22:25:23 +03:00
export async function getTRSFile(target: string, request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosGetBlob({
2023-07-16 22:25:23 +03:00
title: `RSForm TRS file for id=${target}`,
endpoint: `${config.url.BASE}rsforms/${target}/export-trs/`,
2023-07-25 20:27:29 +03:00
request
2023-07-16 22:25:23 +03:00
});
}
export async function postClaimRSForm(target: string, request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPost({
title: `Claim on RSForm id=${target}`,
endpoint: `${config.url.BASE}rsforms/${target}/claim/`,
2023-07-25 20:27:29 +03:00
request
});
}
2023-07-20 17:11:03 +03:00
export async function postCheckExpression(schema: string, request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPost({
title: `Check expression for RSForm id=${schema}: ${request?.data.expression as string}`,
2023-07-20 17:11:03 +03:00
endpoint: `${config.url.BASE}rsforms/${schema}/check/`,
2023-07-25 20:27:29 +03:00
request
2023-07-20 17:11:03 +03:00
});
}
export async function postNewConstituenta(schema: string, request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPost({
title: `New Constituenta for RSForm id=${schema}: ${request?.data.alias as string}`,
endpoint: `${config.url.BASE}rsforms/${schema}/cst-create/`,
2023-07-25 20:27:29 +03:00
request
});
}
export async function patchDeleteConstituenta(schema: string, request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPatch<IRSForm>({
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
title: `Delete Constituents for RSForm id=${schema}: ${request?.data.items.toString()}`,
endpoint: `${config.url.BASE}rsforms/${schema}/cst-multidelete/`,
2023-07-25 20:27:29 +03:00
request
});
}
export async function patchMoveConstituenta(schema: string, request?: IFrontRequest) {
2023-07-25 20:27:29 +03:00
await AxiosPatch<IRSForm>({
title: `Moving Constituents for RSForm id=${schema}: ${JSON.stringify(request?.data.items)} to ${request?.data.move_to as number}`,
endpoint: `${config.url.BASE}rsforms/${schema}/cst-moveto/`,
2023-07-25 20:27:29 +03:00
request
});
}
// ====== Helper functions ===========
2023-07-25 20:27:29 +03:00
async function AxiosGet<ReturnType>({ endpoint, request, title }: IAxiosRequest) {
if (title) console.log(`[[${title}]] requested`);
if (request?.setLoading) request?.setLoading(true);
axios.get<ReturnType>(endpoint)
2023-07-25 20:27:29 +03:00
.then((response) => {
if (request?.setLoading) request?.setLoading(false);
2023-07-26 10:59:55 +03:00
if (request?.onSuccess) request.onSuccess(response);
2023-07-25 20:27:29 +03:00
})
.catch((error) => {
if (request?.setLoading) request?.setLoading(false);
if (request?.showError) toast.error(error.message);
if (request?.onError) request.onError(error);
});
}
async function AxiosGetBlob({ endpoint, request, title }: IAxiosRequest) {
2023-07-16 22:25:23 +03:00
if (title) console.log(`[[${title}]] requested`);
if (request?.setLoading) request?.setLoading(true);
2023-07-25 20:27:29 +03:00
axios.get(endpoint, { responseType: 'blob' })
.then((response) => {
if (request?.setLoading) request?.setLoading(false);
2023-07-26 10:59:55 +03:00
if (request?.onSuccess) request.onSuccess(response);
2023-07-25 20:27:29 +03:00
})
.catch((error) => {
if (request?.setLoading) request?.setLoading(false);
if (request?.showError) toast.error(error.message);
if (request?.onError) request.onError(error);
});
}
async function AxiosPost({ endpoint, request, title }: IAxiosRequest) {
if (title) console.log(`[[${title}]] posted`);
if (request?.setLoading) request?.setLoading(true);
axios.post(endpoint, request?.data)
2023-07-25 20:27:29 +03:00
.then((response) => {
if (request?.setLoading) request?.setLoading(false);
2023-07-26 10:59:55 +03:00
if (request?.onSuccess) request.onSuccess(response);
2023-07-25 20:27:29 +03:00
})
.catch((error) => {
if (request?.setLoading) request?.setLoading(false);
if (request?.showError) toast.error(error.message);
if (request?.onError) request.onError(error);
});
}
async function AxiosDelete({ endpoint, request, title }: IAxiosRequest) {
if (title) console.log(`[[${title}]] is being deleted`);
if (request?.setLoading) request?.setLoading(true);
axios.delete(endpoint)
2023-07-25 20:27:29 +03:00
.then((response) => {
if (request?.setLoading) request?.setLoading(false);
2023-07-26 10:59:55 +03:00
if (request?.onSuccess) request.onSuccess(response);
2023-07-25 20:27:29 +03:00
})
.catch((error) => {
if (request?.setLoading) request?.setLoading(false);
if (request?.showError) toast.error(error.message);
if (request?.onError) request.onError(error);
});
}
async function AxiosPatch<ReturnType>({ endpoint, request, title }: IAxiosRequest) {
if (title) console.log(`[[${title}]] is being patrially updated`);
if (request?.setLoading) request?.setLoading(true);
axios.patch<ReturnType>(endpoint, request?.data)
2023-07-25 20:27:29 +03:00
.then((response) => {
if (request?.setLoading) request?.setLoading(false);
2023-07-26 10:59:55 +03:00
if (request?.onSuccess) request.onSuccess(response);
2023-07-25 20:27:29 +03:00
return response.data;
})
.catch((error) => {
if (request?.setLoading) request?.setLoading(false);
if (request?.showError) toast.error(error.message);
if (request?.onError) request.onError(error);
});
}