Refactor event creation form with improved UI and validation
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:
@@ -3,41 +3,73 @@
|
||||
import React, { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEvents } from "@/context/event-context";
|
||||
import { EventCreate } from "@/client/types.gen";
|
||||
import { EventCreate } from "@/client";
|
||||
import Navbar from "@/components/layout/navbar";
|
||||
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 { createEvent, isCreating } = useEvents();
|
||||
const [slugError, setSlugError] = useState<string | null>(null);
|
||||
|
||||
const [formData, setFormData] = useState<EventCreate>({
|
||||
title: "",
|
||||
description: "",
|
||||
location_name: "",
|
||||
location_address: "",
|
||||
location_url: "",
|
||||
event_date: "",
|
||||
event_start_time: "",
|
||||
event_end_time: "",
|
||||
timezone: "UTC",
|
||||
slug: "",
|
||||
is_public: false,
|
||||
rsvp_enabled: true,
|
||||
});
|
||||
|
||||
const onChange = (
|
||||
e: React.ChangeEvent<
|
||||
HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement
|
||||
>,
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
|
||||
) => {
|
||||
const { name, value, type } = e.target;
|
||||
|
||||
const checked =
|
||||
type === "checkbox" ? (e.target as HTMLInputElement).checked : undefined;
|
||||
setFormData({
|
||||
...formData,
|
||||
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
[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) => {
|
||||
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 {
|
||||
const event = await createEvent(formData);
|
||||
router.push(`/dashboard/events/${event.slug}`);
|
||||
@@ -50,105 +82,140 @@ const CreateEventPage: React.FC = () => {
|
||||
return (
|
||||
<>
|
||||
<Navbar />
|
||||
<div className="container mx-auto px-4 py-8 max-w-4xl">
|
||||
<h1 className="text-3xl font-bold mb-8">Create New Event</h1>
|
||||
|
||||
<form
|
||||
onSubmit={onSubmit}
|
||||
className="bg-background shadow-lg rounded-md p-6 space-y-4"
|
||||
>
|
||||
<input
|
||||
<div className="container mx-auto px-4 py-12 max-w-4xl">
|
||||
<Card className="shadow-lg">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-2xl">Create New Event</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form onSubmit={onSubmit} className="grid grid-cols-1 gap-6">
|
||||
<div>
|
||||
<Label className={"mb-2"}>Event Title *</Label>
|
||||
<Input
|
||||
required
|
||||
name="title"
|
||||
placeholder="My Awesome Event"
|
||||
value={formData.title}
|
||||
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"
|
||||
placeholder="Quick description of your event"
|
||||
value={formData.description || ""}
|
||||
onChange={onChange}
|
||||
placeholder="Event Description"
|
||||
className="w-full rounded border border-border bg-background text-foreground placeholder:text-muted-foreground p-2"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<input
|
||||
name="slug"
|
||||
value={formData.slug}
|
||||
onChange={onChange}
|
||||
placeholder="Slug (event-slug)"
|
||||
pattern="^[a-z0-9-]+$"
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<Label className={"mb-2"}>Event Date *</Label>
|
||||
<Input
|
||||
type="date"
|
||||
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"
|
||||
value={formData.event_date}
|
||||
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"
|
||||
placeholder="Venue name"
|
||||
value={formData.location_name || ""}
|
||||
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"
|
||||
/>
|
||||
|
||||
<input
|
||||
</div>
|
||||
<div>
|
||||
<Label className={"mb-2"}>Location Address</Label>
|
||||
<Input
|
||||
name="location_address"
|
||||
placeholder="123 Main Street"
|
||||
value={formData.location_address || ""}
|
||||
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
|
||||
name="timezone"
|
||||
value={formData.timezone}
|
||||
<div>
|
||||
<Label className={"mb-2"}>Location URL</Label>
|
||||
<Input
|
||||
type="url"
|
||||
name="location_url"
|
||||
placeholder="https://maps.app/location"
|
||||
value={formData.location_url || ""}
|
||||
onChange={onChange}
|
||||
required
|
||||
className="w-full rounded border border-border bg-background text-foreground placeholder:text-muted-foreground p-2"
|
||||
>
|
||||
<option value="UTC">UTC</option>
|
||||
<option value="Europe">Europe</option>
|
||||
{/*<option value="Europe/London">Europe/London</option>*/}
|
||||
{/* Additional timezone options */}
|
||||
</select>
|
||||
/>
|
||||
</div>
|
||||
|
||||
<label className="inline-flex items-center mt-2 text-foreground">
|
||||
<input
|
||||
type="checkbox"
|
||||
name="is_public"
|
||||
<div className="flex items-center space-x-2">
|
||||
<Switch
|
||||
id="is_public"
|
||||
checked={formData.is_public}
|
||||
onChange={onChange}
|
||||
className="checkbox mr-2 rounded bg-background"
|
||||
onCheckedChange={onTogglePublic}
|
||||
/>
|
||||
Public Event
|
||||
</label>
|
||||
<Label htmlFor="is_public">Public Event</Label>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-end gap-4 mt-4">
|
||||
<Button type="submit" disabled={isCreating}>
|
||||
{isCreating ? "Creating..." : "Create Event"}
|
||||
</Button>
|
||||
<div className="flex justify-end gap-2 mt-4">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
type="button"
|
||||
onClick={() => router.back()}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button disabled={isCreating} type="submit">
|
||||
{isCreating ? "Creating..." : "Create Event"}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default CreateEventPage;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user