ConceptPortal-public/rsconcept/frontend/src/context/LibraryContext.tsx

133 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-08-01 20:14:03 +03:00
import { createContext, useCallback, useContext, useEffect, useState } from 'react';
import { ErrorInfo } from '../components/BackendError';
2023-08-25 22:51:20 +03:00
import { DataCallback, deleteLibraryItem, getLibrary, postCloneLibraryItem, postNewRSForm } from '../utils/backendAPI';
import { ILibraryFilter, ILibraryItem, IRSFormCreateData, IRSFormData, matchLibraryItem } from '../utils/models';
import { useAuth } from './AuthContext';
2023-08-01 20:14:03 +03:00
interface ILibraryContext {
2023-08-25 22:51:20 +03:00
items: ILibraryItem[]
2023-08-01 20:14:03 +03:00
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
2023-08-25 22:51:20 +03:00
filter: (params: ILibraryFilter) => ILibraryItem[]
createSchema: (data: IRSFormCreateData, callback?: DataCallback<ILibraryItem>) => void
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);
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-25 22:51:20 +03:00
const [ items, setItems ] = useState<ILibraryItem[]>([])
2023-08-09 17:19:12 +03:00
const [ loading, setLoading ] = useState(false);
const [ processing, setProcessing ] = useState(false);
const [ error, setError ] = useState<ErrorInfo>(undefined);
const { user } = useAuth();
2023-08-01 20:14:03 +03:00
const filter = useCallback(
(params: ILibraryFilter) => {
let result = items;
if (params.ownedBy) {
2023-08-26 17:26:49 +03:00
result = result.filter(item =>
item.owner === params.ownedBy
|| user?.subscriptions.includes(item.id));
2023-08-01 20:14:03 +03:00
}
if (params.is_common !== undefined) {
2023-08-25 22:51:20 +03:00
result = result.filter(item => item.is_common === params.is_common);
2023-08-01 20:14:03 +03:00
}
if (params.queryMeta) {
2023-08-25 22:51:20 +03:00
result = result.filter(item => matchLibraryItem(params.queryMeta!, item));
2023-08-01 20:14:03 +03:00
}
return result;
2023-08-26 17:26:49 +03:00
}, [items, user]);
2023-08-01 20:14:03 +03:00
const reload = useCallback(
(callback?: () => void) => {
2023-08-01 20:14:03 +03:00
setItems([]);
setError(undefined);
getLibrary({
setLoading: setLoading,
showError: true,
onError: error => setError(error),
onSuccess: newData => {
setItems(newData);
if (callback) callback();
}
2023-08-01 20:14:03 +03:00
});
}, []);
useEffect(() => {
reload();
}, [reload, user]);
2023-08-09 17:19:12 +03:00
const createSchema = useCallback(
2023-08-25 22:51:20 +03:00
(data: IRSFormCreateData, callback?: DataCallback<ILibraryItem>) => {
2023-08-09 17:19:12 +03:00
setError(undefined);
postNewRSForm({
data: data,
showError: true,
setLoading: setProcessing,
2023-08-25 22:51:20 +03:00
onError: error => setError(error),
2023-08-09 17:19:12 +03:00
onSuccess: newSchema => {
reload();
if (callback) callback(newSchema);
}
});
}, [reload]);
2023-08-01 20:14:03 +03:00
const destroySchema = useCallback(
(target: number, callback?: () => void) => {
setError(undefined)
2023-08-25 22:51:20 +03:00
deleteLibraryItem(String(target), {
showError: true,
setLoading: setProcessing,
onError: error => setError(error),
onSuccess: () => reload(() => {
if (callback) callback();
})
});
}, [setError, reload]);
const cloneSchema = useCallback(
(target: number, data: IRSFormCreateData, callback: DataCallback<IRSFormData>) => {
if (!user) {
return;
}
setError(undefined)
2023-08-25 22:51:20 +03:00
postCloneLibraryItem(String(target), {
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,
filter, createSchema, cloneSchema, destroySchema
2023-08-01 20:14:03 +03:00
}}>
{ children }
</LibraryContext.Provider>
);
}