import React, { useEffect, useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { ArrowLeft, Bike } from 'lucide-react'; import { Alert, Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator, Button, Field, Input, LoadingState, PageHeader, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, Stack, Surface, } from '@/components/ds'; import { useAppLocations, useMiler, useTenants, useUpdateMiler } from '@/lib/doormileHooks'; /** * Edit a rider. * * Reached from the Riders table, which passes the row along in navigation * state; the record is then re-fetched by `milerprofileid`, because every * `/admin/milers/:id` route keys on that and 404s on the `userid` the same row * also carries. * * Only identity, contact, client and zone are writable. `PUT /admin/milers/:id` * documents no fields for bank details, vehicle settings or shift times, so * those are not offered here rather than shown as inputs that quietly discard * what is typed into them. A miler has a single `displayname` — there is no * first/last split anywhere on this resource. */ const NO_TENANT = 'none'; export default function EditRider() { const navigate = useNavigate(); const location = useLocation(); const passed = location.state?.rider; const milerProfileId = passed?.milerprofileid; const { data: rider, isLoading } = useMiler(milerProfileId); const { data: tenants = [] } = useTenants(); const { data: locations = [] } = useAppLocations(); const updateMiler = useUpdateMiler(); const [form, setForm] = useState(null); /* Seeded from the row that was clicked so the fields are populated on the first paint, then reconciled with the fetched record when it lands. */ useEffect(() => { const source = rider || passed; if (!source) return; setForm({ displayname: source.displayname || source.authname || '', phone: source.phone || source.contactno || '', email: source.email || '', tenantid: source.tenantid ?? '', applocationid: source.applocationid ?? '', }); }, [rider, passed]); const set = (key) => (value) => setForm((current) => ({ ...current, [key]: value })); if (!milerProfileId) { return ( navigate('/doormile/milers')}>Go to Milers} > Open a miler from the Milers table — this page needs the record that was clicked. ); } const submit = (event) => { event.preventDefault(); updateMiler.mutate( { id: milerProfileId, data: { displayname: form.displayname, contactno: form.phone, email: form.email, tenantid: form.tenantid === '' ? undefined : Number(form.tenantid), applocationid: form.applocationid === '' ? undefined : Number(form.applocationid), }, }, { onSuccess: (res) => res?.success !== false && navigate('/doormile/milers') } ); }; return ( Milers Edit } actions={ } /> {isLoading && !form ? ( ) : (
set('displayname')(e.target.value)} /> set('phone')(e.target.value.replace(/\D/g, '').slice(0, 10))} inputMode="numeric" /> set('email')(e.target.value)} />
)}
); }