2023-08-01 20:14:03 +03:00
|
|
|
import { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
import { ErrorInfo } from '../components/BackendError';
|
2023-08-11 19:28:12 +03:00
|
|
|
import { DataCallback, deleteRSForm, getLibrary, postCloneRSForm, postNewRSForm } from '../utils/backendAPI';
|
|
|
|
import { ILibraryFilter, IRSFormCreateData, IRSFormData, IRSFormMeta, matchRSFormMeta } from '../utils/models';
|
2023-08-11 15:53:18 +03:00
|
|
|
import { useAuth } from './AuthContext';
|
2023-08-01 20:14:03 +03:00
|
|
|
|
|
|
|
interface ILibraryContext {
|
|
|
|
items: IRSFormMeta[]
|
|
|
|
loading: boolean
|
2023-08-09 17:19:12 +03:00
|
|
|
processing: boolean
|
2023-08-01 20:14:03 +03:00
|
|
|
error: ErrorInfo
|
|
|
|
setError: (error: ErrorInfo) => void
|
|
|
|
|
|
|
|
filter: (params: ILibraryFilter) => IRSFormMeta[]
|
2023-08-09 17:19:12 +03:00
|
|
|
createSchema: (data: IRSFormCreateData, callback?: DataCallback<IRSFormMeta>) => void
|
2023-08-22 22:38:27 +03:00
|
|
|
cloneSchema: (target: number, data: IRSFormCreateData, callback: DataCallback<IRSFormData>) => void
|
|
|
|
destroySchema: (target: number, callback?: () => void) => void
|
2023-08-01 20:14:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const LibraryContext = createContext<ILibraryContext | null>(null)
|
|
|
|
export const useLibrary = (): ILibraryContext => {
|
|
|
|
const context = useContext(LibraryContext);
|
2023-08-22 20:29:07 +03:00
|
|
|
if (context === null) {
|
2023-08-01 20:14:03 +03:00
|
|
|
throw new Error(
|
|
|
|
'useLibrary has to be used within <LibraryState.Provider>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface LibraryStateProps {
|
|
|
|
children: React.ReactNode
|
|
|
|
}
|
|
|
|
|
|
|
|
export const LibraryState = ({ children }: LibraryStateProps) => {
|
2023-08-09 17:19:12 +03:00
|
|
|
const [ items, setItems ] = useState<IRSFormMeta[]>([])
|
|
|
|
const [ loading, setLoading ] = useState(false);
|
|
|
|
const [ processing, setProcessing ] = useState(false);
|
|
|
|
const [ error, setError ] = useState<ErrorInfo>(undefined);
|
2023-08-11 15:53:18 +03:00
|
|
|
const { user } = useAuth();
|
2023-08-01 20:14:03 +03:00
|
|
|
|
|
|
|
const filter = useCallback(
|
|
|
|
(params: ILibraryFilter) => {
|
|
|
|
let result = items;
|
|
|
|
if (params.ownedBy) {
|
|
|
|
result = result.filter(schema => schema.owner === params.ownedBy);
|
|
|
|
}
|
|
|
|
if (params.is_common !== undefined) {
|
|
|
|
result = result.filter(schema => schema.is_common === params.is_common);
|
|
|
|
}
|
|
|
|
if (params.queryMeta) {
|
|
|
|
result = result.filter(schema => matchRSFormMeta(params.queryMeta!, schema));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, [items]);
|
|
|
|
|
|
|
|
const reload = useCallback(
|
2023-08-11 15:53:18 +03:00
|
|
|
(callback?: () => void) => {
|
2023-08-01 20:14:03 +03:00
|
|
|
setItems([]);
|
|
|
|
setError(undefined);
|
|
|
|
getLibrary({
|
|
|
|
setLoading: setLoading,
|
|
|
|
showError: true,
|
2023-08-22 22:38:27 +03:00
|
|
|
onError: error => setError(error),
|
|
|
|
onSuccess: newData => {
|
2023-08-11 15:53:18 +03:00
|
|
|
setItems(newData);
|
|
|
|
if (callback) callback();
|
|
|
|
}
|
2023-08-01 20:14:03 +03:00
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
reload();
|
2023-08-11 15:53:18 +03:00
|
|
|
}, [reload, user]);
|
2023-08-09 17:19:12 +03:00
|
|
|
|
|
|
|
const createSchema = useCallback(
|
|
|
|
(data: IRSFormCreateData, callback?: DataCallback<IRSFormMeta>) => {
|
|
|
|
setError(undefined);
|
|
|
|
postNewRSForm({
|
|
|
|
data: data,
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => { setError(error); },
|
|
|
|
onSuccess: newSchema => {
|
|
|
|
reload();
|
|
|
|
if (callback) callback(newSchema);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, [reload]);
|
2023-08-01 20:14:03 +03:00
|
|
|
|
2023-08-11 19:28:12 +03:00
|
|
|
const destroySchema = useCallback(
|
2023-08-22 22:38:27 +03:00
|
|
|
(target: number, callback?: () => void) => {
|
2023-08-11 19:28:12 +03:00
|
|
|
setError(undefined)
|
2023-08-22 22:38:27 +03:00
|
|
|
deleteRSForm(String(target), {
|
2023-08-11 19:28:12 +03:00
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSuccess: () => reload(() => {
|
|
|
|
if (callback) callback();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}, [setError, reload]);
|
|
|
|
|
|
|
|
const cloneSchema = useCallback(
|
2023-08-22 22:38:27 +03:00
|
|
|
(target: number, data: IRSFormCreateData, callback: DataCallback<IRSFormData>) => {
|
2023-08-11 19:28:12 +03:00
|
|
|
if (!user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setError(undefined)
|
2023-08-22 22:38:27 +03:00
|
|
|
postCloneRSForm(String(target), {
|
2023-08-11 19:28:12 +03:00
|
|
|
data: data,
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: error => setError(error),
|
|
|
|
onSuccess: newSchema => reload(() => {
|
|
|
|
if (callback) callback(newSchema);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}, [reload, setError, user]);
|
|
|
|
|
2023-08-01 20:14:03 +03:00
|
|
|
return (
|
|
|
|
<LibraryContext.Provider value={{
|
2023-08-09 17:19:12 +03:00
|
|
|
items, loading, processing, error, setError,
|
2023-08-11 19:28:12 +03:00
|
|
|
filter, createSchema, cloneSchema, destroySchema
|
2023-08-01 20:14:03 +03:00
|
|
|
}}>
|
|
|
|
{ children }
|
|
|
|
</LibraryContext.Provider>
|
|
|
|
);
|
|
|
|
}
|