import axios, { AxiosResponse } from 'axios' import { config } from './constants' import { ErrorInfo } from '../components/BackendError' import { toast } from 'react-toastify' import { ICurrentUser, IRSForm, IUserInfo, IUserProfile } from './models' 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({ title: 'Current user', endpoint: `${config.url.AUTH}auth`, request: request }); } export async function getProfile(request?: IFrontRequest) { AxiosGet({ 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({ 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({ 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({ 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 }); } 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 }); } 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 }); } export async function postCheckExpression(schema: string, request?: IFrontRequest) { AxiosPost({ 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({ 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 }); } // ====== Helper functions =========== async function AxiosGet({endpoint, request, title}: IAxiosRequest) { if (title) console.log(`[[${title}]] requested`); if (request?.setLoading) request?.setLoading(true); axios.get(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) { if (title) console.log(`[[${title}]] requested`); if (request?.setLoading) request?.setLoading(true); axios.get(endpoint, {responseType: 'blob'}) .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 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({endpoint, request, title}: IAxiosRequest) { if (title) console.log(`[[${title}]] is being patrially updated`); if (request?.setLoading) request?.setLoading(true); axios.patch(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); }); }