2023-07-15 17:57:25 +03:00
|
|
|
import axios, { AxiosResponse } from 'axios'
|
|
|
|
import { config } from './constants'
|
2023-07-20 17:11:03 +03:00
|
|
|
import { ErrorInfo } from '../components/BackendError'
|
2023-07-15 17:57:25 +03:00
|
|
|
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'
|
2023-07-15 17:57:25 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-16 20:25:55 +03:00
|
|
|
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`
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:57:25 +03:00
|
|
|
AxiosGet<IRSForm[]>({
|
|
|
|
title: `RSForms list`,
|
2023-07-16 20:25:55 +03:00
|
|
|
endpoint: endpoint,
|
2023-07-15 17:57:25 +03:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-15 17:57:25 +03:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:57:25 +03:00
|
|
|
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) {
|
2023-07-24 22:34:03 +03:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-23 15:23:01 +03:00
|
|
|
export async function postNewConstituenta(schema: string, request?: IFrontRequest) {
|
2023-07-24 22:34:03 +03:00
|
|
|
AxiosPost({
|
2023-07-23 15:23:01 +03:00
|
|
|
title: `New Constituenta for RSForm id=${schema}: ${request?.data['alias']}`,
|
2023-07-23 21:38:04 +03:00
|
|
|
endpoint: `${config.url.BASE}rsforms/${schema}/cst-create/`,
|
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-25 00:20:37 +03:00
|
|
|
export async function patchDeleteConstituenta(schema: string, request?: IFrontRequest) {
|
|
|
|
AxiosPatch<IRSForm>({
|
2023-07-23 21:38:04 +03:00
|
|
|
title: `Delete Constituents for RSForm id=${schema}: ${request?.data['items'].toString()}`,
|
|
|
|
endpoint: `${config.url.BASE}rsforms/${schema}/cst-multidelete/`,
|
2023-07-23 15:23:01 +03:00
|
|
|
request: request
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-24 22:34:03 +03:00
|
|
|
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
|
|
|
|
2023-07-15 17:57:25 +03:00
|
|
|
// ====== Helper functions ===========
|
2023-07-23 21:38:04 +03:00
|
|
|
async function AxiosGet<ReturnType>({endpoint, request, title}: IAxiosRequest) {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (title) console.log(`[[${title}]] requested`);
|
|
|
|
if (request?.setLoading) request?.setLoading(true);
|
|
|
|
axios.get<ReturnType>(endpoint)
|
2023-07-23 21:38:04 +03:00
|
|
|
.then((response) => {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (request?.setLoading) request?.setLoading(false);
|
|
|
|
if (request?.onSucccess) request.onSucccess(response);
|
|
|
|
})
|
2023-07-23 21:38:04 +03:00
|
|
|
.catch((error) => {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (request?.setLoading) request?.setLoading(false);
|
|
|
|
if (request?.showError) toast.error(error.message);
|
|
|
|
if (request?.onError) request.onError(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
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'})
|
2023-07-23 21:38:04 +03:00
|
|
|
.then((response) => {
|
2023-07-16 22:25:23 +03:00
|
|
|
if (request?.setLoading) request?.setLoading(false);
|
|
|
|
if (request?.onSucccess) request.onSucccess(response);
|
|
|
|
})
|
2023-07-23 21:38:04 +03:00
|
|
|
.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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
async function AxiosPost({endpoint, request, title}: IAxiosRequest) {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (title) console.log(`[[${title}]] posted`);
|
|
|
|
if (request?.setLoading) request?.setLoading(true);
|
|
|
|
axios.post(endpoint, request?.data)
|
2023-07-23 21:38:04 +03:00
|
|
|
.then((response) => {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (request?.setLoading) request?.setLoading(false);
|
|
|
|
if (request?.onSucccess) request.onSucccess(response);
|
|
|
|
})
|
2023-07-23 21:38:04 +03:00
|
|
|
.catch((error) => {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (request?.setLoading) request?.setLoading(false);
|
|
|
|
if (request?.showError) toast.error(error.message);
|
|
|
|
if (request?.onError) request.onError(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
async function AxiosDelete({endpoint, request, title}: IAxiosRequest) {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (title) console.log(`[[${title}]] is being deleted`);
|
|
|
|
if (request?.setLoading) request?.setLoading(true);
|
|
|
|
axios.delete(endpoint)
|
2023-07-23 21:38:04 +03:00
|
|
|
.then((response) => {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (request?.setLoading) request?.setLoading(false);
|
|
|
|
if (request?.onSucccess) request.onSucccess(response);
|
|
|
|
})
|
2023-07-23 21:38:04 +03:00
|
|
|
.catch((error) => {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (request?.setLoading) request?.setLoading(false);
|
|
|
|
if (request?.showError) toast.error(error.message);
|
|
|
|
if (request?.onError) request.onError(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-24 22:34:03 +03:00
|
|
|
async function AxiosPatch<ReturnType>({endpoint, request, title}: IAxiosRequest) {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (title) console.log(`[[${title}]] is being patrially updated`);
|
|
|
|
if (request?.setLoading) request?.setLoading(true);
|
2023-07-24 22:34:03 +03:00
|
|
|
axios.patch<ReturnType>(endpoint, request?.data)
|
2023-07-23 21:38:04 +03:00
|
|
|
.then((response) => {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (request?.setLoading) request?.setLoading(false);
|
|
|
|
if (request?.onSucccess) request.onSucccess(response);
|
2023-07-24 22:34:03 +03:00
|
|
|
return response.data;
|
2023-07-15 17:57:25 +03:00
|
|
|
})
|
2023-07-23 21:38:04 +03:00
|
|
|
.catch((error) => {
|
2023-07-15 17:57:25 +03:00
|
|
|
if (request?.setLoading) request?.setLoading(false);
|
|
|
|
if (request?.showError) toast.error(error.message);
|
|
|
|
if (request?.onError) request.onError(error);
|
|
|
|
});
|
|
|
|
}
|