Skip to content

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

3.4 KB
Raw Blame
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { mutationOptions } from "@next-safe-action/adapter-tanstack-query";
import { Button } from "@rallly/ui/button";
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@rallly/ui/dialog";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@rallly/ui/form";
import { Textarea } from "@rallly/ui/textarea";
import { useMutation } from "@tanstack/react-query";
import { useForm } from "react-hook-form";
import * as z from "zod";
import { Trans, useTranslation } from "@/i18n/client";
import { banUserAction } from "../actions";

const useSchema = () => {
  const { t } = useTranslation();
  return z.object({
    reason: z
      .string()
      .trim()
      .max(500, {
        error: t("banUserReasonTooLong", {
          defaultValue: "Reason must be 500 characters or fewer",
        }),
      }),
  });
};

export function BanUserDialog({
  userId,
  email,
  open,
  onOpenChange,
}: {
  userId: string;
  email: string;
  open: boolean;
  onOpenChange: (open: boolean) => void;
}) {
  const schema = useSchema();
  const form = useForm({
    resolver: zodResolver(schema),
    defaultValues: {
      reason: "",
    },
  });

  const banUser = useMutation(
    mutationOptions(banUserAction, {
      onSuccess: () => {
        onOpenChange(false);
      },
    }),
  );

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>
            <Trans i18nKey="banUserTitle" defaults="Ban user" />
          </DialogTitle>
          <DialogDescription>
            <Trans
              i18nKey="banUserDesc"
              defaults="Are you sure you want to ban <b>{email}</b>? They will be signed out and won't be able to log in."
              values={{ email }}
              components={{
                b: <b className="whitespace-nowrap font-normal" />,
              }}
            />
          </DialogDescription>
        </DialogHeader>
        <Form {...form}>
          <form
            onSubmit={form.handleSubmit((data) => {
              banUser.mutate({
                userId,
                reason: data.reason || undefined,
              });
            })}
          >
            <FormField
              control={form.control}
              name="reason"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>
                    <Trans
                      i18nKey="banUserReason"
                      defaults="Reason (optional)"
                    />
                  </FormLabel>
                  <FormControl>
                    <Textarea {...field} />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
            <DialogFooter>
              <DialogClose render={<Button />}>
                <Trans i18nKey="cancel" defaults="Cancel" />
              </DialogClose>
              <Button
                variant="destructive"
                loading={banUser.isPending}
                type="submit"
              >
                <Trans i18nKey="banUser" defaults="Ban user" />
              </Button>
            </DialogFooter>
          </form>
        </Form>
      </DialogContent>
    </Dialog>
  );
}