Skip to content

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

2.9 KB
Raw Blame
"use client";

import { zodResolver } from "@hookform/resolvers/zod";
import { mutationOptions } from "@next-safe-action/adapter-tanstack-query";
import { passwordManagerIgnoreProps } from "@rallly/ui";
import { Button } from "@rallly/ui/button";
import type { DialogProps } from "@rallly/ui/dialog";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@rallly/ui/dialog";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@rallly/ui/form";
import { Input } from "@rallly/ui/input";
import { useMutation } from "@tanstack/react-query";
import { useForm } from "react-hook-form";
import { createSpaceAction } from "@/features/space/actions";
import { createSpaceSchema } from "@/features/space/schema";
import { Trans } from "@/i18n/client";

export function CreateSpaceDialog(props: DialogProps) {
  const form = useForm({
    resolver: zodResolver(createSpaceSchema),
    defaultValues: {
      name: "",
    },
  });

  const createSpace = useMutation(mutationOptions(createSpaceAction));

  return (
    <Dialog {...props}>
      <Form {...form}>
        <DialogContent size="sm">
          <DialogHeader>
            <DialogTitle>
              <Trans i18nKey="createSpace" defaults="Create space" />
            </DialogTitle>
            <DialogDescription>
              <Trans
                i18nKey="createSpaceDescription"
                defaults="Create a new space to organize your polls and events."
              />
            </DialogDescription>
          </DialogHeader>
          <form
            onSubmit={form.handleSubmit(async ({ name }) => {
              try {
                await createSpace.mutateAsync({ name });
              } catch {
                // The mutation cache toasts the error
                return;
              }
              props.onOpenChange?.(false);
              form.reset();
            })}
          >
            <FormField
              control={form.control}
              name="name"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>
                    <Trans i18nKey="name" defaults="Name" />
                  </FormLabel>
                  <FormControl>
                    <Input
                      {...passwordManagerIgnoreProps}
                      placeholder="e.g. Acme Corp"
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
            <DialogFooter>
              <Button
                loading={form.formState.isSubmitting}
                type="submit"
                variant="primary"
              >
                <Trans i18nKey="createSpace" defaults="Create space" />
              </Button>
            </DialogFooter>
          </form>
        </DialogContent>
      </Form>
    </Dialog>
  );
}