Refactor event creation form with improved UI and validation
Some checks failed
Build and Push Docker Images / changes (push) Successful in 5s
Build and Push Docker Images / build-backend (push) Has been skipped
Build and Push Docker Images / build-frontend (push) Has been cancelled

Replaced basic HTML form with styled components for better UI consistency. Added fields for start/end time and location URL, along with live slug validation to prevent reserved slugs. Enhanced layout and structure for improved usability and responsiveness.
This commit is contained in:
2025-03-12 09:48:50 +01:00
parent e896575f62
commit 193e9fa4d2

View File

@@ -3,41 +3,73 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useEvents } from "@/context/event-context"; import { useEvents } from "@/context/event-context";
import { EventCreate } from "@/client/types.gen"; import { EventCreate } from "@/client";
import Navbar from "@/components/layout/navbar"; import Navbar from "@/components/layout/navbar";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { RESERVED_SLUGS } from "@/constants";
const CreateEventPage: React.FC = () => { export default function CreateEventPage() {
const router = useRouter(); const router = useRouter();
const { createEvent, isCreating } = useEvents(); const { createEvent, isCreating } = useEvents();
const [slugError, setSlugError] = useState<string | null>(null);
const [formData, setFormData] = useState<EventCreate>({ const [formData, setFormData] = useState<EventCreate>({
title: "", title: "",
description: "", description: "",
location_name: "", location_name: "",
location_address: "", location_address: "",
location_url: "",
event_date: "", event_date: "",
event_start_time: "",
event_end_time: "",
timezone: "UTC", timezone: "UTC",
slug: "", slug: "",
is_public: false, is_public: false,
rsvp_enabled: true,
}); });
const onChange = ( const onChange = (
e: React.ChangeEvent< e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement
>,
) => { ) => {
const { name, value, type } = e.target; const { name, value, type } = e.target;
const checked = const checked =
type === "checkbox" ? (e.target as HTMLInputElement).checked : undefined; type === "checkbox" ? (e.target as HTMLInputElement).checked : undefined;
setFormData({
...formData, setFormData((prev) => ({
...prev,
[name]: type === "checkbox" ? checked : value, [name]: type === "checkbox" ? checked : value,
}); }));
// live validation for slug
if (name === "slug") {
if (RESERVED_SLUGS.includes(value)) {
setSlugError(`The slug "${value}" is reserved.`);
} else {
setSlugError(null);
}
}
};
const onTogglePublic = (checked: boolean) => {
setFormData((prev) => ({ ...prev, is_public: checked }));
}; };
const onSubmit = async (e: React.FormEvent) => { const onSubmit = async (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
if (RESERVED_SLUGS.includes(formData.slug)) {
alert(
`The slug "${formData.slug}" is reserved and cannot be used. Please choose another slug.`,
);
return;
}
try { try {
const event = await createEvent(formData); const event = await createEvent(formData);
router.push(`/dashboard/events/${event.slug}`); router.push(`/dashboard/events/${event.slug}`);
@@ -50,105 +82,140 @@ const CreateEventPage: React.FC = () => {
return ( return (
<> <>
<Navbar /> <Navbar />
<div className="container mx-auto px-4 py-8 max-w-4xl"> <div className="container mx-auto px-4 py-12 max-w-4xl">
<h1 className="text-3xl font-bold mb-8">Create New Event</h1> <Card className="shadow-lg">
<CardHeader>
<form <CardTitle className="text-2xl">Create New Event</CardTitle>
onSubmit={onSubmit} </CardHeader>
className="bg-background shadow-lg rounded-md p-6 space-y-4" <CardContent>
> <form onSubmit={onSubmit} className="grid grid-cols-1 gap-6">
<input <div>
<Label className={"mb-2"}>Event Title *</Label>
<Input
required
name="title" name="title"
placeholder="My Awesome Event"
value={formData.title} value={formData.title}
onChange={onChange} onChange={onChange}
placeholder="Event Title"
required
className="w-full rounded border border-border bg-background text-foreground placeholder:text-muted-foreground p-2"
/> />
</div>
<textarea <div>
<Label className={"mb-2"}>Description</Label>
<Textarea
name="description" name="description"
placeholder="Quick description of your event"
value={formData.description || ""} value={formData.description || ""}
onChange={onChange} onChange={onChange}
placeholder="Event Description"
className="w-full rounded border border-border bg-background text-foreground placeholder:text-muted-foreground p-2"
/> />
</div>
<input <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
name="slug" <div>
value={formData.slug} <Label className={"mb-2"}>Event Date *</Label>
onChange={onChange} <Input
placeholder="Slug (event-slug)" type="date"
pattern="^[a-z0-9-]+$"
required required
className="w-full rounded border border-border bg-background text-foreground placeholder:text-muted-foreground p-2"
/>
<input
type="datetime-local"
name="event_date" name="event_date"
value={formData.event_date} value={formData.event_date}
onChange={onChange} onChange={onChange}
required
className="w-full rounded border border-border bg-background text-foreground placeholder:text-muted-foreground p-2"
/> />
</div>
<div>
<Label className={"mb-2"}>Slug *</Label>
<Input
required
name="slug"
pattern="^[a-z0-9-]+$"
placeholder="my-awesome-event"
value={formData.slug}
onChange={onChange}
className={slugError ? "border-red-500" : ""}
/>
{slugError && (
<p className="text-sm text-red-500 mt-1">{slugError}</p>
)}
</div>
</div>
<input <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<Label className={"mb-2"}>Start Time</Label>
<Input
type="time"
name="event_start_time"
value={formData.event_start_time || ""}
onChange={onChange}
/>
</div>
<div>
<Label className={"mb-2"}>End Time</Label>
<Input
type="time"
name="event_end_time"
value={formData.event_end_time || ""}
onChange={onChange}
/>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<Label className={"mb-2"}>Location Name</Label>
<Input
name="location_name" name="location_name"
placeholder="Venue name"
value={formData.location_name || ""} value={formData.location_name || ""}
onChange={onChange} onChange={onChange}
placeholder="Location Name (e.g., Central Park)"
className="w-full rounded border border-border bg-background text-foreground placeholder:text-muted-foreground p-2"
/> />
</div>
<input <div>
<Label className={"mb-2"}>Location Address</Label>
<Input
name="location_address" name="location_address"
placeholder="123 Main Street"
value={formData.location_address || ""} value={formData.location_address || ""}
onChange={onChange} onChange={onChange}
placeholder="Location Address"
className="w-full rounded border border-border bg-background text-foreground placeholder:text-muted-foreground p-2"
/> />
</div>
</div>
<select <div>
name="timezone" <Label className={"mb-2"}>Location URL</Label>
value={formData.timezone} <Input
type="url"
name="location_url"
placeholder="https://maps.app/location"
value={formData.location_url || ""}
onChange={onChange} onChange={onChange}
required />
className="w-full rounded border border-border bg-background text-foreground placeholder:text-muted-foreground p-2" </div>
>
<option value="UTC">UTC</option>
<option value="Europe">Europe</option>
{/*<option value="Europe/London">Europe/London</option>*/}
{/* Additional timezone options */}
</select>
<label className="inline-flex items-center mt-2 text-foreground"> <div className="flex items-center space-x-2">
<input <Switch
type="checkbox" id="is_public"
name="is_public"
checked={formData.is_public} checked={formData.is_public}
onChange={onChange} onCheckedChange={onTogglePublic}
className="checkbox mr-2 rounded bg-background"
/> />
Public Event <Label htmlFor="is_public">Public Event</Label>
</label> </div>
<div className="flex items-center justify-end gap-4 mt-4"> <div className="flex justify-end gap-2 mt-4">
<Button type="submit" disabled={isCreating}>
{isCreating ? "Creating..." : "Create Event"}
</Button>
<Button <Button
type="button"
variant="outline" variant="outline"
type="button"
onClick={() => router.back()} onClick={() => router.back()}
> >
Cancel Cancel
</Button> </Button>
<Button disabled={isCreating} type="submit">
{isCreating ? "Creating..." : "Create Event"}
</Button>
</div> </div>
</form> </form>
</CardContent>
</Card>
</div> </div>
</> </>
); );
}; }
export default CreateEventPage;