Implement editing of the schedule
Cobbled together minimum viable system for editing the schedule in a way that propogates updates to connected clients.
This commit is contained in:
parent
cdad188233
commit
093a6816bc
7 changed files with 236 additions and 49 deletions
70
app/api/events/actions.ts
Normal file
70
app/api/events/actions.ts
Normal file
|
@ -0,0 +1,70 @@
|
|||
"use server";
|
||||
import { Schedule } from "@/app/schedule/types";
|
||||
import { readFile, writeFile } from "fs/promises";
|
||||
import { broadcastUpdate } from "./streams";
|
||||
|
||||
export async function createEvent(formData: FormData) {
|
||||
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8"));
|
||||
const id = formData.get("id") as string;
|
||||
const name = formData.get("name") as string;
|
||||
const description = formData.get("description") as string;
|
||||
const start = formData.get("start") as string;
|
||||
const end = formData.get("end") as string;
|
||||
const location = formData.get("location") as string;
|
||||
schedule.events.push({
|
||||
name,
|
||||
id,
|
||||
description,
|
||||
slots: [
|
||||
{
|
||||
id: `${id}-1`,
|
||||
start,
|
||||
end,
|
||||
locations: [location],
|
||||
}
|
||||
]
|
||||
});
|
||||
broadcastUpdate(schedule);
|
||||
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
|
||||
}
|
||||
|
||||
export async function modifyEvent(formData: FormData) {
|
||||
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8"));
|
||||
const id = formData.get("id") as string;
|
||||
const name = formData.get("name") as string;
|
||||
const description = formData.get("description") as string;
|
||||
const start = formData.get("start") as string;
|
||||
const end = formData.get("end") as string;
|
||||
const location = formData.get("location") as string;
|
||||
const index = schedule.events.findIndex(event => event.id === id);
|
||||
if (index === -1) {
|
||||
throw Error("No such event");
|
||||
}
|
||||
schedule.events[index] = {
|
||||
name,
|
||||
id,
|
||||
description,
|
||||
slots: [
|
||||
{
|
||||
id: `${id}-1`,
|
||||
start,
|
||||
end,
|
||||
locations: [location],
|
||||
}
|
||||
]
|
||||
};
|
||||
broadcastUpdate(schedule);
|
||||
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
|
||||
}
|
||||
|
||||
export async function deleteEvent(formData: FormData) {
|
||||
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8"));
|
||||
const id = formData.get("id") as string;
|
||||
const index = schedule.events.findIndex(event => event.id === id);
|
||||
if (index === -1) {
|
||||
throw Error("No such event");
|
||||
}
|
||||
schedule.events.splice(index, 1);
|
||||
broadcastUpdate(schedule);
|
||||
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue