simplify artifact mutation, remove optimistic update
This commit is contained in:
parent
5bac8a0b53
commit
eaae29f007
1 changed files with 2 additions and 28 deletions
|
|
@ -82,34 +82,8 @@ export function useUpdateCollectionArtifact() {
|
||||||
return createMutation(() => ({
|
return createMutation(() => ({
|
||||||
mutationFn: ({ id, input }: { id: string; input: Partial<CollectionArtifactInput> }) =>
|
mutationFn: ({ id, input }: { id: string; input: Partial<CollectionArtifactInput> }) =>
|
||||||
artifactAdapter.updateCollectionArtifact(id, input),
|
artifactAdapter.updateCollectionArtifact(id, input),
|
||||||
onMutate: async ({ id, input }) => {
|
onSettled: () => {
|
||||||
// Cancel any outgoing refetches
|
// Invalidate collection list to reflect changes
|
||||||
await queryClient.cancelQueries({ queryKey: artifactKeys.collectionArtifact(id) })
|
|
||||||
|
|
||||||
// Snapshot the previous value
|
|
||||||
const previousArtifact = queryClient.getQueryData<CollectionArtifact>(
|
|
||||||
artifactKeys.collectionArtifact(id)
|
|
||||||
)
|
|
||||||
|
|
||||||
// Optimistically update the cache
|
|
||||||
if (previousArtifact) {
|
|
||||||
queryClient.setQueryData(artifactKeys.collectionArtifact(id), {
|
|
||||||
...previousArtifact,
|
|
||||||
...input
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return { previousArtifact }
|
|
||||||
},
|
|
||||||
onError: (_err, { id }, context) => {
|
|
||||||
// Rollback on error
|
|
||||||
if (context?.previousArtifact) {
|
|
||||||
queryClient.setQueryData(artifactKeys.collectionArtifact(id), context.previousArtifact)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onSettled: (_data, _err, { id }) => {
|
|
||||||
// Always refetch after mutation
|
|
||||||
queryClient.invalidateQueries({ queryKey: artifactKeys.collectionArtifact(id) })
|
|
||||||
queryClient.invalidateQueries({ queryKey: artifactKeys.collectionBase })
|
queryClient.invalidateQueries({ queryKey: artifactKeys.collectionBase })
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue