Skip to content

Commit

Permalink
fix(webapp): clear series list cache when series status changed
Browse files Browse the repository at this point in the history
So that when the user navigates to the series list page again, the query
will be re-fetched and will be up-to-date with the latest state.

Related to #71
  • Loading branch information
JoosepAlviste committed Sep 24, 2023
1 parent a98ba4e commit f48b932
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Button, Select, AlertDialog } from '#/components'
import { type FragmentType, graphql, useFragment } from '#/generated/gql'
import { UserSeriesStatus } from '#/generated/gql/graphql'
import { useToast } from '#/hooks'
import { invalidateCacheFields } from '#/lib/apollo'

const STATUS_LABELS = {
[UserSeriesStatus.InProgress]: 'In progress',
Expand Down Expand Up @@ -42,6 +43,9 @@ export const SeriesStatusSelect = ({
}
}
`),
{
update: invalidateCacheFields(['userSeriesList']),
},
)

const [markSeriesEpisodesAsSeenMutate] = useMutation(
Expand Down
21 changes: 21 additions & 0 deletions apps/webapp/src/lib/apollo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type ApolloCache,
ApolloClient,
createHttpLink,
InMemoryCache,
Expand Down Expand Up @@ -43,3 +44,23 @@ export const makeApolloClient = ({

return apolloClient
}

/**
* Invalidate the give cache fields after after a mutation.
*
* Usage example:
* ```ts
* const [mutate] = useMutation(graphql(`...`), {
* update: invalidateCacheFields(['userSeriesList']),
* })
* ```
*/
export const invalidateCacheFields =
(fields: string[]) => (cache: ApolloCache<unknown>) => {
cache.modify({
fields: fields.reduce<Record<string, () => void>>((acc, curr) => {
acc[curr] = () => undefined
return acc
}, {}),
})
}

0 comments on commit f48b932

Please sign in to comment.