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

253 lines
7.6 KiB
TypeScript
Raw Normal View History

import axios, { AxiosResponse } from 'axios'
import { config } from './constants'
2023-07-20 17:11:03 +03:00
import { ErrorInfo } from '../components/BackendError'
import { toast } from 'react-toastify'
import { ICurrentUser, IRSForm, IUserInfo, IUserProfile } from './models'
2023-07-20 17:11:03 +03:00
import { FilterType, RSFormsFilter } from '../hooks/useRSForms'
export type BackendCallback = (response: AxiosResponse) => void;
export interface IFrontRequest {
onSucccess?: 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) {
AxiosPost({
title: 'Login',
endpoint: `${config.url.AUTH}login`,
request: request
});
}
export async function getAuth(request?: IFrontRequest) {
AxiosGet<ICurrentUser>({
title: 'Current user',
endpoint: `${config.url.AUTH}auth`,
request: request
});
}
export async function getProfile(request?: IFrontRequest) {
AxiosGet<IUserProfile>({
title: 'Current user profile',
endpoint: `${config.url.AUTH}profile`,
request: request
});
}
export async function postLogout(request?: IFrontRequest) {
AxiosPost({
title: 'Logout',
endpoint: `${config.url.AUTH}logout`,
request: request
});
};
export async function postSignup(request?: IFrontRequest) {
AxiosPost({
title: 'Register user',
endpoint: `${config.url.AUTH}signup`,
request: request
});
}
export async function getActiveUsers(request?: IFrontRequest) {
AxiosGet<IUserInfo>({
title: 'Active users list',
endpoint: `${config.url.AUTH}active-users`,
request: request
});
}
export async function getRSForms(filter: RSFormsFilter, request?: IFrontRequest) {
let endpoint: string = ''
if (filter.type === FilterType.PERSONAL) {
endpoint = `${config.url.BASE}rsforms?owner=${filter.data!}`
} else {
endpoint = `${config.url.BASE}rsforms?is_common=true`
}
AxiosGet<IRSForm[]>({
title: `RSForms list`,
endpoint: endpoint,
request: request
});
}
export async function postNewRSForm(request?: IFrontRequest) {
AxiosPost({
title: `New RSForm`,
endpoint: `${config.url.BASE}rsforms/create-detailed/`,
request: request
});
}
export async function getRSFormDetails(target: string, request?: IFrontRequest) {
AxiosGet<IRSForm>({
title: `RSForm details for id=${target}`,
endpoint: `${config.url.BASE}rsforms/${target}/details/`,
request: request
});
}
export async function patchRSForm(target: string, request?: IFrontRequest) {
AxiosPatch({
title: `RSForm id=${target}`,
endpoint: `${config.url.BASE}rsforms/${target}/`,
request: request
});
}
2023-07-18 14:55:40 +03:00
export async function patchConstituenta(target: string, request?: IFrontRequest) {
AxiosPatch({
title: `Constituenta id=${target}`,
endpoint: `${config.url.BASE}constituents/${target}/`,
request: request
});
}
export async function deleteRSForm(target: string, request?: IFrontRequest) {
AxiosDelete({
title: `RSForm id=${target}`,
endpoint: `${config.url.BASE}rsforms/${target}/`,
request: request
});
}
2023-07-16 22:25:23 +03:00
export async function getTRSFile(target: string, request?: IFrontRequest) {
AxiosGetBlob({
title: `RSForm TRS file for id=${target}`,
endpoint: `${config.url.BASE}rsforms/${target}/export-trs/`,
request: request
});
}
export async function postClaimRSForm(target: string, request?: IFrontRequest) {
AxiosPost({
title: `Claim on RSForm id=${target}`,
endpoint: `${config.url.BASE}rsforms/${target}/claim/`,
request: request
});
}
2023-07-20 17:11:03 +03:00
export async function postCheckExpression(schema: string, request?: IFrontRequest) {
AxiosPost({
2023-07-20 17:11:03 +03:00
title: `Check expression for RSForm id=${schema}: ${request?.data['expression']}`,
endpoint: `${config.url.BASE}rsforms/${schema}/check/`,
request: request
});
}
export async function postNewConstituenta(schema: string, request?: IFrontRequest) {
AxiosPost({
title: `New Constituenta for RSForm id=${schema}: ${request?.data['alias']}`,
endpoint: `${config.url.BASE}rsforms/${schema}/cst-create/`,
request: request
});
}
export async function postDeleteConstituenta(schema: string, request?: IFrontRequest) {
AxiosPost({
title: `Delete Constituents for RSForm id=${schema}: ${request?.data['items'].toString()}`,
endpoint: `${config.url.BASE}rsforms/${schema}/cst-multidelete/`,
request: request
});
}
export async function patchMoveConstituenta(schema: string, request?: IFrontRequest) {
AxiosPatch<IRSForm>({
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/`,
request: request
});
}
2023-07-20 17:11:03 +03:00
// ====== Helper functions ===========
async function AxiosGet<ReturnType>({endpoint, request, title}: IAxiosRequest) {
if (title) console.log(`[[${title}]] requested`);
if (request?.setLoading) request?.setLoading(true);
axios.get<ReturnType>(endpoint)
.then((response) => {
if (request?.setLoading) request?.setLoading(false);
if (request?.onSucccess) request.onSucccess(response);
})
.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);
axios.get(endpoint, {responseType: 'blob'})
.then((response) => {
2023-07-16 22:25:23 +03:00
if (request?.setLoading) request?.setLoading(false);
if (request?.onSucccess) request.onSucccess(response);
})
.catch((error) => {
2023-07-16 22:25:23 +03:00
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)
.then((response) => {
if (request?.setLoading) request?.setLoading(false);
if (request?.onSucccess) request.onSucccess(response);
})
.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)
.then((response) => {
if (request?.setLoading) request?.setLoading(false);
if (request?.onSucccess) request.onSucccess(response);
})
.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)
.then((response) => {
if (request?.setLoading) request?.setLoading(false);
if (request?.onSucccess) request.onSucccess(response);
return response.data;
})
.catch((error) => {
if (request?.setLoading) request?.setLoading(false);
if (request?.showError) toast.error(error.message);
if (request?.onError) request.onError(error);
});
}