Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

✨ Add edit event type dialog (RAL-1185) (#2387)

Luke Vella4ca2bc7b0d61
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.json
index 62322bd92..d8286141c 100644
--- a/apps/web/public/locales/en/app.json
+++ b/apps/web/public/locales/en/app.json
@@ -657,5 +657,8 @@
"eventTypeActionsLabel": "Event type actions",
"deleteEventTypeTitle": "Delete event type?",
"deleteEventTypeDescription": "Existing bookings for <b>{name}</b> will not be affected, but no new bookings can be made.",
- "deleteEventTypeError": "Failed to delete event type"
+ "deleteEventTypeError": "Failed to delete event type",
+ "edit": "Edit",
+ "editEventTypeTitle": "Edit Event Type",
+ "updateEventTypeError": "Failed to update event type"
}
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/event-types/event-types-page.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/event-types/event-types-page.tsx
index 7fb5bf558..10cf0d0ac 100644
--- a/apps/web/src/app/[locale]/(space)/(dashboard)/event-types/event-types-page.tsx
+++ b/apps/web/src/app/[locale]/(space)/(dashboard)/event-types/event-types-page.tsx
@@ -18,6 +18,7 @@ import {
Link2Icon,
MapPinIcon,
MoreVerticalIcon,
+ PencilIcon,
PlusIcon,
Trash2Icon,
} from "lucide-react";
@@ -41,6 +42,8 @@ import { OptimizedAvatarImage } from "@/components/optimized-avatar-image";
import { RandomGradientBar } from "@/components/random-gradient-bar";
import { CreateEventTypeDialog } from "@/features/event-types/components/create-event-type-dialog";
import { DeleteEventTypeDialog } from "@/features/event-types/components/delete-event-type-dialog";
+import { EditEventTypeDialog } from "@/features/event-types/components/edit-event-type-dialog";
+import type { EventTypeDTO } from "@/features/event-types/types";
import { Trans } from "@/i18n/client";
import { useLocale } from "@/lib/locale/client";
import type { LocationType } from "@/lib/location";
@@ -80,6 +83,7 @@ function EventTypeCard({
hostImage,
locationType,
locationLabel,
+ onEdit,
onDelete,
}: {
name: string;
@@ -89,6 +93,7 @@ function EventTypeCard({
hostImage?: string;
locationType: LocationType | null;
locationLabel: string | null;
+ onEdit: () => void;
onDelete: () => void;
}) {
const { locale } = useLocale();
@@ -113,6 +118,10 @@ function EventTypeCard({
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
+ <DropdownMenuItem onClick={onEdit}>
+ <PencilIcon className="size-4" />
+ <Trans i18nKey="edit" defaults="Edit" />
+ </DropdownMenuItem>
<DropdownMenuItem variant="destructive" onClick={onDelete}>
<Trash2Icon className="size-4" />
<Trans i18nKey="delete" defaults="Delete" />
@@ -156,11 +165,10 @@ function EventTypeCard({
export function EventTypesPage() {
const [{ eventTypes }] = trpc.eventTypes.list.useSuspenseQuery();
const createDialog = useDialog();
+ const editDialog = useDialog();
const deleteDialog = useDialog();
- const [selectedEventType, setSelectedEventType] = React.useState<{
- id: string;
- name: string;
- } | null>(null);
+ const [selectedEventType, setSelectedEventType] =
+ React.useState<EventTypeDTO | null>(null);
return (
<PageContainer>
@@ -199,11 +207,12 @@ export function EventTypesPage() {
: (eventType.location.text ?? eventType.location.url)
: null
}
+ onEdit={() => {
+ setSelectedEventType(eventType);
+ editDialog.trigger();
+ }}
onDelete={() => {
- setSelectedEventType({
- id: eventType.id,
- name: eventType.name,
- });
+ setSelectedEventType(eventType);
deleteDialog.trigger();
}}
/>
@@ -214,11 +223,17 @@ export function EventTypesPage() {
</PageContent>
<CreateEventTypeDialog {...createDialog.dialogProps} />
{selectedEventType ? (
- <DeleteEventTypeDialog
- {...deleteDialog.dialogProps}
- eventTypeId={selectedEventType.id}
- eventTypeName={selectedEventType.name}
- />
+ <>
+ <EditEventTypeDialog
+ {...editDialog.dialogProps}
+ eventType={selectedEventType}
+ />
+ <DeleteEventTypeDialog
+ {...deleteDialog.dialogProps}
+ eventTypeId={selectedEventType.id}
+ eventTypeName={selectedEventType.name}
+ />
+ </>
) : null}
</PageContainer>
);
diff --git a/apps/web/src/features/event-types/components/create-event-type-dialog.tsx b/apps/web/src/features/event-types/components/create-event-type-dialog.tsx
index a5641c65c..539a8869c 100644
--- a/apps/web/src/features/event-types/components/create-event-type-dialog.tsx
+++ b/apps/web/src/features/event-types/components/create-event-type-dialog.tsx
@@ -13,95 +13,19 @@ import {
DialogHeader,
DialogTitle,
} from "@rallly/ui/dialog";
-import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
-} from "@rallly/ui/form";
-import { Input } from "@rallly/ui/input";
-import { RadioGroup, RadioGroupItem } from "@rallly/ui/radio-group";
+import { Form } from "@rallly/ui/form";
import { toast } from "@rallly/ui/sonner";
-import { Textarea } from "@rallly/ui/textarea";
-import { Link2Icon, MapPinIcon, PlusIcon } from "lucide-react";
import React from "react";
import { useForm } from "react-hook-form";
-import { z } from "zod";
+import {
+ buildEventTypeFormSchema,
+ EventTypeFormFields,
+ eventTypeFormDefaults,
+ eventTypeValuesToInput,
+} from "@/features/event-types/components/event-type-form";
import { Trans, useTranslation } from "@/i18n/client";
import { trpc } from "@/trpc/client";
-function buildFormSchema(
- t: (key: string, opts: { defaultValue: string }) => string,
-) {
- return z
- .object({
- name: z.string().min(1).max(255),
- duration: z.number().int().positive(),
- maxAttendees: z.string().optional(),
- description: z.string().max(2000).optional(),
- locationType: z.enum(["", "in_person", "custom_link"]),
- locationAddress: z.string().optional(),
- locationUrl: z.string().optional(),
- locationText: z.string().optional(),
- })
- .superRefine((data, ctx) => {
- if (data.maxAttendees) {
- const parsed = Number(data.maxAttendees);
- if (!Number.isInteger(parsed) || parsed <= 0) {
- ctx.addIssue({
- path: ["maxAttendees"],
- code: "custom",
- message: t("maxAttendeesInvalid", {
- defaultValue: "Must be a positive integer",
- }),
- });
- }
- }
- if (data.locationType === "in_person") {
- if (!data.locationAddress?.trim()) {
- ctx.addIssue({
- path: ["locationAddress"],
- code: "custom",
- message: t("requiredField", { defaultValue: "Required" }),
- });
- }
- } else if (data.locationType === "custom_link") {
- if (!data.locationUrl?.trim()) {
- ctx.addIssue({
- path: ["locationUrl"],
- code: "custom",
- message: t("requiredField", { defaultValue: "Required" }),
- });
- } else {
- try {
- new URL(data.locationUrl);
- } catch {
- ctx.addIssue({
- path: ["locationUrl"],
- code: "custom",
- message: t("urlInvalid", {
- defaultValue: "Must be a valid URL",
- }),
- });
- }
- }
- }
- });
-}
-
-const defaultValues = {
- name: "",
- duration: 60,
- maxAttendees: "",
- description: "",
- locationType: "" as "" | "in_person" | "custom_link",
- locationAddress: "",
- locationUrl: "",
- locationText: "",
-};
-
export function CreateEventTypeDialog({ open, onOpenChange }: DialogProps) {
const { t } = useTranslation();
const posthog = usePostHog();
@@ -109,18 +33,16 @@ export function CreateEventTypeDialog({ open, onOpenChange }: DialogProps) {
const [showMaxAttendees, setShowMaxAttendees] = React.useState(false);
const [showDescription, setShowDescription] = React.useState(false);
- const formSchema = React.useMemo(() => buildFormSchema(t), [t]);
+ const formSchema = React.useMemo(() => buildEventTypeFormSchema(t), [t]);
const form = useForm({
resolver: zodResolver(formSchema),
- defaultValues,
+ defaultValues: eventTypeFormDefaults,
});
- const locationType = form.watch("locationType");
-
const handleOpenChange = (nextOpen: boolean) => {
if (!nextOpen) {
- form.reset(defaultValues);
+ form.reset(eventTypeFormDefaults);
setShowMaxAttendees(false);
setShowDescription(false);
}
@@ -128,30 +50,10 @@ export function CreateEventTypeDialog({ open, onOpenChange }: DialogProps) {
};
const onSubmit = form.handleSubmit(async (values) => {
- const capacity = values.maxAttendees ? Number(values.maxAttendees) : null;
- const description = values.description?.trim() || undefined;
- const location =
- values.locationType === "in_person" && values.locationAddress
- ? {
- type: "in_person" as const,
- address: values.locationAddress.trim(),
- }
- : values.locationType === "custom_link" && values.locationUrl
- ? {
- type: "custom_link" as const,
- url: values.locationUrl.trim(),
- text: values.locationText?.trim() || undefined,
- }
- : undefined;
+ const input = eventTypeValuesToInput(values);
try {
- await createEventType.mutateAsync({
- name: values.name.trim(),
- duration: values.duration,
- capacity,
- description,
- location,
- });
+ await createEventType.mutateAsync(input);
} catch {
toast.error(
t("createEventTypeError", {
@@ -161,26 +63,14 @@ export function CreateEventTypeDialog({ open, onOpenChange }: DialogProps) {
return;
}
posthog?.capture("event_type_creation:form_submit", {
- duration_minutes: values.duration,
- location_type: location?.type ?? "none",
- has_max_attendees: capacity !== null,
- has_description: description !== undefined,
+ duration_minutes: input.duration,
+ location_type: input.location?.type ?? "none",
+ has_max_attendees: input.capacity !== null,
+ has_description: input.description !== undefined,
});
handleOpenChange(false);
});
- const handleRemoveMaxAttendees = () => {
- setShowMaxAttendees(false);
- form.setValue("maxAttendees", "");
- form.clearErrors("maxAttendees");
- };
-
- const handleRemoveDescription = () => {
- setShowDescription(false);
- form.setValue("description", "");
- form.clearErrors("description");
- };
-
return (
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogContent size="lg">
@@ -200,260 +90,13 @@ export function CreateEventTypeDialog({ open, onOpenChange }: DialogProps) {
</DialogHeader>
<Form {...form}>
<form onSubmit={onSubmit} className="space-y-5">
- <div className="grid gap-4 sm:grid-cols-[1fr_8rem]">
- <FormField
- control={form.control}
- name="name"
- render={({ field }) => (
- <FormItem>
- <FormLabel>
- <Trans i18nKey="name" defaults="Name" />
- </FormLabel>
- <FormControl>
- <Input
- {...field}
- autoFocus
- placeholder={t("eventTypeNamePlaceholder", {
- defaultValue: "Discovery call",
- })}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="duration"
- render={({ field }) => (
- <FormItem>
- <FormLabel>
- <Trans i18nKey="durationLabel" defaults="Duration" />
- </FormLabel>
- <FormControl>
- <div className="relative">
- <Input
- type="number"
- min={1}
- step={1}
- className="pr-10"
- {...field}
- onChange={(event) => {
- const value = event.target.value;
- field.onChange(value === "" ? "" : Number(value));
- }}
- />
- <span className="pointer-events-none absolute inset-y-0 right-2.5 flex items-center text-muted-foreground text-sm">
- <Trans i18nKey="minutesAbbreviation" defaults="min" />
- </span>
- </div>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- </div>
-
- <FormField
- control={form.control}
- name="locationType"
- render={({ field }) => (
- <FormItem>
- <FormLabel>
- <Trans i18nKey="location" defaults="Location" />
- </FormLabel>
- <FormControl>
- <RadioGroup
- value={field.value}
- onValueChange={(value) => {
- field.onChange(value);
- form.clearErrors(["locationAddress", "locationUrl"]);
- }}
- className="grid grid-cols-2 gap-2"
- >
- <RadioCardOption
- value="in_person"
- icon={<MapPinIcon className="size-4" />}
- label={t("locationInPerson", {
- defaultValue: "In person",
- })}
- />
- <RadioCardOption
- value="custom_link"
- icon={<Link2Icon className="size-4" />}
- label={t("locationCustomLink", {
- defaultValue: "Custom link",
- })}
- />
- </RadioGroup>
- </FormControl>
- </FormItem>
- )}
+ <EventTypeFormFields
+ form={form}
+ showMaxAttendees={showMaxAttendees}
+ setShowMaxAttendees={setShowMaxAttendees}
+ showDescription={showDescription}
+ setShowDescription={setShowDescription}
/>
-
- {locationType === "in_person" ? (
- <FormField
- control={form.control}
- name="locationAddress"
- render={({ field }) => (
- <FormItem>
- <FormLabel>
- <Trans i18nKey="address" defaults="Address" />
- </FormLabel>
- <FormControl>
- <Input
- {...field}
- placeholder={t("addressPlaceholder", {
- defaultValue: "123 Main Street",
- })}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- ) : null}
-
- {locationType === "custom_link" ? (
- <div className="grid gap-3 sm:grid-cols-2">
- <FormField
- control={form.control}
- name="locationUrl"
- render={({ field }) => (
- <FormItem>
- <FormLabel>
- <Trans i18nKey="url" defaults="URL" />
- </FormLabel>
- <FormControl>
- <Input type="url" {...field} placeholder="https://" />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="locationText"
- render={({ field }) => (
- <FormItem>
- <FormLabel className="flex items-center gap-1.5">
- <Trans
- i18nKey="locationDisplayText"
- defaults="Display text"
- />
- <span className="text-muted-foreground">
- <Trans
- i18nKey="optionalLabel"
- defaults="(Optional)"
- />
- </span>
- </FormLabel>
- <FormControl>
- <Input
- {...field}
- placeholder={t("locationDisplayTextPlaceholder", {
- defaultValue: "Meeting link",
- })}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- </div>
- ) : null}
-
- {showMaxAttendees ? (
- <FormField
- control={form.control}
- name="maxAttendees"
- render={({ field }) => (
- <FormItem>
- <div className="flex items-center justify-between">
- <FormLabel>
- <Trans
- i18nKey="maxAttendees"
- defaults="Max attendees"
- />
- </FormLabel>
- <RemoveOptionalButton
- onClick={handleRemoveMaxAttendees}
- />
- </div>
- <FormControl>
- <Input type="number" min={1} step={1} {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- ) : null}
-
- {showDescription ? (
- <FormField
- control={form.control}
- name="description"
- render={({ field }) => (
- <FormItem>
- <div className="flex items-center justify-between">
- <FormLabel>
- <Trans i18nKey="description" defaults="Description" />
- </FormLabel>
- <RemoveOptionalButton onClick={handleRemoveDescription} />
- </div>
- <FormControl>
- <Textarea
- {...field}
- rows={3}
- placeholder={t("eventTypeDescriptionPlaceholder", {
- defaultValue: "What is this event about?",
- })}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- ) : null}
-
- {!showMaxAttendees || !showDescription ? (
- <div className="flex flex-wrap items-center gap-1">
- {!showMaxAttendees ? (
- <Button
- type="button"
- variant="ghost"
- size="sm"
- onClick={() => {
- setShowMaxAttendees(true);
- form.setValue("maxAttendees", "1");
- }}
- className="text-muted-foreground"
- >
- <PlusIcon data-icon="inline-start" />
- <Trans
- i18nKey="setMaxAttendees"
- defaults="Set max attendees"
- />
- </Button>
- ) : null}
- {!showDescription ? (
- <Button
- type="button"
- variant="ghost"
- size="sm"
- onClick={() => setShowDescription(true)}
- className="text-muted-foreground"
- >
- <PlusIcon data-icon="inline-start" />
- <Trans
- i18nKey="addDescription"
- defaults="Add description"
- />
- </Button>
- ) : null}
- </div>
- ) : null}
-
<DialogFooter className="pt-2">
<DialogClose asChild>
<Button type="button" disabled={createEventType.isPending}>
@@ -474,41 +117,3 @@ export function CreateEventTypeDialog({ open, onOpenChange }: DialogProps) {
</Dialog>
);
}
-
-function RadioCardOption({
- value,
- icon,
- label,
-}: {
- value: string;
- icon: React.ReactNode;
- label: string;
-}) {
- const id = React.useId();
- return (
- <label
- htmlFor={id}
- className="group flex cursor-pointer items-center gap-2.5 rounded-lg border border-input bg-card px-3 py-2.5 text-sm transition-colors hover:bg-accent has-data-[state=checked]:border-primary has-data-[state=checked]:bg-primary/5"
- >
- <RadioGroupItem id={id} value={value} />
- <span className="flex items-center gap-1.5 font-medium text-foreground">
- <span className="text-muted-foreground group-has-data-[state=checked]:text-primary">
- {icon}
- </span>
- {label}
- </span>
- </label>
- );
-}
-
-function RemoveOptionalButton({ onClick }: { onClick: () => void }) {
- return (
- <button
- type="button"
- onClick={onClick}
- className="font-normal text-muted-foreground text-sm underline-offset-2 hover:text-foreground hover:underline"
- >
- <Trans i18nKey="remove" defaults="Remove" />
- </button>
- );
-}
diff --git a/apps/web/src/features/event-types/components/edit-event-type-dialog.tsx b/apps/web/src/features/event-types/components/edit-event-type-dialog.tsx
new file mode 100644
index 000000000..cc7959045
--- /dev/null
+++ b/apps/web/src/features/event-types/components/edit-event-type-dialog.tsx
@@ -0,0 +1,131 @@
+"use client";
+
+import { zodResolver } from "@hookform/resolvers/zod";
+import { usePostHog } from "@rallly/posthog/client";
+import { Button } from "@rallly/ui/button";
+import type { DialogProps } from "@rallly/ui/dialog";
+import {
+ Dialog,
+ DialogClose,
+ DialogContent,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@rallly/ui/dialog";
+import { Form } from "@rallly/ui/form";
+import { toast } from "@rallly/ui/sonner";
+import React from "react";
+import { useForm } from "react-hook-form";
+import {
+ buildEventTypeFormSchema,
+ EventTypeFormFields,
+ eventTypeToFormValues,
+ eventTypeValuesToInput,
+} from "@/features/event-types/components/event-type-form";
+import type { EventTypeDTO } from "@/features/event-types/types";
+import { Trans, useTranslation } from "@/i18n/client";
+import { trpc } from "@/trpc/client";
+
+export function EditEventTypeDialog({
+ eventType,
+ open,
+ onOpenChange,
+}: DialogProps & {
+ eventType: EventTypeDTO;
+}) {
+ const { t } = useTranslation();
+ const posthog = usePostHog();
+ const updateEventType = trpc.eventTypes.update.useMutation();
+
+ const initialValues = React.useMemo(
+ () => eventTypeToFormValues(eventType),
+ [eventType],
+ );
+
+ const [showMaxAttendees, setShowMaxAttendees] = React.useState(
+ eventType.capacity !== null,
+ );
+ const [showDescription, setShowDescription] = React.useState(
+ eventType.description !== null,
+ );
+
+ const formSchema = React.useMemo(() => buildEventTypeFormSchema(t), [t]);
+
+ const form = useForm({
+ resolver: zodResolver(formSchema),
+ defaultValues: initialValues,
+ });
+
+ React.useEffect(() => {
+ form.reset(initialValues);
+ setShowMaxAttendees(eventType.capacity !== null);
+ setShowDescription(eventType.description !== null);
+ }, [eventType.capacity, eventType.description, form, initialValues]);
+
+ const handleOpenChange = (nextOpen: boolean) => {
+ if (!nextOpen) {
+ form.reset(initialValues);
+ }
+ onOpenChange?.(nextOpen);
+ };
+
+ const onSubmit = form.handleSubmit(async (values) => {
+ const input = eventTypeValuesToInput(values);
+
+ try {
+ await updateEventType.mutateAsync({ id: eventType.id, ...input });
+ } catch {
+ toast.error(
+ t("updateEventTypeError", {
+ defaultValue: "Failed to update event type",
+ }),
+ );
+ return;
+ }
+ posthog?.capture("event_type_edit:form_submit", {
+ event_type_id: eventType.id,
+ duration_minutes: input.duration,
+ location_type: input.location?.type ?? "none",
+ has_max_attendees: input.capacity !== null,
+ has_description: input.description !== undefined,
+ });
+ handleOpenChange(false);
+ });
+
+ return (
+ <Dialog open={open} onOpenChange={handleOpenChange}>
+ <DialogContent size="lg">
+ <DialogHeader>
+ <DialogTitle>
+ <Trans i18nKey="editEventTypeTitle" defaults="Edit Event Type" />
+ </DialogTitle>
+ </DialogHeader>
+ <Form {...form}>
+ <form onSubmit={onSubmit} className="space-y-5">
+ <EventTypeFormFields
+ form={form}
+ showMaxAttendees={showMaxAttendees}
+ setShowMaxAttendees={setShowMaxAttendees}
+ showDescription={showDescription}
+ setShowDescription={setShowDescription}
+ />
+ <DialogFooter className="pt-2">
+ <DialogClose asChild>
+ <Button type="button" disabled={updateEventType.isPending}>
+ <Trans i18nKey="cancel" defaults="Cancel" />
+ </Button>
+ </DialogClose>
+ <Button
+ type="submit"
+ variant="primary"
+ loading={updateEventType.isPending}
+ >
+ <Trans i18nKey="save" defaults="Save" />
+ </Button>
+ </DialogFooter>
+ </form>
+ </Form>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/apps/web/src/features/event-types/components/event-type-form.tsx b/apps/web/src/features/event-types/components/event-type-form.tsx
new file mode 100644
index 000000000..74f7a0169
--- /dev/null
+++ b/apps/web/src/features/event-types/components/event-type-form.tsx
@@ -0,0 +1,451 @@
+"use client";
+
+import { Button } from "@rallly/ui/button";
+import {
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@rallly/ui/form";
+import { Input } from "@rallly/ui/input";
+import { RadioGroup, RadioGroupItem } from "@rallly/ui/radio-group";
+import { Textarea } from "@rallly/ui/textarea";
+import { Link2Icon, MapPinIcon, PlusIcon } from "lucide-react";
+import React from "react";
+import type { UseFormReturn } from "react-hook-form";
+import { z } from "zod";
+import type { EventTypeDTO } from "@/features/event-types/types";
+import { Trans, useTranslation } from "@/i18n/client";
+import type { Location } from "@/lib/location";
+
+export function buildEventTypeFormSchema(
+ t: (key: string, opts: { defaultValue: string }) => string,
+) {
+ return z
+ .object({
+ name: z.string().min(1).max(255),
+ duration: z.number().int().positive(),
+ maxAttendees: z.string().optional(),
+ description: z.string().max(2000).optional(),
+ locationType: z.enum(["", "in_person", "custom_link"]),
+ locationAddress: z.string().optional(),
+ locationUrl: z.string().optional(),
+ locationText: z.string().optional(),
+ })
+ .superRefine((data, ctx) => {
+ if (data.maxAttendees) {
+ const parsed = Number(data.maxAttendees);
+ if (!Number.isInteger(parsed) || parsed <= 0) {
+ ctx.addIssue({
+ path: ["maxAttendees"],
+ code: "custom",
+ message: t("maxAttendeesInvalid", {
+ defaultValue: "Must be a positive integer",
+ }),
+ });
+ }
+ }
+ if (data.locationType === "in_person") {
+ if (!data.locationAddress?.trim()) {
+ ctx.addIssue({
+ path: ["locationAddress"],
+ code: "custom",
+ message: t("requiredField", { defaultValue: "Required" }),
+ });
+ }
+ } else if (data.locationType === "custom_link") {
+ if (!data.locationUrl?.trim()) {
+ ctx.addIssue({
+ path: ["locationUrl"],
+ code: "custom",
+ message: t("requiredField", { defaultValue: "Required" }),
+ });
+ } else {
+ try {
+ new URL(data.locationUrl);
+ } catch {
+ ctx.addIssue({
+ path: ["locationUrl"],
+ code: "custom",
+ message: t("urlInvalid", {
+ defaultValue: "Must be a valid URL",
+ }),
+ });
+ }
+ }
+ }
+ });
+}
+
+export type EventTypeFormValues = z.infer<
+ ReturnType<typeof buildEventTypeFormSchema>
+>;
+
+export const eventTypeFormDefaults: EventTypeFormValues = {
+ name: "",
+ duration: 60,
+ maxAttendees: "",
+ description: "",
+ locationType: "",
+ locationAddress: "",
+ locationUrl: "",
+ locationText: "",
+};
+
+export function eventTypeToFormValues(
+ eventType: EventTypeDTO,
+): EventTypeFormValues {
+ const location = eventType.location;
+ return {
+ name: eventType.name,
+ duration: eventType.duration,
+ maxAttendees: eventType.capacity !== null ? String(eventType.capacity) : "",
+ description: eventType.description ?? "",
+ locationType: location?.type ?? "",
+ locationAddress: location?.type === "in_person" ? location.address : "",
+ locationUrl: location?.type === "custom_link" ? location.url : "",
+ locationText: location?.type === "custom_link" ? (location.text ?? "") : "",
+ };
+}
+
+type EventTypeInput = {
+ name: string;
+ duration: number;
+ capacity: number | null;
+ description: string | undefined;
+ location: Location | undefined;
+};
+
+export function eventTypeValuesToInput(
+ values: EventTypeFormValues,
+): EventTypeInput {
+ const capacity = values.maxAttendees ? Number(values.maxAttendees) : null;
+ const description = values.description?.trim() || undefined;
+ const location: Location | undefined =
+ values.locationType === "in_person" && values.locationAddress
+ ? { type: "in_person", address: values.locationAddress.trim() }
+ : values.locationType === "custom_link" && values.locationUrl
+ ? {
+ type: "custom_link",
+ url: values.locationUrl.trim(),
+ text: values.locationText?.trim() || undefined,
+ }
+ : undefined;
+ return {
+ name: values.name.trim(),
+ duration: values.duration,
+ capacity,
+ description,
+ location,
+ };
+}
+
+export function EventTypeFormFields({
+ form,
+ showMaxAttendees,
+ setShowMaxAttendees,
+ showDescription,
+ setShowDescription,
+}: {
+ form: UseFormReturn<EventTypeFormValues>;
+ showMaxAttendees: boolean;
+ setShowMaxAttendees: (value: boolean) => void;
+ showDescription: boolean;
+ setShowDescription: (value: boolean) => void;
+}) {
+ const { t } = useTranslation();
+ const locationType = form.watch("locationType");
+
+ const handleRemoveMaxAttendees = () => {
+ setShowMaxAttendees(false);
+ form.setValue("maxAttendees", "");
+ form.clearErrors("maxAttendees");
+ };
+
+ const handleRemoveDescription = () => {
+ setShowDescription(false);
+ form.setValue("description", "");
+ form.clearErrors("description");
+ };
+
+ return (
+ <>
+ <div className="grid gap-4 sm:grid-cols-[1fr_8rem]">
+ <FormField
+ control={form.control}
+ name="name"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>
+ <Trans i18nKey="name" defaults="Name" />
+ </FormLabel>
+ <FormControl>
+ <Input
+ {...field}
+ autoFocus
+ placeholder={t("eventTypeNamePlaceholder", {
+ defaultValue: "Discovery call",
+ })}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="duration"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>
+ <Trans i18nKey="durationLabel" defaults="Duration" />
+ </FormLabel>
+ <FormControl>
+ <div className="relative">
+ <Input
+ type="number"
+ min={1}
+ step={1}
+ className="pr-10"
+ {...field}
+ onChange={(event) => {
+ const value = event.target.value;
+ field.onChange(value === "" ? "" : Number(value));
+ }}
+ />
+ <span className="pointer-events-none absolute inset-y-0 right-2.5 flex items-center text-muted-foreground text-sm">
+ <Trans i18nKey="minutesAbbreviation" defaults="min" />
+ </span>
+ </div>
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </div>
+
+ <FormField
+ control={form.control}
+ name="locationType"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>
+ <Trans i18nKey="location" defaults="Location" />
+ </FormLabel>
+ <FormControl>
+ <RadioGroup
+ value={field.value}
+ onValueChange={(value) => {
+ field.onChange(value);
+ form.clearErrors(["locationAddress", "locationUrl"]);
+ }}
+ className="grid grid-cols-2 gap-2"
+ >
+ <RadioCardOption
+ value="in_person"
+ icon={<MapPinIcon className="size-4" />}
+ label={t("locationInPerson", { defaultValue: "In person" })}
+ />
+ <RadioCardOption
+ value="custom_link"
+ icon={<Link2Icon className="size-4" />}
+ label={t("locationCustomLink", {
+ defaultValue: "Custom link",
+ })}
+ />
+ </RadioGroup>
+ </FormControl>
+ </FormItem>
+ )}
+ />
+
+ {locationType === "in_person" ? (
+ <FormField
+ control={form.control}
+ name="locationAddress"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>
+ <Trans i18nKey="address" defaults="Address" />
+ </FormLabel>
+ <FormControl>
+ <Input
+ {...field}
+ placeholder={t("addressPlaceholder", {
+ defaultValue: "123 Main Street",
+ })}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ ) : null}
+
+ {locationType === "custom_link" ? (
+ <div className="grid gap-3 sm:grid-cols-2">
+ <FormField
+ control={form.control}
+ name="locationUrl"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>
+ <Trans i18nKey="url" defaults="URL" />
+ </FormLabel>
+ <FormControl>
+ <Input type="url" {...field} placeholder="https://" />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="locationText"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel className="flex items-center gap-1.5">
+ <Trans
+ i18nKey="locationDisplayText"
+ defaults="Display text"
+ />
+ <span className="text-muted-foreground">
+ <Trans i18nKey="optionalLabel" defaults="(Optional)" />
+ </span>
+ </FormLabel>
+ <FormControl>
+ <Input
+ {...field}
+ placeholder={t("locationDisplayTextPlaceholder", {
+ defaultValue: "Meeting link",
+ })}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </div>
+ ) : null}
+
+ {showMaxAttendees ? (
+ <FormField
+ control={form.control}
+ name="maxAttendees"
+ render={({ field }) => (
+ <FormItem>
+ <div className="flex items-center justify-between">
+ <FormLabel>
+ <Trans i18nKey="maxAttendees" defaults="Max attendees" />
+ </FormLabel>
+ <RemoveOptionalButton onClick={handleRemoveMaxAttendees} />
+ </div>
+ <FormControl>
+ <Input type="number" min={1} step={1} {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ ) : null}
+
+ {showDescription ? (
+ <FormField
+ control={form.control}
+ name="description"
+ render={({ field }) => (
+ <FormItem>
+ <div className="flex items-center justify-between">
+ <FormLabel>
+ <Trans i18nKey="description" defaults="Description" />
+ </FormLabel>
+ <RemoveOptionalButton onClick={handleRemoveDescription} />
+ </div>
+ <FormControl>
+ <Textarea
+ {...field}
+ rows={3}
+ placeholder={t("eventTypeDescriptionPlaceholder", {
+ defaultValue: "What is this event about?",
+ })}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ ) : null}
+
+ {!showMaxAttendees || !showDescription ? (
+ <div className="flex flex-wrap items-center gap-1">
+ {!showMaxAttendees ? (
+ <Button
+ type="button"
+ variant="ghost"
+ size="sm"
+ onClick={() => {
+ setShowMaxAttendees(true);
+ form.setValue("maxAttendees", "1");
+ }}
+ className="text-muted-foreground"
+ >
+ <PlusIcon data-icon="inline-start" />
+ <Trans i18nKey="setMaxAttendees" defaults="Set max attendees" />
+ </Button>
+ ) : null}
+ {!showDescription ? (
+ <Button
+ type="button"
+ variant="ghost"
+ size="sm"
+ onClick={() => setShowDescription(true)}
+ className="text-muted-foreground"
+ >
+ <PlusIcon data-icon="inline-start" />
+ <Trans i18nKey="addDescription" defaults="Add description" />
+ </Button>
+ ) : null}
+ </div>
+ ) : null}
+ </>
+ );
+}
+
+function RadioCardOption({
+ value,
+ icon,
+ label,
+}: {
+ value: string;
+ icon: React.ReactNode;
+ label: string;
+}) {
+ const id = React.useId();
+ return (
+ <label
+ htmlFor={id}
+ className="group flex cursor-pointer items-center gap-2.5 rounded-lg border border-input bg-card px-3 py-2.5 text-sm transition-colors hover:bg-accent has-data-[state=checked]:border-primary has-data-[state=checked]:bg-primary/5"
+ >
+ <RadioGroupItem id={id} value={value} />
+ <span className="flex items-center gap-1.5 font-medium text-foreground">
+ <span className="text-muted-foreground group-has-data-[state=checked]:text-primary">
+ {icon}
+ </span>
+ {label}
+ </span>
+ </label>
+ );
+}
+
+function RemoveOptionalButton({ onClick }: { onClick: () => void }) {
+ return (
+ <button
+ type="button"
+ onClick={onClick}
+ className="font-normal text-muted-foreground text-sm underline-offset-2 hover:text-foreground hover:underline"
+ >
+ <Trans i18nKey="remove" defaults="Remove" />
+ </button>
+ );
+}