mirror of
https://github.com/hanzo-apps/dataroom.git
synced 2026-10-30 14:47:36 +00:00
122 lines
3.2 KiB
TypeScript
122 lines
3.2 KiB
TypeScript
import { useState } from "react";
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useTeam } from "@/context/team-context";
|
|
import { toast } from "sonner";
|
|
import { mutate } from "swr";
|
|
import { z } from "zod";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from "@/components/ui/dialog";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
import { useAnalytics } from "@/lib/analytics";
|
|
|
|
export function AddTeamMembers({
|
|
open,
|
|
setOpen,
|
|
children,
|
|
}: {
|
|
open: boolean;
|
|
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
|
|
children?: React.ReactNode;
|
|
}) {
|
|
const [email, setEmail] = useState<string>("");
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
const teamInfo = useTeam();
|
|
const teamId = teamInfo?.currentTeam?.id;
|
|
const analytics = useAnalytics();
|
|
const router = useRouter();
|
|
const emailSchema = z
|
|
.string()
|
|
.trim()
|
|
.toLowerCase()
|
|
.min(3, { message: "Please enter a valid email." })
|
|
.email({ message: "Please enter a valid email." });
|
|
|
|
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
|
|
const validation = emailSchema.safeParse(email);
|
|
if (!validation.success) {
|
|
toast.error(validation.error.errors[0].message);
|
|
return;
|
|
}
|
|
|
|
setLoading(true);
|
|
const response = await fetch(`/api/teams/${teamId}/invite`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
email: validation.data,
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
setLoading(false);
|
|
setOpen(false);
|
|
toast.error(error);
|
|
return;
|
|
}
|
|
|
|
analytics.capture("Team Member Invitation Sent", {
|
|
email: validation.data,
|
|
teamId: teamId,
|
|
});
|
|
|
|
mutate(`/api/teams/${teamId}/invitations`);
|
|
mutate(`/api/teams/${teamId}/limits`);
|
|
|
|
toast.success("An invitation email has been sent!");
|
|
setOpen(false);
|
|
setLoading(false);
|
|
|
|
// Redirect to team members page
|
|
router.push("/settings/people");
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
<DialogTrigger asChild>{children}</DialogTrigger>
|
|
<DialogContent className="sm:max-w-[425px]">
|
|
<DialogHeader className="text-start">
|
|
<DialogTitle>Add Member</DialogTitle>
|
|
<DialogDescription>
|
|
You can easily add team members.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<form onSubmit={handleSubmit}>
|
|
<Label htmlFor="domain" className="opacity-80">
|
|
Email
|
|
</Label>
|
|
<Input
|
|
id="email"
|
|
placeholder="[email protected]"
|
|
className="mb-8 mt-1 w-full"
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
/>
|
|
|
|
<DialogFooter>
|
|
<Button type="submit" className="h-9 w-full">
|
|
{loading ? "Sending email..." : "Add member"}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|