Skip to content

Commit

Permalink
Reset leave chat optimistic update if fails (#4058)
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey authored May 16, 2024
1 parent 5e5716c commit 449f16b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/state/queries/messages/leave-conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ export function useLeaveConvo(
return data
},
onMutate: () => {
let prevPages: ChatBskyConvoListConvos.OutputSchema[] = []
queryClient.setQueryData(
CONVO_LIST_KEY,
(old?: {
pageParams: Array<string | undefined>
pages: Array<ChatBskyConvoListConvos.OutputSchema>
}) => {
if (!old) return old
prevPages = old.pages
return {
...old,
pages: old.pages.map(page => {
Expand All @@ -49,13 +51,27 @@ export function useLeaveConvo(
}
},
)
return {prevPages}
},
onSuccess: data => {
queryClient.invalidateQueries({queryKey: CONVO_LIST_KEY})
onSuccess?.(data)
},
onError: error => {
onError: (error, _, context) => {
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})
onError?.(error)
},
Expand Down

0 comments on commit 449f16b

Please sign in to comment.