// CreateUserDialog - Multi-step form for creating new users import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Checkbox } from '@/components/ui/checkbox'; import { Progress } from '@/components/ui/progress'; import { User, Mail, Phone, KeyRound, Globe, Tag, ChevronLeft, ChevronRight, Loader2, } from 'lucide-react'; import { createUserSchema, type CreateUserInput } from '@/lib/validations/user'; import { mockTags } from '../data/mockUserCrmData'; import { toast } from 'sonner'; import { cn } from '@/lib/utils'; interface CreateUserDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onUserCreated?: () => void; } const steps = [ { id: 1, title: 'Basic Info', icon: User }, { id: 2, title: 'Security', icon: KeyRound }, { id: 3, title: 'Preferences', icon: Globe }, { id: 4, title: 'Tags', icon: Tag }, ]; const countryCodes = [ { code: '+91', country: 'India' }, { code: '+1', country: 'USA' }, { code: '+44', country: 'UK' }, { code: '+971', country: 'UAE' }, { code: '+65', country: 'Singapore' }, ]; const languages = [ { code: 'en', name: 'English' }, { code: 'hi', name: 'Hindi' }, { code: 'ta', name: 'Tamil' }, { code: 'te', name: 'Telugu' }, { code: 'mr', name: 'Marathi' }, ]; const timezones = [ { code: 'Asia/Kolkata', name: 'India (IST)' }, { code: 'America/New_York', name: 'Eastern Time (ET)' }, { code: 'Europe/London', name: 'London (GMT)' }, { code: 'Asia/Dubai', name: 'Dubai (GST)' }, { code: 'Asia/Singapore', name: 'Singapore (SGT)' }, ]; const currencies = [ { code: 'INR', symbol: '₹', name: 'Indian Rupee' }, { code: 'USD', symbol: '$', name: 'US Dollar' }, { code: 'GBP', symbol: '£', name: 'British Pound' }, { code: 'AED', symbol: 'د.إ', name: 'UAE Dirham' }, { code: 'SGD', symbol: 'S$', name: 'Singapore Dollar' }, ]; export function CreateUserDialog({ open, onOpenChange, onUserCreated }: CreateUserDialogProps) { const [currentStep, setCurrentStep] = useState(1); const [isSubmitting, setIsSubmitting] = useState(false); const [selectedTags, setSelectedTags] = useState([]); const form = useForm({ resolver: zodResolver(createUserSchema), defaultValues: { name: '', email: '', phone: '', countryCode: '+91', password: '', role: 'User', language: 'en', timezone: 'Asia/Kolkata', currency: 'INR', tagIds: [], }, }); const handleNext = async () => { let isValid = true; if (currentStep === 1) { isValid = await form.trigger(['name', 'email', 'phone', 'countryCode']); } else if (currentStep === 2) { isValid = await form.trigger(['password', 'role']); } else if (currentStep === 3) { isValid = await form.trigger(['language', 'timezone', 'currency']); } if (isValid && currentStep < 4) { setCurrentStep(currentStep + 1); } }; const handleBack = () => { if (currentStep > 1) { setCurrentStep(currentStep - 1); } }; const handleTagToggle = (tagId: string) => { const updated = selectedTags.includes(tagId) ? selectedTags.filter((t) => t !== tagId) : [...selectedTags, tagId]; setSelectedTags(updated); form.setValue('tagIds', updated); }; const onSubmit = async (data: CreateUserInput) => { setIsSubmitting(true); try { // Simulate API call await new Promise((resolve) => setTimeout(resolve, 1500)); toast.success(`User "${data.name}" created successfully!`); onOpenChange(false); onUserCreated?.(); form.reset(); setCurrentStep(1); setSelectedTags([]); } catch (error) { toast.error('Failed to create user'); } finally { setIsSubmitting(false); } }; const handleClose = () => { if (!isSubmitting) { onOpenChange(false); form.reset(); setCurrentStep(1); setSelectedTags([]); } }; const progress = (currentStep / 4) * 100; return ( Create New User Add a new user to the platform. Step {currentStep} of 4. {/* Progress Bar */}
{steps.map((step) => (
= step.id ? 'text-primary' : 'text-muted-foreground' )} > {step.title}
))}
{/* Step 1: Basic Info */} {currentStep === 1 && (
( Full Name )} /> ( Email Address
)} />
( Code )} /> ( Phone Number
)} />
)} {/* Step 2: Security */} {currentStep === 2 && (
( Password (Optional)
If left blank, user will receive a magic link to set password.
)} /> ( Role )} />
)} {/* Step 3: Preferences */} {currentStep === 3 && (
( Language )} /> ( Timezone )} /> ( Currency )} />
)} {/* Step 4: Tags */} {currentStep === 4 && (
Tags (Optional) Add labels to help segment this user.
{mockTags.map((tag) => ( handleTagToggle(tag.id)} className={cn( 'cursor-pointer transition-all', selectedTags.includes(tag.id) ? tag.color : 'bg-secondary/50 text-muted-foreground hover:bg-secondary' )} > {tag.name} ))}
)} {currentStep < 4 ? ( ) : ( )}
); }