Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 11 additions & 44 deletions src/contexts/UserCollectionsContext.tsx
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -13,13 +13,12 @@ export interface UserCollectionsContextState extends BasePaginatedContextState<C

/**
* This lets us persist the loaded state across multiple instances of the provider
* For initial load only.
*/
const initialPersistentContextState = createDefaultState();
let persistentContext = createDefaultState();

function createDefaultState(): UserCollectionsContextState {
return {
...defaultBaseContext<Collection>(), // Specify Model type
...defaultBaseContext(),
loadAll: true,
order: {
'created_at': 'desc',
Expand All @@ -38,47 +37,15 @@ export interface UserCollectionsContextProviderProps extends BasePaginatedContex
}

export const UserCollectionsContextProvider: React.FC<PropsWithChildren<UserCollectionsContextProviderProps>> = (props => {
const [userCollectionsState, setUserCollectionsState] = useState<UserCollectionsContextState>(initialPersistentContextState);

const wrappedSetUserCollectionsState: Dispatch<SetStateAction<BasePaginatedContextState<Collection>>> =
useCallback((action) => {
setUserCollectionsState(currentUserCollectionsState => {
const baseStateChanges = typeof action === 'function'
? (action as (prevState: BasePaginatedContextState<Collection>) => BasePaginatedContextState<Collection>)(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<Collection>(
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<Collection>(wrappedSetUserCollectionsState, userCollectionsState, `/users/${props.userId}/collections`)
}), [userCollectionsState, wrappedSetUserCollectionsState, props.userId]);
const fullContext = {
...persistentContext,
...createCallbacks(setUserCollectionsState, persistentContext, '/users/' + props.userId + '/collections')
}

return (
<UserCollectionsContext.Provider value={fullContext}>
Expand Down
Loading