import type { Client, CreateClientData } from '@/admin/api/clients'; import { clientsApi } from '@/admin/api/clients'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Spinner } from '@/components/ui/spinner'; import { useForm } from '@tanstack/react-form'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { dateI18n, getSettings } from '@wordpress/date'; import { __ } from '@wordpress/i18n'; import { useEffect, useState } from 'react'; import { toast } from 'sonner'; const TIMEZONES = __ALLCOACH_ADMIN__.timezones.map((tz) => ({ value: tz.value, label: tz.text, })); const DEFAULT_VALUES = { firstName: '', lastName: '', email: '', phone: '', timezone: '', }; interface ClientDialogProps { trigger?: React.ReactNode; editTarget?: Client | null; } const ClientDialog = ({ trigger, editTarget = null }: ClientDialogProps) => { const [open, setOpen] = useState(false); const queryClient = useQueryClient(); const form = useForm({ defaultValues: DEFAULT_VALUES }); useEffect(() => { if (open && editTarget) { form.setFieldValue('firstName', editTarget.first_name); form.setFieldValue('lastName', editTarget.last_name); form.setFieldValue('email', editTarget.email); form.setFieldValue('phone', editTarget.phone ?? ''); form.setFieldValue('timezone', editTarget.timezone ?? ''); } else if (open) { form.reset(); } }, [open, editTarget]); const buildPayload = (): CreateClientData => { const v = form.state.values; return { first_name: v.firstName.trim(), last_name: v.lastName.trim(), email: v.email.trim(), phone: v.phone.trim() || undefined, timezone: v.timezone || undefined, }; }; const saveClient = useMutation({ mutationFn: () => editTarget ? clientsApi.update(editTarget.id, buildPayload()) : clientsApi.create(buildPayload()), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['clients'] }); toast.success( editTarget ? __('Client updated.', 'allcoach') : __('Client added.', 'allcoach'), ); setOpen(false); }, onError: () => { toast.error(__('Failed to save client. Please try again.', 'allcoach')); }, }); return ( {trigger && {trigger}} {editTarget ? __('Edit Client', 'allcoach') : __('Add Client', 'allcoach')} {editTarget && (

{dateI18n(getSettings().formats.date, editTarget.created_at)}{' '} {dateI18n(getSettings().formats.time, editTarget.created_at)}

)}
{/* First Name + Last Name */}
{(field) => (
field.handleChange(e.target.value)} onBlur={field.handleBlur} placeholder={__('Jane', 'allcoach')} />
)}
{(field) => (
field.handleChange(e.target.value)} onBlur={field.handleBlur} placeholder={__('Doe', 'allcoach')} />
)}
{/* Email */} {(field) => (
field.handleChange(e.target.value)} onBlur={field.handleBlur} placeholder={__('jane@example.com', 'allcoach')} />
)}
{/* Phone */} {(field) => (
field.handleChange(e.target.value)} onBlur={field.handleBlur} placeholder={__('+ 1 555 000 0000', 'allcoach')} />
)}
{(field) => (
)}
s.values}> {(values) => ( )}
); }; export default ClientDialog;