owltide/app/api/events/actions.ts
Hornwitser 7e395c5616 Fix timestamps for create and update action
The browser strips the timezone indicator to use the datetime picker, so
the server needs to add this back again.
2025-02-27 22:20:03 +01:00

70 lines
2.1 KiB
TypeScript

"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: start + "Z",
end: end + "Z",
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: start + "Z",
end: end + "Z",
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");
}