"use client"; import { useState, useEffect } from "react"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { format } from "date-fns"; import { CalendarIcon, X } from "lucide-react"; import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle, SheetFooter, SheetClose, } from "@/app/_components/ui/sheet"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/app/_components/ui/form"; import { Input } from "@/app/_components/ui/input"; import { Button } from "@/app/_components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/app/_components/ui/select"; import { Textarea } from "@/app/_components/ui/textarea"; import { Avatar, AvatarFallback, AvatarImage, } from "@/app/_components/ui/avatar"; import { Calendar } from "@/app/_components/ui/calendar"; import { Popover, PopoverContent, PopoverTrigger, } from "@/app/_components/ui/popover"; import { Separator } from "@/app/_components/ui/separator"; import { Tabs, TabsContent, TabsList, TabsTrigger, } from "@/app/_components/ui/tabs"; import { User } from "./users-table"; // Create a schema for form validation const userFormSchema = z.object({ // User fields email: z.string().email({ message: "Please enter a valid email address" }), firstName: z.string().min(1, { message: "First name is required" }), lastName: z.string().min(1, { message: "Last name is required" }), avatar: z.string().optional(), role: z.enum(["admin", "staff", "user"]), password: z .string() .min(8, { message: "Password must be at least 8 characters long" }) .optional(), // Profile fields bio: z.string().optional(), phone: z.string().optional(), address: z.string().optional(), city: z.string().optional(), country: z.string().optional(), birthDate: z.date().optional(), }); type UserFormValues = z.infer; // Extended user type to include profile information type UserWithProfile = User & { profile?: { bio?: string; phone?: string; address?: string; city?: string; country?: string; birthDate?: Date; }; }; interface EditUserSheetProps { open: boolean; onOpenChange: (open: boolean) => void; user?: UserWithProfile | null; onSave: (user: UserWithProfile) => void; isNew?: boolean; } export function EditUserSheet({ open, onOpenChange, user, onSave, isNew = false, }: EditUserSheetProps) { const form = useForm({ resolver: zodResolver(userFormSchema), defaultValues: { email: "", firstName: "", lastName: "", avatar: "/placeholder.svg?height=40&width=40", role: "user", password: isNew ? "" : undefined, bio: "", phone: "", address: "", city: "", country: "", birthDate: undefined, }, }); // Update form when user changes useEffect(() => { if (user) { const birthDate = user.profile?.birthDate ? new Date(user.profile.birthDate) : undefined; form.reset({ email: user.email, firstName: user.firstName, lastName: user.lastName, avatar: user.avatar, role: user.role, // Don't populate password for existing users bio: user.profile?.bio || "", phone: user.profile?.phone || "", address: user.profile?.address || "", city: user.profile?.city || "", country: user.profile?.country || "", birthDate: birthDate, }); } else if (isNew) { form.reset({ email: "", firstName: "", lastName: "", avatar: "/placeholder.svg?height=40&width=40", role: "user", password: "", bio: "", phone: "", address: "", city: "", country: "", birthDate: undefined, }); } }, [user, isNew, form]); const onSubmit = (data: UserFormValues) => { // Prepare the user object with profile const updatedUser: UserWithProfile = { id: user?.id || "new-id", // In a real app, this would be handled by the backend email: data.email, firstName: data.firstName || "", lastName: data.lastName || "", avatar: data.avatar || "/placeholder.svg?height=40&width=40", role: data.role, status: user?.status || "active", lastSignedIn: user?.lastSignedIn || new Date().toISOString().split("T")[0], profile: { bio: data.bio, phone: data.phone, address: data.address, city: data.city, country: data.country, birthDate: data.birthDate, }, }; // Save the user onSave(updatedUser); // Close the sheet onOpenChange(false); }; return ( {isNew ? "Add New User" : "Edit User"} {isNew ? "Fill in the details to create a new user account." : "Make changes to the user profile here."}
User Details Profile Information
{form.watch("firstName")?.charAt(0) || ""} {form.watch("lastName")?.charAt(0) || ""}
( First Name )} /> ( Last Name )} />
( Email )} /> {isNew && ( ( Password Minimum 8 characters )} /> )} ( Role )} /> ( Avatar URL )} />
( Bio