hooked up create event
This commit is contained in:
@@ -21,11 +21,9 @@ import {
|
||||
} from "@/components/ui/form"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import Textarea from "../ui/textarea/Textarea.vue"
|
||||
import { CalendarEvent } from "@/api/calendar"
|
||||
import { CalendarEvent } from "@shared/types/calendar"
|
||||
|
||||
|
||||
// ---------- helpers ----------
|
||||
const dateRe = /^\d{4}-\d{2}-\d{2}$/ // YYYY-MM-DD
|
||||
const timeRe = /^(?:[01]\d|2[0-3]):[0-5]\d$/ // HH:mm (24h)
|
||||
|
||||
function toLocalDateString(d: Date) {
|
||||
// yyyy-MM-dd with local time zone
|
||||
@@ -45,37 +43,11 @@ function roundToNextHour(d = new Date()) {
|
||||
t.setHours(t.getHours() + 1)
|
||||
return t
|
||||
}
|
||||
function parseLocalDateTime(dateStr: string, timeStr: string) {
|
||||
// Construct a Date in the user's local timezone
|
||||
const [y, m, d] = dateStr.split("-").map(Number)
|
||||
const [hh, mm] = timeStr.split(":").map(Number)
|
||||
return new Date(y, m - 1, d, hh, mm, 0, 0)
|
||||
}
|
||||
|
||||
// ---------- schema ----------
|
||||
const zEvent = z.object({
|
||||
name: z.string().min(2, "Please enter at least 2 characters").max(100),
|
||||
startDate: z.string().regex(dateRe, "Use YYYY-MM-DD"),
|
||||
startTime: z.string().regex(timeRe, "Use HH:mm (24h)"),
|
||||
endDate: z.string().regex(dateRe, "Use YYYY-MM-DD"),
|
||||
endTime: z.string().regex(timeRe, "Use HH:mm (24h)"),
|
||||
location: z.string().max(200).default(""),
|
||||
color: z.string().regex(/^#([0-9A-Fa-f]{6})$/, "Use a hex color like #AABBCC"),
|
||||
description: z.string().max(2000).default(""),
|
||||
id: z.number().int().nonnegative().nullable().default(null),
|
||||
}).superRefine((vals, ctx) => {
|
||||
const start = parseLocalDateTime(vals.startDate, vals.startTime)
|
||||
const end = parseLocalDateTime(vals.endDate, vals.endTime)
|
||||
if (!(end > start)) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "End must be after start",
|
||||
path: ["endTime"], // attach to a visible field
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const formSchema = toTypedSchema(zEvent)
|
||||
import { calendarEventSchema, parseLocalDateTime } from '@shared/schemas/calendarEventSchema'
|
||||
import { createCalendarEvent } from "@/api/calendar"
|
||||
const formSchema = toTypedSchema(calendarEventSchema)
|
||||
|
||||
// ---------- dialog state & defaults ----------
|
||||
const dialogOpen = ref(false)
|
||||
@@ -107,7 +79,7 @@ watch(dialogOpen, (isOpen) => {
|
||||
}
|
||||
})
|
||||
// ---------- submit ----------
|
||||
function onSubmit(vals: z.infer<typeof zEvent>) {
|
||||
function onSubmit(vals: z.infer<typeof calendarEventSchema>) {
|
||||
const start = parseLocalDateTime(vals.startDate, vals.startTime)
|
||||
const end = parseLocalDateTime(vals.endDate, vals.endTime)
|
||||
|
||||
@@ -118,11 +90,14 @@ function onSubmit(vals: z.infer<typeof zEvent>) {
|
||||
location: vals.location,
|
||||
color: vals.color,
|
||||
description: vals.description,
|
||||
id: null,
|
||||
creator: null
|
||||
}
|
||||
|
||||
console.log("Submitting CalendarEvent:", event)
|
||||
try {
|
||||
console.log("Submitting CalendarEvent:", event)
|
||||
createCalendarEvent(event);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
// close after success
|
||||
dialogOpen.value = false
|
||||
|
||||
Reference in New Issue
Block a user