Fix LibraryContext update when user changes

This commit is contained in:
IRBorisov 2023-08-11 15:53:18 +03:00
parent 0036e7302d
commit 004664a53e
3 changed files with 22 additions and 10 deletions

View File

@ -58,7 +58,9 @@ export const AuthState = ({ children }: AuthStateProps) => {
showError: true, showError: true,
setLoading: setLoading, setLoading: setLoading,
onError: error => { setError(error); }, onError: error => { setError(error); },
onSuccess: newData => reload(() => { if (callback) callback(newData); }) onSuccess: newData => reload(() => {
if (callback) callback(newData);
})
}); });
} }
@ -66,7 +68,9 @@ export const AuthState = ({ children }: AuthStateProps) => {
setError(undefined); setError(undefined);
postLogout({ postLogout({
showError: true, showError: true,
onSuccess: newData => reload(() => { if (callback) callback(newData); }) onSuccess: newData => reload(() => {
if (callback) callback(newData);
})
}); });
} }

View File

@ -3,6 +3,7 @@ import { createContext, useCallback, useContext, useEffect, useState } from 'rea
import { ErrorInfo } from '../components/BackendError'; import { ErrorInfo } from '../components/BackendError';
import { DataCallback, getLibrary, postNewRSForm } from '../utils/backendAPI'; import { DataCallback, getLibrary, postNewRSForm } from '../utils/backendAPI';
import { ILibraryFilter, IRSFormCreateData, IRSFormMeta, matchRSFormMeta } from '../utils/models'; import { ILibraryFilter, IRSFormCreateData, IRSFormMeta, matchRSFormMeta } from '../utils/models';
import { useAuth } from './AuthContext';
interface ILibraryContext { interface ILibraryContext {
items: IRSFormMeta[] items: IRSFormMeta[]
@ -11,7 +12,7 @@ interface ILibraryContext {
error: ErrorInfo error: ErrorInfo
setError: (error: ErrorInfo) => void setError: (error: ErrorInfo) => void
reload: () => void reload: (callback?: () => void) => void
filter: (params: ILibraryFilter) => IRSFormMeta[] filter: (params: ILibraryFilter) => IRSFormMeta[]
createSchema: (data: IRSFormCreateData, callback?: DataCallback<IRSFormMeta>) => void createSchema: (data: IRSFormCreateData, callback?: DataCallback<IRSFormMeta>) => void
} }
@ -36,6 +37,7 @@ export const LibraryState = ({ children }: LibraryStateProps) => {
const [ loading, setLoading ] = useState(false); const [ loading, setLoading ] = useState(false);
const [ processing, setProcessing ] = useState(false); const [ processing, setProcessing ] = useState(false);
const [ error, setError ] = useState<ErrorInfo>(undefined); const [ error, setError ] = useState<ErrorInfo>(undefined);
const { user } = useAuth();
const filter = useCallback( const filter = useCallback(
(params: ILibraryFilter) => { (params: ILibraryFilter) => {
@ -53,20 +55,23 @@ export const LibraryState = ({ children }: LibraryStateProps) => {
}, [items]); }, [items]);
const reload = useCallback( const reload = useCallback(
() => { (callback?: () => void) => {
setItems([]); setItems([]);
setError(undefined); setError(undefined);
getLibrary({ getLibrary({
setLoading: setLoading, setLoading: setLoading,
showError: true, showError: true,
onError: (error) => setError(error), onError: (error) => setError(error),
onSuccess: newData => { setItems(newData); } onSuccess: newData => {
setItems(newData);
if (callback) callback();
}
}); });
}, []); }, []);
useEffect(() => { useEffect(() => {
reload(); reload();
}, [reload]); }, [reload, user]);
const createSchema = useCallback( const createSchema = useCallback(
(data: IRSFormCreateData, callback?: DataCallback<IRSFormMeta>) => { (data: IRSFormCreateData, callback?: DataCallback<IRSFormMeta>) => {

View File

@ -5,7 +5,7 @@ import { type IUserInfo } from '../utils/models';
interface IUsersContext { interface IUsersContext {
users: IUserInfo[] users: IUserInfo[]
reload: () => void reload: (callback?: () => void) => void
getUserLabel: (userID: number | null) => string getUserLabel: (userID: number | null) => string
} }
@ -47,11 +47,14 @@ export const UsersState = ({ children }: UsersStateProps) => {
} }
const reload = useCallback( const reload = useCallback(
() => { (callback?: () => void) => {
getActiveUsers({ getActiveUsers({
showError: true, showError: true,
onError: () => { setUsers([]); }, onError: () => setUsers([]),
onSuccess: newData => { setUsers(newData); } onSuccess: newData => {
setUsers(newData);
if (callback) callback();
}
}); });
}, [setUsers]); }, [setUsers]);