refactor: improve return of reorder

This commit is contained in:
Carlos Valente
2025-05-16 20:23:35 +02:00
committed by Carlos Valente
parent 1ee773a426
commit 8232e64cc6
6 changed files with 36 additions and 15 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ export type ReorderEntry = {
/**
* HTTP request to reorder an entry
*/
export async function patchReorderEntry(data: ReorderEntry): Promise<AxiosResponse<OntimeEntry>> {
export async function patchReorderEntry(data: ReorderEntry): Promise<AxiosResponse<Rundown>> {
return axios.patch(`${rundownPath}/reorder`, data);
}
@@ -549,6 +549,22 @@ export const useEntryActions = () => {
onError: (_error, _data, context) => {
queryClient.setQueryData<Rundown>(RUNDOWN, context?.previousData);
},
// Mutation finished, we update the rundown with the response
onSuccess: (response) => {
if (response.data) {
const { id, title, order, flatOrder, entries, revision } = response.data;
queryClient.setQueryData<Rundown>(RUNDOWN, {
id,
title,
order,
flatOrder,
entries,
revision,
});
}
},
// Mutation finished, failed or successful
// Fetch anyway, just to be sure
onSettled: () => {
@@ -86,15 +86,15 @@ export async function rundownBatchPut(req: Request, res: Response<MessageRespons
}
}
export async function rundownReorder(req: Request, res: Response<OntimeEntry | ErrorResponse>) {
export async function rundownReorder(req: Request, res: Response<Rundown | ErrorResponse>) {
if (failEmptyObjects(req.body, res)) {
return;
}
try {
const { eventId, from, to } = req.body;
const event = await reorderEntry(eventId, from, to);
res.status(200).send(event.newEvent);
const newRundown = await reorderEntry(eventId, from, to);
res.status(200).send(newRundown);
} catch (error) {
const message = getErrorMessage(error);
res.status(400).send({ message });
@@ -190,17 +190,17 @@ export async function batchEditEvents(ids: string[], data: Partial<OntimeEvent>)
* @param {number} from - index of event from
* @param {number} to - index of event to
*/
export async function reorderEntry(eventId: EntryId, from: number, to: number) {
export async function reorderEntry(eventId: EntryId, from: number, to: number): Promise<Rundown> {
const scopedMutation = cache.mutateCache(cache.reorder);
const reorderedItem = await scopedMutation({ eventId, from, to });
const { changeList, newRundown } = await scopedMutation({ eventId, from, to });
// notify runtime that rundown has changed
updateRuntimeOnChange();
// notify timer and external services of change
notifyChanges({ timer: true, external: true });
notifyChanges({ timer: changeList, external: true });
return reorderedItem;
return newRundown;
}
export async function applyDelay(delayId: EntryId) {
@@ -748,7 +748,7 @@ describe('reorder() mutation', () => {
});
// move first event to the end
const { newRundown } = reorder({
const { newRundown, changeList } = reorder({
rundown: rundown,
eventId: rundown.order[0],
from: 0,
@@ -761,6 +761,7 @@ describe('reorder() mutation', () => {
'3': { id: '3', cue: 'data3', revision: 1 },
'1': { id: '1', cue: 'data1', revision: 1 },
});
expect(changeList).toStrictEqual(['2', '3', '1']);
});
});
@@ -286,6 +286,7 @@ type MutationParams<T> = T & CommonParams;
type MutatingReturn = {
newRundown: Rundown;
newEvent?: OntimeEntry;
changeList?: EntryId[];
didMutate: boolean;
};
type MutatingFn<T extends object> = (params: MutationParams<T>) => MutatingReturn;
@@ -298,11 +299,11 @@ export function mutateCache<T extends object>(mutation: MutatingFn<T>) {
function scopedMutation(params: T) {
// we work on a copy of the rundown
const rundownCopy = structuredClone(currentRundown);
const { newEvent, newRundown, didMutate } = mutation({ ...params, rundown: rundownCopy });
const { newEvent, newRundown, changeList, didMutate } = mutation({ ...params, rundown: rundownCopy });
// early return without calling side effects
if (!didMutate) {
return { newEvent, newRundown, didMutate };
return { newEvent, newRundown, changeList, didMutate };
}
newRundown.revision += 1;
@@ -341,7 +342,7 @@ export function add({ rundown, atIndex, parent, entry }: AddArgs): Required<Muta
}
setIsStale();
return { newRundown: rundown, newEvent: newEntry, didMutate: true };
return { newRundown: rundown, changeList: [], newEvent: newEntry, didMutate: true };
}
type RemoveArgs = MutationParams<{ eventIds: EntryId[] }>;
@@ -440,7 +441,7 @@ export function edit({ rundown, eventId, patch }: EditArgs): Required<MutatingRe
// if nothing changed, nothing to do
if (!hasChanges(entry, patch)) {
return { newRundown: rundown, newEvent: entry, didMutate: false };
return { newRundown: rundown, changeList: [eventId], newEvent: entry, didMutate: false };
}
const newEvent = makeEvent(entry, patch);
@@ -455,7 +456,7 @@ export function edit({ rundown, eventId, patch }: EditArgs): Required<MutatingRe
rundown.entries[newEvent.id] = newEvent;
}
return { newRundown: rundown, newEvent, didMutate: true };
return { newRundown: rundown, changeList: [newEvent.id], newEvent, didMutate: true };
}
type BatchEditArgs = MutationParams<{ eventIds: EntryId[]; patch: Partial<OntimeEntry> }>;
@@ -490,8 +491,11 @@ export function reorder({ rundown, eventId, from, to }: ReorderArgs): Required<M
}
}
// all events from the first one, need to be updated
const changeList = rundown.order.slice(Math.min(from, to), rundown.order.length);
setIsStale();
return { newRundown: rundown, newEvent: eventFrom, didMutate: true };
return { newRundown: rundown, changeList, newEvent: eventFrom, didMutate: true };
}
type ApplyDelayArgs = MutationParams<{ delayId: EntryId }>;