mirror of
https://github.com/cpvalente/ontime.git
synced 2026-09-10 08:39:34 +00:00
refract: optimistic delete
This commit is contained in:
@@ -21,7 +21,6 @@ export default function EventListWrapper() {
|
|||||||
eventsNamespace,
|
eventsNamespace,
|
||||||
fetchAllEvents
|
fetchAllEvents
|
||||||
);
|
);
|
||||||
|
|
||||||
const addEvent = useMutation(requestPost, {
|
const addEvent = useMutation(requestPost, {
|
||||||
// we optimistically update here
|
// we optimistically update here
|
||||||
onMutate: async (newEvent) => {
|
onMutate: async (newEvent) => {
|
||||||
@@ -30,12 +29,12 @@ export default function EventListWrapper() {
|
|||||||
|
|
||||||
// Snapshot the previous value
|
// Snapshot the previous value
|
||||||
let previousEvents = queryClient.getQueryData(eventsNamespace);
|
let previousEvents = queryClient.getQueryData(eventsNamespace);
|
||||||
console.log('debug', previousEvents)
|
console.log('debug', previousEvents);
|
||||||
if (previousEvents == null) {
|
if (previousEvents == null) {
|
||||||
refetch();
|
refetch();
|
||||||
previousEvents = queryClient.getQueryData(eventsNamespace);
|
previousEvents = queryClient.getQueryData(eventsNamespace);
|
||||||
}
|
}
|
||||||
console.log('debug 2', previousEvents)
|
console.log('debug 2', previousEvents);
|
||||||
|
|
||||||
// optimistically update object, temp ID until refetch
|
// optimistically update object, temp ID until refetch
|
||||||
let optimistic = [...previousEvents];
|
let optimistic = [...previousEvents];
|
||||||
@@ -125,7 +124,35 @@ export default function EventListWrapper() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const deleteEvent = useMutation(requestDelete);
|
const deleteEvent = useMutation(requestDelete, {
|
||||||
|
// we optimistically update here
|
||||||
|
onMutate: async (eventId) => {
|
||||||
|
// cancel ongoing queries
|
||||||
|
queryClient.cancelQueries([eventsNamespace, eventId]);
|
||||||
|
|
||||||
|
// Snapshot the previous value
|
||||||
|
const previousEvents = queryClient.getQueryData(eventsNamespace);
|
||||||
|
|
||||||
|
let filtered = [...previousEvents]
|
||||||
|
filtered.filter((e) => e.id === 'eventId');
|
||||||
|
|
||||||
|
// optimistically update object
|
||||||
|
queryClient.setQueryData(eventsNamespace, filtered);
|
||||||
|
|
||||||
|
// Return a context with the previous and new todo
|
||||||
|
return { previousEvents };
|
||||||
|
},
|
||||||
|
|
||||||
|
// Mutation fails, rollback undos optimist update
|
||||||
|
onError: (error, newEvent, context) => {
|
||||||
|
queryClient.setQueryData(eventsNamespace, context.previousEvents);
|
||||||
|
},
|
||||||
|
// Mutation finished, failed or successful
|
||||||
|
// Fetch anyway, just to be sure
|
||||||
|
onSettled: (newEvent) => {
|
||||||
|
queryClient.invalidateQueries(eventsNamespace);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
// Show toasts on errors
|
// Show toasts on errors
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user