Reset leave chat optimistic update if fails (#4058)

zio/stable
Eric Bailey 2024-05-16 15:52:53 -05:00 committed by GitHub
parent 5e5716c0a8
commit 449f16b7c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 1 deletions

View File

@ -31,6 +31,7 @@ export function useLeaveConvo(
return data return data
}, },
onMutate: () => { onMutate: () => {
let prevPages: ChatBskyConvoListConvos.OutputSchema[] = []
queryClient.setQueryData( queryClient.setQueryData(
CONVO_LIST_KEY, CONVO_LIST_KEY,
(old?: { (old?: {
@ -38,6 +39,7 @@ export function useLeaveConvo(
pages: Array<ChatBskyConvoListConvos.OutputSchema> pages: Array<ChatBskyConvoListConvos.OutputSchema>
}) => { }) => {
if (!old) return old if (!old) return old
prevPages = old.pages
return { return {
...old, ...old,
pages: old.pages.map(page => { pages: old.pages.map(page => {
@ -49,13 +51,27 @@ export function useLeaveConvo(
} }
}, },
) )
return {prevPages}
}, },
onSuccess: data => { onSuccess: data => {
queryClient.invalidateQueries({queryKey: CONVO_LIST_KEY}) queryClient.invalidateQueries({queryKey: CONVO_LIST_KEY})
onSuccess?.(data) onSuccess?.(data)
}, },
onError: error => { onError: (error, _, context) => {
logger.error(error) logger.error(error)
queryClient.setQueryData(
CONVO_LIST_KEY,
(old?: {
pageParams: Array<string | undefined>
pages: Array<ChatBskyConvoListConvos.OutputSchema>
}) => {
if (!old) return old
return {
...old,
pages: context?.prevPages || old.pages,
}
},
)
queryClient.invalidateQueries({queryKey: CONVO_LIST_KEY}) queryClient.invalidateQueries({queryKey: CONVO_LIST_KEY})
onError?.(error) onError?.(error)
}, },