-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2707 from Amsterdam/improvement/dependabot
feat(api): reorganize ApiGroup types and add useContextCache hook and…
- Loading branch information
Showing
4 changed files
with
79 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useCallback, useContext } from "react" | ||
import { ApiContext } from "./ApiProvider" | ||
|
||
type GroupName = "itineraries" | ||
|
||
/* | ||
** Hook for getting and updating items in the Context. | ||
** GroupName and urlKey are defined in the hook where the useApiRequest hook is called. | ||
EXAMPLE: | ||
export const useSuggestions = (itineraryId: number, options?: Options) => { | ||
const handleError = useErrorHandler() | ||
return useApiRequest<{ cases: Case[] }>({ | ||
...options, | ||
url: makeGatewayUrl([ "itineraries", itineraryId, "suggestions" ]), | ||
groupName: "suggestions", | ||
handleError, | ||
isProtected: true | ||
}) | ||
} | ||
CORRECT: | ||
const { getContextItem, updateContextItem } = useContextCache("suggestions", makeGatewayUrl([ "itineraries", itineraryId, "suggestions" ])) | ||
*/ | ||
|
||
const useContextCache = (groupName: GroupName, apiUrl: string) => { | ||
const contextGroup = useContext(ApiContext)[groupName] | ||
const item = contextGroup.getCacheItem(apiUrl)?.value | ||
|
||
const getContextItem = useCallback(() => item, [item]) | ||
const updateContextItem = useCallback((updatedItem: any) => | ||
contextGroup.updateCacheItem(apiUrl, () => updatedItem), [contextGroup, apiUrl] | ||
) | ||
const clearContextCache = useCallback(() => contextGroup.clearCache(), [ contextGroup ]) | ||
|
||
return { getContextItem, updateContextItem, clearContextCache } | ||
} | ||
|
||
export default useContextCache |