118 lines
3.0 KiB
TypeScript
118 lines
3.0 KiB
TypeScript
"use client";
|
|
|
|
import type React from "react";
|
|
|
|
import { useState } from "react";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/app/_components/ui/dialog";
|
|
import { Button } from "@/app/_components/ui/button";
|
|
import { Label } from "@/app/_components/ui/label";
|
|
import { Input } from "@/app/_components/ui/input";
|
|
import { Textarea } from "@/app/_components/ui/textarea";
|
|
import { useMutation } from "@tanstack/react-query";
|
|
import { inviteUser } from "@/app/(protected)/(admin)/dashboard/user-management/action";
|
|
import { toast } from "sonner";
|
|
|
|
interface InviteUserDialogProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
onUserInvited: () => void;
|
|
}
|
|
|
|
export function InviteUserDialog({
|
|
open,
|
|
onOpenChange,
|
|
onUserInvited,
|
|
}: InviteUserDialogProps) {
|
|
const [formData, setFormData] = useState({
|
|
email: "",
|
|
metadata: "{}",
|
|
});
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const handleInputChange = (
|
|
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
|
|
) => {
|
|
const { name, value } = e.target;
|
|
setFormData((prev) => ({ ...prev, [name]: value }));
|
|
};
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setIsLoading(true);
|
|
|
|
let metadata = {};
|
|
try {
|
|
metadata = JSON.parse(formData.metadata);
|
|
} catch (error) {
|
|
toast.error("Invalid JSON. Please check your metadata format.");
|
|
setIsLoading(false);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await inviteUser({
|
|
email: formData.email,
|
|
});
|
|
toast.success("Invitation sent");
|
|
onUserInvited();
|
|
onOpenChange(false);
|
|
setFormData({
|
|
email: "",
|
|
metadata: "{}",
|
|
});
|
|
} catch (error) {
|
|
toast.error("Failed to send invitation");
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>Invite User</DialogTitle>
|
|
<DialogDescription>
|
|
Send an invitation email to a new user.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<div className="space-y-2">
|
|
<Label htmlFor="invite-email">Email *</Label>
|
|
<Input
|
|
id="invite-email"
|
|
name="email"
|
|
type="email"
|
|
required
|
|
value={formData.email}
|
|
onChange={handleInputChange}
|
|
placeholder="example@gmail.com"
|
|
/>
|
|
</div>
|
|
|
|
<DialogFooter>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => onOpenChange(false)}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" disabled={isLoading}>
|
|
{isLoading ? "Sending..." : "Send Invitation"}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|