diff --git a/src/contexts/UserCollectionsContext.tsx b/src/contexts/UserCollectionsContext.tsx index eb88a1a..5ef4629 100644 --- a/src/contexts/UserCollectionsContext.tsx +++ b/src/contexts/UserCollectionsContext.tsx @@ -1,9 +1,9 @@ import { BasePaginatedContextProviderProps, BasePaginatedContextState, createCallbacks, - defaultBaseContext, + defaultBaseContext, prepareContextState, } from './BasePaginatedContext'; -import React, {PropsWithChildren, useCallback, useEffect, useMemo, useState, Dispatch, SetStateAction} from 'react'; +import React, {PropsWithChildren, useEffect, useState} from 'react'; import Collection from "../models/user/collection"; /** @@ -13,13 +13,12 @@ export interface UserCollectionsContextState extends BasePaginatedContextState(), // Specify Model type + ...defaultBaseContext(), loadAll: true, order: { 'created_at': 'desc', @@ -38,47 +37,15 @@ export interface UserCollectionsContextProviderProps extends BasePaginatedContex } export const UserCollectionsContextProvider: React.FC> = (props => { - const [userCollectionsState, setUserCollectionsState] = useState(initialPersistentContextState); - - const wrappedSetUserCollectionsState: Dispatch>> = - useCallback((action) => { - setUserCollectionsState(currentUserCollectionsState => { - const baseStateChanges = typeof action === 'function' - ? (action as (prevState: BasePaginatedContextState) => BasePaginatedContextState)(currentUserCollectionsState) - : action; - return { ...currentUserCollectionsState, ...baseStateChanges } as UserCollectionsContextState; - }); - }, [setUserCollectionsState]); - + const [userCollectionsState, setUserCollectionsState] = useState(persistentContext); useEffect(() => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - setUserCollectionsState(_prevState => { - let newState = createDefaultState(); - - newState = createCallbacks( - wrappedSetUserCollectionsState, - newState, - `/users/${props.userId}/collections` - ); - - newState.refreshData(false).catch(console.error); + prepareContextState(setUserCollectionsState, userCollectionsState, '/users/' + props.userId + '/collections') + }, [props.userId, userCollectionsState]); - return { - ...newState, - loadedData: [], - lastLoadedPage: undefined, - noResults: false, - refreshing: true, - initialLoadComplete: false, - initiated: true, - }; - }); - }, [props.userId, setUserCollectionsState, wrappedSetUserCollectionsState]); - - const fullContext = useMemo(() => ({ - ...userCollectionsState, - ...createCallbacks(wrappedSetUserCollectionsState, userCollectionsState, `/users/${props.userId}/collections`) - }), [userCollectionsState, wrappedSetUserCollectionsState, props.userId]); + const fullContext = { + ...persistentContext, + ...createCallbacks(setUserCollectionsState, persistentContext, '/users/' + props.userId + '/collections') + } return (