mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-04 23:18:01 +00:00
b384284175
* chore: migrate and upgrade state dependencies * refactor(eventlist): get data from store * refactor(messagecontrol): get data from store * refactor(playbackcontrol): get data from store * refactor(info): get data from store * refactor(messagecontrol): handle mutations in hook * feat(playbackcontrol): get data from store * refactor(playbackcontrol): handle mutations in hook * refactor(playbackcontrol): simplify object * refactor: simplify interfaces * refactor(InputRow): mixed control + styles * refactor(socket): cleanup unused endpoints * refactor(table): prepare table socket endpoint * refactor(timer): extract timer to own endpoint * refactor(table): get data from store * chore: update tests * refactor(socket): update sockets on cycle * refactor: remove unnecessary safeguards
37 lines
1.2 KiB
JavaScript
37 lines
1.2 KiB
JavaScript
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
import { EVENTS_TABLE } from '../api/apiConstants';
|
|
|
|
/**
|
|
* @description utility hook to handle mutations in events
|
|
* @param mutation
|
|
*/
|
|
export default function useMutateEvents(mutation){
|
|
const queryClient = useQueryClient();
|
|
return useMutation(mutation, {
|
|
onMutate: async (newEvent) => {
|
|
// cancel ongoing queries
|
|
queryClient.cancelQueries(EVENTS_TABLE, { exact: true });
|
|
|
|
// Snapshot the previous value
|
|
const previousEvent = queryClient.getQueryData([EVENTS_TABLE, newEvent.id]);
|
|
|
|
// optimistically update object
|
|
queryClient.setQueryData([EVENTS_TABLE, newEvent.id], newEvent);
|
|
|
|
// Return a context with the previous and new event
|
|
return { previousEvent, newEvent };
|
|
},
|
|
|
|
// Mutation fails, rollback undoes optimist update
|
|
onError: (error, newEvent, context) => {
|
|
queryClient.setQueryData([EVENTS_TABLE, context.newEvent.id], context.previousEvent);
|
|
},
|
|
|
|
// Mutation finished, failed or successful
|
|
// Fetch anyway, just to be sure
|
|
onSettled: (newEvent) => {
|
|
queryClient.invalidateQueries([EVENTS_TABLE, newEvent.id]);
|
|
},
|
|
});
|
|
} |