From 8232e64cc6e69bcc19508b33d7895741311422d0 Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Fri, 16 May 2025 20:23:35 +0200 Subject: [PATCH] refactor: improve return of reorder --- apps/client/src/common/api/rundown.ts | 2 +- apps/client/src/common/hooks/useEntryAction.ts | 16 ++++++++++++++++ .../src/api-data/rundown/rundown.controller.ts | 6 +++--- .../services/rundown-service/RundownService.ts | 8 ++++---- .../__tests__/rundownCache.test.ts | 3 ++- .../src/services/rundown-service/rundownCache.ts | 16 ++++++++++------ 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/apps/client/src/common/api/rundown.ts b/apps/client/src/common/api/rundown.ts index ef9fd8ca3..fe66e8a32 100644 --- a/apps/client/src/common/api/rundown.ts +++ b/apps/client/src/common/api/rundown.ts @@ -64,7 +64,7 @@ export type ReorderEntry = { /** * HTTP request to reorder an entry */ -export async function patchReorderEntry(data: ReorderEntry): Promise> { +export async function patchReorderEntry(data: ReorderEntry): Promise> { return axios.patch(`${rundownPath}/reorder`, data); } diff --git a/apps/client/src/common/hooks/useEntryAction.ts b/apps/client/src/common/hooks/useEntryAction.ts index 7b9a96e97..8b78efc3d 100644 --- a/apps/client/src/common/hooks/useEntryAction.ts +++ b/apps/client/src/common/hooks/useEntryAction.ts @@ -549,6 +549,22 @@ export const useEntryActions = () => { onError: (_error, _data, context) => { queryClient.setQueryData(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, { + id, + title, + order, + flatOrder, + entries, + revision, + }); + } + }, + // Mutation finished, failed or successful // Fetch anyway, just to be sure onSettled: () => { diff --git a/apps/server/src/api-data/rundown/rundown.controller.ts b/apps/server/src/api-data/rundown/rundown.controller.ts index a16e7f755..d484bb0ea 100644 --- a/apps/server/src/api-data/rundown/rundown.controller.ts +++ b/apps/server/src/api-data/rundown/rundown.controller.ts @@ -86,15 +86,15 @@ export async function rundownBatchPut(req: Request, res: Response) { +export async function rundownReorder(req: Request, res: Response) { 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 }); diff --git a/apps/server/src/services/rundown-service/RundownService.ts b/apps/server/src/services/rundown-service/RundownService.ts index bb94b72eb..c8e2f2dbe 100644 --- a/apps/server/src/services/rundown-service/RundownService.ts +++ b/apps/server/src/services/rundown-service/RundownService.ts @@ -190,17 +190,17 @@ export async function batchEditEvents(ids: string[], data: Partial) * @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 { 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) { diff --git a/apps/server/src/services/rundown-service/__tests__/rundownCache.test.ts b/apps/server/src/services/rundown-service/__tests__/rundownCache.test.ts index b22a34859..f53026d54 100644 --- a/apps/server/src/services/rundown-service/__tests__/rundownCache.test.ts +++ b/apps/server/src/services/rundown-service/__tests__/rundownCache.test.ts @@ -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']); }); }); diff --git a/apps/server/src/services/rundown-service/rundownCache.ts b/apps/server/src/services/rundown-service/rundownCache.ts index aca2869b2..0b72b81de 100644 --- a/apps/server/src/services/rundown-service/rundownCache.ts +++ b/apps/server/src/services/rundown-service/rundownCache.ts @@ -286,6 +286,7 @@ type MutationParams = T & CommonParams; type MutatingReturn = { newRundown: Rundown; newEvent?: OntimeEntry; + changeList?: EntryId[]; didMutate: boolean; }; type MutatingFn = (params: MutationParams) => MutatingReturn; @@ -298,11 +299,11 @@ export function mutateCache(mutation: MutatingFn) { 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; @@ -440,7 +441,7 @@ export function edit({ rundown, eventId, patch }: EditArgs): Required }>; @@ -490,8 +491,11 @@ export function reorder({ rundown, eventId, from, to }: ReorderArgs): Required;