Skip to content

Commit

Permalink
Merge pull request #2707 from Amsterdam/improvement/dependabot
Browse files Browse the repository at this point in the history
feat(api): reorganize ApiGroup types and add useContextCache hook and…
  • Loading branch information
remyvdwereld authored Nov 18, 2024
2 parents 2af5bd1 + 2af7787 commit f9fc066
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ updates:
schedule:
interval: "daily"
open-pull-requests-limit: 100
ignore:
- dependency-name: "*" # This will ignore all dependencies
update-types: ["version-update:semver-patch"]
38 changes: 19 additions & 19 deletions src/app/state/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import currentDate from "../../features/shared/utils/currentDate"
import { Case, Itinerary, ItineraryItem, District } from "app/features/types"

export type ApiGroup =
| "auth"
| "case"
| "daySettings"
| "decos"
| "itineraries"
| "users"
| "meldingen"
| "permits"
| "postCodeRangesPresets"
| "settings"
| "daySettings"
| "teamSettings"
| "themes"
| "postCodeRangesPresets"
| "case"
| "permits"
| "decos"
| "auth"
| "meldingen"
| "users"

export type Options = {
keepUsingInvalidCache?: boolean
Expand Down Expand Up @@ -64,6 +64,17 @@ export const useItineraryItem = (id: number | string, options?: Options) => {
})
}

export const useSuggestions = (itineraryId: number, options?: Options) => {
const handleError = useErrorHandler()
return useApiRequest<{ cases: Case[] }>({
...options,
url: makeGatewayUrl([ "itineraries", itineraryId, "suggestions" ]),
groupName: "itineraries",
handleError,
isProtected: true
})
}

export const useCase = (id: number | string, options?: Options) => {
const handleError = useErrorHandler()
return useApiRequest<Case>({
Expand Down Expand Up @@ -116,17 +127,6 @@ export const useSearch = (streetNumber: number, postalCode?: string, streetName?
})
}

export const useSuggestions = (itineraryId: number, options?: Options) => {
const handleError = useErrorHandler()
return useApiRequest<{ cases: Case[] }>({
...options,
url: makeGatewayUrl([ "itineraries", itineraryId, "suggestions" ]),
groupName: "itineraries",
handleError,
isProtected: true
})
}

export const useUsers = (options?: Options) => {
const handleError = useErrorHandler()
return useApiRequest<{ results: Components.Schemas.User[] }>({
Expand Down
36 changes: 18 additions & 18 deletions src/app/state/rest/provider/ApiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,67 @@ import { ApiGroup } from "../index"
type GroupedContext = Record<ApiGroup, ApiCache & RequestQueue>

export const ApiContext = React.createContext<GroupedContext>({
itineraries: noopContext,
auth: noopContext,
case: noopContext,
users: noopContext,
settings: noopContext,
permits: noopContext,
decos: noopContext,
daySettings: noopContext,
decos: noopContext,
itineraries: noopContext,
meldingen: noopContext,
permits: noopContext,
postCodeRangesPresets: noopContext,
settings: noopContext,
teamSettings: noopContext,
themes: noopContext,
postCodeRangesPresets: noopContext,
auth: noopContext,
meldingen: noopContext
users: noopContext
})

const ApiProvider: React.FC = ({ children }) => {
const value: GroupedContext = {
itineraries: {
auth: {
...useApiCache(),
...useRequestQueue()
},
case: {
...useApiCache(),
...useRequestQueue()
},
users: {
daySettings: {
...useApiCache(),
...useRequestQueue()
},
settings: {
decos: {
...useApiCache(),
...useRequestQueue()
},
daySettings: {
itineraries: {
...useApiCache(),
...useRequestQueue()
},
teamSettings: {
meldingen: {
...useApiCache(),
...useRequestQueue()
},
themes: {
permits: {
...useApiCache(),
...useRequestQueue()
},
postCodeRangesPresets: {
...useApiCache(),
...useRequestQueue()
},
permits: {
settings: {
...useApiCache(),
...useRequestQueue()
},
decos: {
teamSettings: {
...useApiCache(),
...useRequestQueue()
},
meldingen: {
themes: {
...useApiCache(),
...useRequestQueue()
},
auth: {
users: {
...useApiCache(),
...useRequestQueue()
}
Expand Down
39 changes: 39 additions & 0 deletions src/app/state/rest/provider/useContextCache.ts
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

0 comments on commit f9fc066

Please sign in to comment.