Created
February 18, 2020 05:56
-
-
Save seandearnaley/330c2c77c9f62cc0531ea4dfc2b23bd3 to your computer and use it in GitHub Desktop.
old version of useRemoveCategory
This file contains hidden or 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
import { ApolloError, FetchResult } from '@apollo/client'; | |
import { | |
GetCategoriesDocument, | |
useRemoveCategoryMutation, | |
CategoryPartsFragment, | |
RemoveCategoryMutation, | |
} from '../../generated/graphql'; | |
// NOTE: the rationale for using a custom hook is for the cache update, | |
// now the removeCategory function can be used elsewhere with shared cache logic | |
export const useRemoveCategory = (): [ | |
( | |
id: string | null, | |
) => Promise< | |
FetchResult< | |
RemoveCategoryMutation, | |
Record<string, any>, | |
Record<string, any> | |
> | |
>, | |
boolean, | |
ApolloError | undefined, | |
] => { | |
const [ | |
removeCategoryMutation, | |
{ loading, error }, | |
] = useRemoveCategoryMutation(); | |
const removeCategory = (id: string | null) => | |
removeCategoryMutation({ | |
variables: { | |
id: id ?? '', | |
}, | |
update: (cache, { data }) => { | |
const id = data?.removeCategory?.category?.id; | |
if (!id) return; | |
const { categories } = | |
cache.readQuery({ | |
query: GetCategoriesDocument, | |
}) || {}; | |
cache.writeQuery({ | |
query: GetCategoriesDocument, | |
data: { | |
categories: categories.filter( | |
(category: CategoryPartsFragment) => category.id !== id, | |
), | |
}, | |
}); | |
}, | |
}); | |
return [removeCategory, loading, error]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment