2023-07-20 17:11:03 +03:00
|
|
|
import { createContext, useState, useContext, useMemo, useCallback } from 'react';
|
|
|
|
import { IConstituenta, IRSForm } from '../utils/models';
|
2023-07-15 17:46:19 +03:00
|
|
|
import { useRSFormDetails } from '../hooks/useRSFormDetails';
|
|
|
|
import { ErrorInfo } from '../components/BackendError';
|
|
|
|
import { useAuth } from './AuthContext';
|
2023-07-23 21:38:04 +03:00
|
|
|
import { BackendCallback, deleteRSForm, getTRSFile, patchConstituenta, patchRSForm, postClaimRSForm, postDeleteConstituenta, postNewConstituenta } from '../utils/backendAPI';
|
2023-07-20 17:11:03 +03:00
|
|
|
import { toast } from 'react-toastify';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
|
|
interface IRSFormContext {
|
|
|
|
schema?: IRSForm
|
|
|
|
active?: IConstituenta
|
|
|
|
error: ErrorInfo
|
|
|
|
loading: boolean
|
|
|
|
processing: boolean
|
2023-07-21 00:09:05 +03:00
|
|
|
isOwned: boolean
|
2023-07-15 17:46:19 +03:00
|
|
|
isEditable: boolean
|
|
|
|
isClaimable: boolean
|
2023-07-20 17:11:03 +03:00
|
|
|
forceAdmin: boolean
|
|
|
|
readonly: boolean
|
|
|
|
isTracking: boolean
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-07-21 00:09:05 +03:00
|
|
|
setActive: React.Dispatch<React.SetStateAction<IConstituenta | undefined>>
|
|
|
|
toggleForceAdmin: () => void
|
|
|
|
toggleReadonly: () => void
|
2023-07-20 17:11:03 +03:00
|
|
|
toggleTracking: () => void
|
2023-07-23 21:38:04 +03:00
|
|
|
|
|
|
|
reload: () => Promise<void>
|
|
|
|
update: (data: any, callback?: BackendCallback) => Promise<void>
|
|
|
|
destroy: (callback?: BackendCallback) => Promise<void>
|
|
|
|
claim: (callback?: BackendCallback) => Promise<void>
|
|
|
|
download: (callback: BackendCallback) => Promise<void>
|
|
|
|
|
|
|
|
cstUpdate: (data: any, callback?: BackendCallback) => Promise<void>
|
|
|
|
cstCreate: (data: any, callback?: BackendCallback) => Promise<void>
|
|
|
|
cstDelete: (data: any, callback?: BackendCallback) => Promise<void>
|
2023-07-15 17:46:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export const RSFormContext = createContext<IRSFormContext>({
|
|
|
|
schema: undefined,
|
|
|
|
active: undefined,
|
|
|
|
error: undefined,
|
|
|
|
loading: false,
|
|
|
|
processing: false,
|
2023-07-21 00:09:05 +03:00
|
|
|
isOwned: false,
|
2023-07-15 17:46:19 +03:00
|
|
|
isEditable: false,
|
|
|
|
isClaimable: false,
|
2023-07-20 17:11:03 +03:00
|
|
|
forceAdmin: false,
|
|
|
|
readonly: false,
|
|
|
|
isTracking: true,
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
|
|
setActive: () => {},
|
2023-07-21 00:09:05 +03:00
|
|
|
toggleForceAdmin: () => {},
|
|
|
|
toggleReadonly: () => {},
|
2023-07-20 17:11:03 +03:00
|
|
|
toggleTracking: () => {},
|
2023-07-23 21:38:04 +03:00
|
|
|
|
|
|
|
reload: async () => {},
|
|
|
|
update: async () => {},
|
|
|
|
destroy: async () => {},
|
|
|
|
claim: async () => {},
|
|
|
|
download: async () => {},
|
|
|
|
|
|
|
|
cstUpdate: async () => {},
|
|
|
|
cstCreate: async () => {},
|
|
|
|
cstDelete: async () => {},
|
2023-07-15 17:46:19 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
interface RSFormStateProps {
|
2023-07-23 15:23:01 +03:00
|
|
|
schemaID: string
|
2023-07-15 17:46:19 +03:00
|
|
|
children: React.ReactNode
|
|
|
|
}
|
|
|
|
|
2023-07-23 15:23:01 +03:00
|
|
|
export const RSFormState = ({ schemaID, children }: RSFormStateProps) => {
|
2023-07-15 17:46:19 +03:00
|
|
|
const { user } = useAuth();
|
2023-07-23 15:23:01 +03:00
|
|
|
const { schema, reload, error, setError, loading } = useRSFormDetails({target: schemaID});
|
2023-07-15 17:46:19 +03:00
|
|
|
const [processing, setProcessing] = useState(false)
|
|
|
|
const [active, setActive] = useState<IConstituenta | undefined>(undefined);
|
|
|
|
|
2023-07-20 17:11:03 +03:00
|
|
|
const [forceAdmin, setForceAdmin] = useState(false);
|
|
|
|
const [readonly, setReadonly] = useState(false);
|
|
|
|
|
2023-07-21 00:09:05 +03:00
|
|
|
const isOwned = useMemo(() => user?.id === schema?.owner || false, [user, schema]);
|
|
|
|
const isClaimable = useMemo(() => (user?.id !== schema?.owner || false), [user, schema]);
|
2023-07-23 21:38:04 +03:00
|
|
|
const isEditable = useMemo(
|
|
|
|
() => {
|
2023-07-20 17:11:03 +03:00
|
|
|
return (
|
2023-07-23 21:38:04 +03:00
|
|
|
!loading && !readonly &&
|
2023-07-21 00:09:05 +03:00
|
|
|
(isOwned || (forceAdmin && user?.is_staff) || false)
|
2023-07-20 17:11:03 +03:00
|
|
|
)
|
2023-07-23 21:38:04 +03:00
|
|
|
}, [user, readonly, forceAdmin, isOwned, loading]);
|
2023-07-20 17:11:03 +03:00
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
const isTracking = useMemo(
|
|
|
|
() => {
|
2023-07-20 17:11:03 +03:00
|
|
|
return true;
|
|
|
|
}, []);
|
2023-07-21 00:09:05 +03:00
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
const toggleTracking = useCallback(
|
|
|
|
() => {
|
2023-07-20 17:11:03 +03:00
|
|
|
toast('not implemented yet');
|
|
|
|
}, []);
|
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
const update = useCallback(
|
|
|
|
async (data: any, callback?: BackendCallback) => {
|
2023-07-15 17:46:19 +03:00
|
|
|
setError(undefined);
|
2023-07-23 15:23:01 +03:00
|
|
|
patchRSForm(schemaID, {
|
2023-07-15 17:57:25 +03:00
|
|
|
data: data,
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
2023-07-15 17:46:19 +03:00
|
|
|
});
|
2023-07-23 21:38:04 +03:00
|
|
|
}, [schemaID, setError]);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
const destroy = useCallback(
|
|
|
|
async (callback?: BackendCallback) => {
|
2023-07-15 17:46:19 +03:00
|
|
|
setError(undefined);
|
2023-07-23 15:23:01 +03:00
|
|
|
deleteRSForm(schemaID, {
|
2023-07-15 17:57:25 +03:00
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
2023-07-15 17:46:19 +03:00
|
|
|
});
|
2023-07-23 21:38:04 +03:00
|
|
|
}, [schemaID, setError]);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
const claim = useCallback(
|
|
|
|
async (callback?: BackendCallback) => {
|
2023-07-15 17:46:19 +03:00
|
|
|
setError(undefined);
|
2023-07-23 15:23:01 +03:00
|
|
|
postClaimRSForm(schemaID, {
|
2023-07-15 17:57:25 +03:00
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
2023-07-15 17:46:19 +03:00
|
|
|
});
|
2023-07-23 21:38:04 +03:00
|
|
|
}, [schemaID, setError]);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
const download = useCallback(
|
|
|
|
async (callback: BackendCallback) => {
|
2023-07-16 22:25:23 +03:00
|
|
|
setError(undefined);
|
2023-07-23 15:23:01 +03:00
|
|
|
getTRSFile(schemaID, {
|
2023-07-16 22:25:23 +03:00
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
|
|
|
});
|
2023-07-23 21:38:04 +03:00
|
|
|
}, [schemaID, setError]);
|
2023-07-16 22:25:23 +03:00
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
const cstUpdate = useCallback(
|
|
|
|
async (data: any, callback?: BackendCallback) => {
|
2023-07-18 14:55:40 +03:00
|
|
|
setError(undefined);
|
|
|
|
patchConstituenta(String(active!.entityUID), {
|
|
|
|
data: data,
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
|
|
|
});
|
2023-07-23 21:38:04 +03:00
|
|
|
}, [active, setError]);
|
2023-07-18 14:55:40 +03:00
|
|
|
|
2023-07-23 21:38:04 +03:00
|
|
|
const cstCreate = useCallback(
|
|
|
|
async (data: any, callback?: BackendCallback) => {
|
2023-07-23 15:23:01 +03:00
|
|
|
setError(undefined);
|
|
|
|
postNewConstituenta(schemaID, {
|
|
|
|
data: data,
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
|
|
|
});
|
2023-07-23 21:38:04 +03:00
|
|
|
}, [schemaID, setError]);
|
|
|
|
|
|
|
|
const cstDelete = useCallback(
|
|
|
|
async (data: any, callback?: BackendCallback) => {
|
|
|
|
setError(undefined);
|
|
|
|
postDeleteConstituenta(schemaID, {
|
|
|
|
data: data,
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSucccess: callback
|
|
|
|
});
|
|
|
|
}, [schemaID, setError]);
|
2023-07-23 15:23:01 +03:00
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
return (
|
|
|
|
<RSFormContext.Provider value={{
|
|
|
|
schema, error, loading, processing,
|
|
|
|
active, setActive,
|
2023-07-21 00:09:05 +03:00
|
|
|
forceAdmin, readonly,
|
|
|
|
toggleForceAdmin: () => setForceAdmin(prev => !prev),
|
|
|
|
toggleReadonly: () => setReadonly(prev => !prev),
|
|
|
|
isOwned, isEditable, isClaimable,
|
2023-07-20 17:11:03 +03:00
|
|
|
isTracking, toggleTracking,
|
2023-07-23 15:23:01 +03:00
|
|
|
reload, update, download, destroy, claim,
|
2023-07-23 21:38:04 +03:00
|
|
|
cstUpdate, cstCreate, cstDelete,
|
2023-07-15 17:46:19 +03:00
|
|
|
}}>
|
|
|
|
{ children }
|
|
|
|
</RSFormContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useRSForm = () => useContext(RSFormContext);
|