Add PATCH /api/schedule endpoint for editing the schedule in a manner that's access controlled.
21 lines
658 B
TypeScript
21 lines
658 B
TypeScript
import type { ChangeRecord } from "~/shared/types/schedule";
|
|
|
|
export function applyChange<T extends { id: string }>(change: ChangeRecord<T>, data: T[]) {
|
|
const index = data.findIndex(item => item.id === change.data.id);
|
|
if (change.op === "del") {
|
|
if (index !== -1)
|
|
data.splice(index, 1);
|
|
} else if (change.op === "set") {
|
|
if (index !== -1)
|
|
data.splice(index, 1, change.data);
|
|
else
|
|
data.push(change.data)
|
|
}
|
|
}
|
|
|
|
export function applyChangeArray<T extends { id: string }>(changes: ChangeRecord<T>[], data: T[]) {
|
|
// Note: quadratic complexity due to findIndex in applyChange
|
|
for (const change of changes) {
|
|
applyChange(change, data);
|
|
}
|
|
}
|