2023-07-16 22:25:23 +03:00
|
|
|
import { createContext, useState, useContext, useMemo } from 'react';
|
2023-07-15 17:46:19 +03:00
|
|
|
import { IConstituenta, IRSForm } from '../models';
|
|
|
|
import { useRSFormDetails } from '../hooks/useRSFormDetails';
|
|
|
|
import { ErrorInfo } from '../components/BackendError';
|
|
|
|
import { useAuth } from './AuthContext';
|
2023-07-18 14:55:40 +03:00
|
|
|
import { BackendCallback, deleteRSForm, getTRSFile, patchConstituenta, patchRSForm, postClaimRSForm } from '../backendAPI';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
|
|
interface IRSFormContext {
|
|
|
|
schema?: IRSForm
|
|
|
|
active?: IConstituenta
|
|
|
|
error: ErrorInfo
|
|
|
|
loading: boolean
|
|
|
|
processing: boolean
|
|
|
|
isEditable: boolean
|
|
|
|
isClaimable: boolean
|
|
|
|
|
2023-07-16 20:25:55 +03:00
|
|
|
setActive: (cst: IConstituenta | undefined) => void
|
2023-07-15 17:46:19 +03:00
|
|
|
reload: () => void
|
2023-07-16 22:25:23 +03:00
|
|
|
update: (data: any, callback?: BackendCallback) => void
|
2023-07-15 17:57:25 +03:00
|
|
|
destroy: (callback: BackendCallback) => void
|
|
|
|
claim: (callback: BackendCallback) => void
|
2023-07-16 22:25:23 +03:00
|
|
|
download: (callback: BackendCallback) => void
|
2023-07-18 14:55:40 +03:00
|
|
|
|
|
|
|
cstUpdate: (data: any, callback: BackendCallback) => void
|
2023-07-15 17:46:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export const RSFormContext = createContext<IRSFormContext>({
|
|
|
|
schema: undefined,
|
|
|
|
active: undefined,
|
|
|
|
error: undefined,
|
|
|
|
loading: false,
|
|
|
|
processing: false,
|
|
|
|
isEditable: false,
|
|
|
|
isClaimable: false,
|
|
|
|
|
|
|
|
setActive: () => {},
|
|
|
|
reload: () => {},
|
2023-07-16 22:25:23 +03:00
|
|
|
update: () => {},
|
2023-07-15 17:46:19 +03:00
|
|
|
destroy: () => {},
|
|
|
|
claim: () => {},
|
2023-07-16 22:25:23 +03:00
|
|
|
download: () => {},
|
2023-07-18 14:55:40 +03:00
|
|
|
|
|
|
|
cstUpdate: () => {},
|
2023-07-15 17:46:19 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
interface RSFormStateProps {
|
|
|
|
id: string
|
|
|
|
children: React.ReactNode
|
|
|
|
}
|
|
|
|
|
|
|
|
export const RSFormState = ({ id, children }: RSFormStateProps) => {
|
|
|
|
const { user } = useAuth();
|
|
|
|
const { schema, reload, error, setError, loading } = useRSFormDetails({target: id});
|
|
|
|
const [processing, setProcessing] = useState(false)
|
|
|
|
const [active, setActive] = useState<IConstituenta | undefined>(undefined);
|
|
|
|
|
|
|
|
const isEditable = useMemo(() => (user?.id === schema?.owner || user?.is_staff || false), [user, schema]);
|
|
|
|
const isClaimable = useMemo(() => (user?.id !== schema?.owner || false), [user, schema]);
|
|
|
|
|
2023-07-16 22:25:23 +03:00
|
|
|
async function update(data: any, callback?: BackendCallback) {
|
2023-07-15 17:46:19 +03:00
|
|
|
setError(undefined);
|
2023-07-15 17:57:25 +03:00
|
|
|
patchRSForm(id, {
|
|
|
|
data: data,
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
2023-07-15 17:46:19 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:57:25 +03:00
|
|
|
async function destroy(callback: BackendCallback) {
|
2023-07-15 17:46:19 +03:00
|
|
|
setError(undefined);
|
2023-07-15 17:57:25 +03:00
|
|
|
deleteRSForm(id, {
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
2023-07-15 17:46:19 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:57:25 +03:00
|
|
|
async function claim(callback: BackendCallback) {
|
2023-07-15 17:46:19 +03:00
|
|
|
setError(undefined);
|
2023-07-15 17:57:25 +03:00
|
|
|
postClaimRSForm(id, {
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
2023-07-15 17:46:19 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-16 22:25:23 +03:00
|
|
|
async function download(callback: BackendCallback) {
|
|
|
|
setError(undefined);
|
|
|
|
getTRSFile(id, {
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-18 14:55:40 +03:00
|
|
|
async function cstUpdate(data: any, callback?: BackendCallback) {
|
|
|
|
setError(undefined);
|
|
|
|
patchConstituenta(String(active!.entityUID), {
|
|
|
|
data: data,
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
return (
|
|
|
|
<RSFormContext.Provider value={{
|
|
|
|
schema, error, loading, processing,
|
|
|
|
active, setActive,
|
|
|
|
isEditable, isClaimable,
|
2023-07-18 14:55:40 +03:00
|
|
|
cstUpdate,
|
2023-07-16 22:25:23 +03:00
|
|
|
reload, update, download, destroy, claim
|
2023-07-15 17:46:19 +03:00
|
|
|
}}>
|
|
|
|
{ children }
|
|
|
|
</RSFormContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useRSForm = () => useContext(RSFormContext);
|