diff --git a/api/src/index.js b/api/src/index.js index 5f5cbac..756c157 100644 --- a/api/src/index.js +++ b/api/src/index.js @@ -9,7 +9,7 @@ app.use(morgan('dev')) app.use(cors({ origin: ['https://aj17thdev.nexuszone.net', 'http://localhost:5173'], // your SPA origins - credentials: true + credentials: true, })); app.use(express.json()) diff --git a/api/src/routes/calendar.ts b/api/src/routes/calendar.ts index 61762cc..ff486cb 100644 --- a/api/src/routes/calendar.ts +++ b/api/src/routes/calendar.ts @@ -1,5 +1,5 @@ import { Request, Response } from "express"; -import { createEvent, getEventAttendance, getEventDetails, getShortEventsInRange, setAttendanceStatus, setEventCancelled } from "../services/calendarService"; +import { createEvent, getEventAttendance, getEventDetails, getShortEventsInRange, setAttendanceStatus, setEventCancelled, updateEvent } from "../services/calendarService"; import { CalendarAttendance, CalendarEvent } from "@app/shared/types/calendar"; const express = require('express'); @@ -92,7 +92,6 @@ r.post('/', async (req: Request, res: Response) => { event.creator_id = member; event.start = new Date(event.start); event.end = new Date(event.end); - console.log(event); createEvent(event); res.sendStatus(200); } catch (error) { @@ -101,5 +100,19 @@ r.post('/', async (req: Request, res: Response) => { } }) +r.put('/', async (req: Request, res: Response) => { + try { + let event: CalendarEvent = req.body; + event.start = new Date(event.start); + event.end = new Date(event.end); + console.log(event); + updateEvent(event); + res.sendStatus(200); + } catch (error) { + console.error('Failed to update event:', error); + res.status(500).json(error); + } +}) + module.exports.calendarRouter = r; \ No newline at end of file diff --git a/api/src/services/calendarService.ts b/api/src/services/calendarService.ts index 657bcbf..c88f671 100644 --- a/api/src/services/calendarService.ts +++ b/api/src/services/calendarService.ts @@ -34,7 +34,7 @@ export async function updateEvent(eventObject: CalendarEvent) { end = ?, location = ?, color = ?, - description = ?, + description = ? WHERE id = ? `; @@ -54,7 +54,6 @@ export async function updateEvent(eventObject: CalendarEvent) { export async function setEventCancelled(eventID: number, cancelled: boolean) { const input = cancelled ? 1 : 0; - console.log(cancelled, input); const sql = ` UPDATE calendar_events SET cancelled = ? @@ -73,7 +72,6 @@ export async function getShortEventsInRange(startDate: string, endDate: string): ORDER BY start ASC `; const res: CalendarEventShort[] = await pool.query(sql, [startDate, endDate]); - console.log(res); return res; } diff --git a/ui/src/api/calendar.ts b/ui/src/api/calendar.ts index 6c10c2b..91fa66f 100644 --- a/ui/src/api/calendar.ts +++ b/ui/src/api/calendar.ts @@ -83,7 +83,18 @@ export async function createCalendarEvent(eventData: CalendarEvent) { } export async function editCalendarEvent(eventData: CalendarEvent) { + let res = await fetch(`${addr}/calendar`, { + method: "PUT", + credentials: "include", + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(eventData) + }); + if (res.ok) { + return; + } else { + throw new Error(`Failed to set attendance: ${res.status} ${res.statusText}`); + } } export async function setCancelCalendarEvent(eventID: number, cancel: boolean) { diff --git a/ui/src/components/calendar/CreateCalendarEvent.vue b/ui/src/components/calendar/CreateCalendarEvent.vue index d7229e4..cf99546 100644 --- a/ui/src/components/calendar/CreateCalendarEvent.vue +++ b/ui/src/components/calendar/CreateCalendarEvent.vue @@ -45,13 +45,18 @@ function roundToNextHour(d = new Date()) { } import { calendarEventSchema, parseLocalDateTime } from '@shared/schemas/calendarEventSchema' -import { createCalendarEvent } from "@/api/calendar" +import { createCalendarEvent, editCalendarEvent } from "@/api/calendar" +import DialogDescription from "../ui/dialog/DialogDescription.vue" const formSchema = toTypedSchema(calendarEventSchema) // ---------- dialog state & defaults ---------- const clickedDate = ref(null); const dialogOpen = ref(false) -function openDialog(dateStr?: string) { +const dialogMode = ref<'create' | 'edit'>('create'); +const editEvent = ref(); +function openDialog(dateStr?: string, mode?: 'create' | 'edit', event?: CalendarEvent) { + dialogMode.value = mode ?? 'create'; + editEvent.value = event ?? null; clickedDate.value = dateStr ?? null; dialogOpen.value = true initialValues.value = makeInitialValues() @@ -59,6 +64,22 @@ function openDialog(dateStr?: string) { defineExpose({ openDialog }) function makeInitialValues() { + + if (dialogMode.value === 'edit' && editEvent.value) { + const e = editEvent.value; + return { + name: e.name, + startDate: toLocalDateString(new Date(e.start)), + startTime: toLocalTimeString(new Date(e.start)), + endDate: toLocalDateString(new Date(e.end)), + endTime: toLocalTimeString(new Date(e.end)), + location: e.location, + color: e.color, + description: e.description, + id: e.id, + } + } + let start: Date; if (clickedDate.value) { const local = new Date(clickedDate.value + "T00:00:00"); @@ -95,6 +116,7 @@ async function onSubmit(vals: z.infer) { const end = parseLocalDateTime(vals.endDate, vals.endTime) const event: CalendarEvent = { + id: vals.id ?? null, name: vals.name, start, end, @@ -104,8 +126,11 @@ async function onSubmit(vals: z.infer) { } try { - console.log("Submitting CalendarEvent:", event) - await createCalendarEvent(event); + if (dialogMode.value === "edit") { + await editCalendarEvent(event); + } else { + await createCalendarEvent(event); + } emit('reload'); } catch (error) { console.error(error); @@ -128,6 +153,7 @@ const formRef = ref(null); Create Event +
() // const activeEvent = computed(() => props.event) @@ -91,6 +92,12 @@ async function setCancel(isCancelled: boolean) { emit("reload"); activeEvent.value = await getCalendarEvent(activeEvent.value.id); } + +async function forceReload() { + activeEvent.value = await getCalendarEvent(activeEvent.value.id); +} + +defineExpose({forceReload})