updates on the ui changes and changed into doormile
This commit is contained in:
234
src/pages/doormile/riders/CreateRider.jsx
Normal file
234
src/pages/doormile/riders/CreateRider.jsx
Normal file
@@ -0,0 +1,234 @@
|
||||
import React, { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { ArrowLeft, Bike } from 'lucide-react';
|
||||
import {
|
||||
Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage,
|
||||
BreadcrumbSeparator, Button, Field, Input, PageHeader, Select, SelectContent,
|
||||
SelectItem, SelectTrigger, SelectValue, Stack, Surface,
|
||||
} from '@/components/ds';
|
||||
import { useAppLocations, useCreateMiler, useHubs, useTenants } from '@/lib/doormileHooks';
|
||||
|
||||
/**
|
||||
* Create a rider — a miler-app login plus the zone and hub they work out of.
|
||||
*
|
||||
* `configid` is deliberately not sent: the server defaults it to the miler-app
|
||||
* login partition, and overriding it creates an account the rider app cannot
|
||||
* sign in to.
|
||||
*/
|
||||
|
||||
const VEHICLE_TYPES = ['Bike', 'Scooter', 'Bicycle', 'Car', 'Van'];
|
||||
|
||||
const NO_HUB = 'none';
|
||||
|
||||
const EMPTY_FORM = {
|
||||
displayname: '',
|
||||
authname: '',
|
||||
email: '',
|
||||
contactno: '',
|
||||
password: '',
|
||||
defaultvehicletype: 'Bike',
|
||||
tenantid: '',
|
||||
applocationid: '',
|
||||
hubid: '',
|
||||
};
|
||||
|
||||
export default function CreateRider() {
|
||||
const navigate = useNavigate();
|
||||
|
||||
/* Doormile staff manage riders across every client and must choose one; a
|
||||
client login is scoped to its own tenant server-side, so the picker would
|
||||
only offer the answer the server already knows. */
|
||||
const loginTenantId = localStorage.getItem('tenantid');
|
||||
const isStaff = !loginTenantId || loginTenantId === '0';
|
||||
|
||||
const { data: tenants = [] } = useTenants({ enabled: isStaff });
|
||||
const { data: hubs = [] } = useHubs();
|
||||
const { data: locations = [] } = useAppLocations();
|
||||
|
||||
const [form, setForm] = useState(EMPTY_FORM);
|
||||
const [errors, setErrors] = useState({});
|
||||
const createMiler = useCreateMiler();
|
||||
|
||||
const set = (key) => (value) => setForm((current) => ({ ...current, [key]: value }));
|
||||
|
||||
const submit = (event) => {
|
||||
event.preventDefault();
|
||||
|
||||
const nextErrors = {};
|
||||
if (!form.displayname.trim()) nextErrors.displayname = 'Enter the name shown to operators';
|
||||
if (!form.authname.trim()) nextErrors.authname = 'Enter the login name for the rider app';
|
||||
if (!/^\d{10}$/.test(form.contactno)) nextErrors.contactno = 'Enter a 10-digit mobile number';
|
||||
if (!form.email.trim()) nextErrors.email = 'Enter an email address';
|
||||
if (!form.password) nextErrors.password = 'Set a password';
|
||||
if (isStaff && !form.tenantid) nextErrors.tenantid = 'Choose the client this rider works for';
|
||||
if (!form.applocationid) nextErrors.applocationid = 'Choose the city';
|
||||
setErrors(nextErrors);
|
||||
if (Object.keys(nextErrors).length) return;
|
||||
|
||||
createMiler.mutate(
|
||||
{
|
||||
authname: form.authname,
|
||||
displayname: form.displayname,
|
||||
email: form.email,
|
||||
contactno: form.contactno,
|
||||
password: form.password,
|
||||
tenantid: isStaff ? Number(form.tenantid) : Number(loginTenantId),
|
||||
defaultvehicletype: form.defaultvehicletype,
|
||||
applocationid: Number(form.applocationid),
|
||||
hubid: form.hubid ? Number(form.hubid) : undefined,
|
||||
},
|
||||
{ onSuccess: (res) => res?.success !== false && navigate('/doormile/riders') }
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<Stack space="lg">
|
||||
<PageHeader
|
||||
icon={Bike}
|
||||
title="New rider"
|
||||
subtitle="Creates a miler-app login and puts them on a zone"
|
||||
breadcrumb={
|
||||
<Breadcrumb>
|
||||
<BreadcrumbList>
|
||||
<BreadcrumbItem>
|
||||
<BreadcrumbLink href="/doormile/riders">Riders</BreadcrumbLink>
|
||||
</BreadcrumbItem>
|
||||
<BreadcrumbSeparator />
|
||||
<BreadcrumbItem>
|
||||
<BreadcrumbPage>New rider</BreadcrumbPage>
|
||||
</BreadcrumbItem>
|
||||
</BreadcrumbList>
|
||||
</Breadcrumb>
|
||||
}
|
||||
actions={
|
||||
<Button variant="outline" onClick={() => navigate('/doormile/riders')}>
|
||||
<ArrowLeft className="mr-1.5 h-4 w-4" /> Back
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
|
||||
<form onSubmit={submit}>
|
||||
<Surface radius="xl" padding="lg">
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
|
||||
<Field label="Display name" required error={errors.displayname}>
|
||||
<Input
|
||||
value={form.displayname}
|
||||
onChange={(e) => set('displayname')(e.target.value)}
|
||||
placeholder="Murali S"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="Login name" required error={errors.authname} hint="What the rider types into the app">
|
||||
<Input value={form.authname} onChange={(e) => set('authname')(e.target.value)} placeholder="murali.s" />
|
||||
</Field>
|
||||
|
||||
<Field label="Mobile number" required error={errors.contactno}>
|
||||
<Input
|
||||
value={form.contactno}
|
||||
onChange={(e) => set('contactno')(e.target.value.replace(/\D/g, '').slice(0, 10))}
|
||||
placeholder="9876543210"
|
||||
inputMode="numeric"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="Email" required error={errors.email}>
|
||||
<Input
|
||||
type="email"
|
||||
value={form.email}
|
||||
onChange={(e) => set('email')(e.target.value)}
|
||||
placeholder="murali@example.com"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="Password" required error={errors.password}>
|
||||
<Input
|
||||
type="password"
|
||||
autoComplete="new-password"
|
||||
value={form.password}
|
||||
onChange={(e) => set('password')(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="Default vehicle">
|
||||
<Select value={form.defaultvehicletype} onValueChange={set('defaultvehicletype')}>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{VEHICLE_TYPES.map((type) => (
|
||||
<SelectItem key={type} value={type}>
|
||||
{type}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
{isStaff && (
|
||||
<Field label="Client" required error={errors.tenantid}>
|
||||
<Select value={String(form.tenantid)} onValueChange={set('tenantid')}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Choose a client" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{(tenants || []).map((tenant) => (
|
||||
<SelectItem key={tenant.tenantid} value={String(tenant.tenantid)}>
|
||||
{tenant.tenantname || `Tenant #${tenant.tenantid}`}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
)}
|
||||
|
||||
<Field label="City" required error={errors.applocationid}>
|
||||
<Select value={String(form.applocationid)} onValueChange={set('applocationid')}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Choose a city" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{(locations || [])
|
||||
.filter((location) => location.applocationid)
|
||||
.map((location) => (
|
||||
<SelectItem key={location.applocationid} value={String(location.applocationid)}>
|
||||
{location.locationname || `Location #${location.applocationid}`}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
<Field label="Hub" hint="Optional — where the rider starts their shift">
|
||||
<Select
|
||||
value={form.hubid === '' ? NO_HUB : String(form.hubid)}
|
||||
onValueChange={(value) => set('hubid')(value === NO_HUB ? '' : value)}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="No hub" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={NO_HUB}>No hub</SelectItem>
|
||||
{(hubs || []).map((hub) => (
|
||||
<SelectItem key={hub.hubid} value={String(hub.hubid)}>
|
||||
{hub.hubname || `Hub #${hub.hubid}`}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 flex justify-end gap-2 border-t border-border pt-5">
|
||||
<Button type="button" variant="ghost" onClick={() => navigate('/doormile/riders')}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" loading={createMiler.isPending}>
|
||||
Create rider
|
||||
</Button>
|
||||
</div>
|
||||
</Surface>
|
||||
</form>
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
191
src/pages/doormile/riders/EditRider.jsx
Normal file
191
src/pages/doormile/riders/EditRider.jsx
Normal file
@@ -0,0 +1,191 @@
|
||||
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 (
|
||||
<Stack space="lg">
|
||||
<PageHeader icon={Bike} title="Edit rider" />
|
||||
<Alert
|
||||
tone="warning"
|
||||
title="No rider selected"
|
||||
action={<Button onClick={() => navigate('/doormile/riders')}>Go to Riders</Button>}
|
||||
>
|
||||
Open a rider from the Riders table — this page needs the record that was clicked.
|
||||
</Alert>
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
|
||||
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/riders') }
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<Stack space="lg">
|
||||
<PageHeader
|
||||
icon={Bike}
|
||||
title={form?.displayname || 'Edit rider'}
|
||||
subtitle="Identity, contact, client and zone"
|
||||
breadcrumb={
|
||||
<Breadcrumb>
|
||||
<BreadcrumbList>
|
||||
<BreadcrumbItem>
|
||||
<BreadcrumbLink href="/doormile/riders">Riders</BreadcrumbLink>
|
||||
</BreadcrumbItem>
|
||||
<BreadcrumbSeparator />
|
||||
<BreadcrumbItem>
|
||||
<BreadcrumbPage>Edit</BreadcrumbPage>
|
||||
</BreadcrumbItem>
|
||||
</BreadcrumbList>
|
||||
</Breadcrumb>
|
||||
}
|
||||
actions={
|
||||
<Button variant="outline" onClick={() => navigate('/doormile/riders')}>
|
||||
<ArrowLeft className="mr-1.5 h-4 w-4" /> Back
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
|
||||
{isLoading && !form ? (
|
||||
<LoadingState message="Loading rider…" />
|
||||
) : (
|
||||
<form onSubmit={submit}>
|
||||
<Surface radius="xl" padding="lg">
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
|
||||
<Field label="Display name">
|
||||
<Input value={form?.displayname || ''} onChange={(e) => set('displayname')(e.target.value)} />
|
||||
</Field>
|
||||
|
||||
<Field label="Mobile number">
|
||||
<Input
|
||||
value={form?.phone || ''}
|
||||
onChange={(e) => set('phone')(e.target.value.replace(/\D/g, '').slice(0, 10))}
|
||||
inputMode="numeric"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="Email">
|
||||
<Input type="email" value={form?.email || ''} onChange={(e) => set('email')(e.target.value)} />
|
||||
</Field>
|
||||
|
||||
<Field label="Client">
|
||||
<Select
|
||||
value={form?.tenantid === '' ? NO_TENANT : String(form?.tenantid ?? '')}
|
||||
onValueChange={(value) => set('tenantid')(value === NO_TENANT ? '' : value)}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="No client" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={NO_TENANT}>No client</SelectItem>
|
||||
{(tenants || []).map((tenant) => (
|
||||
<SelectItem key={tenant.tenantid} value={String(tenant.tenantid)}>
|
||||
{tenant.tenantname || `Tenant #${tenant.tenantid}`}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
<Field label="City">
|
||||
<Select
|
||||
value={String(form?.applocationid ?? '')}
|
||||
onValueChange={set('applocationid')}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Choose a city" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{(locations || [])
|
||||
.filter((entry) => entry.applocationid)
|
||||
.map((entry) => (
|
||||
<SelectItem key={entry.applocationid} value={String(entry.applocationid)}>
|
||||
{entry.locationname || `Location #${entry.applocationid}`}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 flex justify-end gap-2 border-t border-border pt-5">
|
||||
<Button type="button" variant="ghost" onClick={() => navigate('/doormile/riders')}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" loading={updateMiler.isPending}>
|
||||
Save changes
|
||||
</Button>
|
||||
</div>
|
||||
</Surface>
|
||||
</form>
|
||||
)}
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
449
src/pages/doormile/riders/Riders.jsx
Normal file
449
src/pages/doormile/riders/Riders.jsx
Normal file
@@ -0,0 +1,449 @@
|
||||
import React, { useMemo, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import dayjs from 'dayjs';
|
||||
import {
|
||||
Ban, Bell, Bike, Car, CheckCircle2, CircleSlash, Pencil, Plus, Radio, Users,
|
||||
} from 'lucide-react';
|
||||
import {
|
||||
Avatar, Button, ConfirmModal, DataTable, Drawer, EmptyState, Field, Grid, IconButton,
|
||||
Input, KpiCard, Modal, PageHeader, SearchInput, Select, SelectContent, SelectItem, SelectTrigger,
|
||||
SelectValue, Stack, StatusBadge, Surface, Tabs, Textarea,
|
||||
} from '@/components/ds';
|
||||
import { ListToolbar } from '@/components/doormile/ListToolbar';
|
||||
import {
|
||||
useAssignMilerVehicle, useBlockMiler, useMilerLogs, useMilers, useNotifyMiler,
|
||||
useRiderSummaryCounts, useVehicles,
|
||||
} from '@/lib/doormileHooks';
|
||||
import { formatDoormileTimestamp } from '@/lib/doormileTimestamp';
|
||||
import { currency, matchesQuery, orDash, useDebouncedValue } from '@/lib/doormileFormat';
|
||||
|
||||
/**
|
||||
* Riders — the milers who actually carry the parcels.
|
||||
*
|
||||
* Two identifiers matter here and they are not interchangeable: `userid` is
|
||||
* what a booking's assignment records and what `assign-miler` wants, while
|
||||
* `milerprofileid` is the primary key of this resource and what block, notify,
|
||||
* assign-vehicle and logs all key on. Passing one where the other belongs 404s.
|
||||
*
|
||||
* The rider-substitution feature from the source console is deliberately absent:
|
||||
* it has no backend endpoint at all, so there is nothing here to build against.
|
||||
*/
|
||||
|
||||
/** "Active" means neither offline nor blocked — the same rule the counts use. */
|
||||
const isActive = (rider) => !['offline', 'blocked'].includes(String(rider?.availabilitystatus || '').toLowerCase());
|
||||
|
||||
const riderName = (rider) => rider?.displayname || rider?.authname || '—';
|
||||
|
||||
/** A bare `HH:mm:ss` from the shift record, rendered as a clock time. */
|
||||
const shiftTime = (value) =>
|
||||
value ? dayjs(`${dayjs().format('YYYY-MM-DD')} ${value}`).format('hh:mm A') : '—';
|
||||
|
||||
const TABS = [
|
||||
{ value: 'all', label: 'All riders' },
|
||||
{ value: 'active', label: 'Active' },
|
||||
{ value: 'blocked', label: 'Blocked' },
|
||||
];
|
||||
|
||||
export default function Riders() {
|
||||
const navigate = useNavigate();
|
||||
const { data: riders = [], isLoading, isFetching } = useMilers();
|
||||
const { data: summary } = useRiderSummaryCounts();
|
||||
const { data: vehicles = [] } = useVehicles();
|
||||
|
||||
const [search, setSearch] = useState('');
|
||||
const debouncedSearch = useDebouncedValue(search);
|
||||
const [tab, setTab] = useState('all');
|
||||
|
||||
const [blockRow, setBlockRow] = useState(null);
|
||||
const [vehicleRow, setVehicleRow] = useState(null);
|
||||
const [selectedVehicle, setSelectedVehicle] = useState('');
|
||||
const [notifyRow, setNotifyRow] = useState(null);
|
||||
const [notice, setNotice] = useState({ title: 'DoormileXpress', message: '' });
|
||||
const [logsRow, setLogsRow] = useState(null);
|
||||
|
||||
const blockMiler = useBlockMiler();
|
||||
const assignVehicle = useAssignMilerVehicle();
|
||||
const notifyMiler = useNotifyMiler();
|
||||
|
||||
const counts = useMemo(() => {
|
||||
const list = riders || [];
|
||||
return {
|
||||
all: list.length,
|
||||
active: list.filter(isActive).length,
|
||||
blocked: list.filter((rider) => String(rider.availabilitystatus || '').toLowerCase() === 'blocked').length,
|
||||
};
|
||||
}, [riders]);
|
||||
|
||||
const rows = useMemo(() => {
|
||||
let list = riders || [];
|
||||
if (tab === 'active') list = list.filter(isActive);
|
||||
else if (tab === 'blocked') {
|
||||
list = list.filter((rider) => String(rider.availabilitystatus || '').toLowerCase() === 'blocked');
|
||||
}
|
||||
return list.filter((rider) =>
|
||||
matchesQuery(rider, [riderName, 'phone', 'userid', 'city', 'suburb', 'defaultvehicletype'], debouncedSearch)
|
||||
);
|
||||
}, [riders, tab, debouncedSearch]);
|
||||
|
||||
const columns = useMemo(
|
||||
() => [
|
||||
{
|
||||
key: 'rider',
|
||||
header: 'Rider',
|
||||
sortable: true,
|
||||
accessor: riderName,
|
||||
cell: (row) => (
|
||||
<div className="flex items-center gap-2.5 min-w-0">
|
||||
<Avatar name={riderName(row)} size="sm" />
|
||||
<div className="min-w-0">
|
||||
<p className="truncate text-body-sm font-semibold text-ink-1">{riderName(row)}</p>
|
||||
<p className="text-caption text-ink-3">
|
||||
{orDash(row.phone)} · #{row.userid}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: 'address',
|
||||
header: 'Base',
|
||||
sortValue: (row) => row.suburb || row.address || '',
|
||||
cell: (row) => (
|
||||
<div className="min-w-0">
|
||||
<p className="truncate text-body-sm text-ink-2" title={row.address || ''}>
|
||||
{row.suburb || row.address || '—'}
|
||||
</p>
|
||||
{row.city ? <p className="text-caption text-ink-3">{row.city}</p> : null}
|
||||
</div>
|
||||
),
|
||||
hideBelow: 'lg',
|
||||
},
|
||||
{
|
||||
key: 'defaultvehicletype',
|
||||
header: 'Vehicle',
|
||||
sortable: true,
|
||||
accessor: (row) => orDash(row.defaultvehicletype),
|
||||
hideBelow: 'md',
|
||||
},
|
||||
{
|
||||
key: 'shift',
|
||||
header: 'Shift',
|
||||
sortValue: (row) => row.starttime || '',
|
||||
accessor: (row) => `${shiftTime(row.starttime)} – ${shiftTime(row.endtime)}`,
|
||||
hideBelow: 'lg',
|
||||
},
|
||||
{
|
||||
key: 'basefare',
|
||||
header: 'Base fare',
|
||||
sortValue: (row) => Number(row.basefare) || 0,
|
||||
accessor: (row) => currency(row.basefare),
|
||||
align: 'right',
|
||||
hideBelow: 'lg',
|
||||
},
|
||||
{
|
||||
key: 'availabilitystatus',
|
||||
header: 'Status',
|
||||
sortable: true,
|
||||
cell: (row) => <StatusBadge status={row.availabilitystatus} dot size="sm" />,
|
||||
},
|
||||
{
|
||||
key: 'actions',
|
||||
header: 'Actions',
|
||||
align: 'right',
|
||||
cell: (row) => (
|
||||
<div className="flex justify-end gap-1">
|
||||
<IconButton
|
||||
label="Edit rider"
|
||||
icon={Pencil}
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => navigate('/doormile/riders/edit', { state: { rider: row } })}
|
||||
/>
|
||||
<IconButton
|
||||
label="Assign vehicle"
|
||||
icon={Car}
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setVehicleRow(row);
|
||||
setSelectedVehicle('');
|
||||
}}
|
||||
/>
|
||||
<IconButton
|
||||
label="Send a notification"
|
||||
icon={Bell}
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setNotifyRow(row);
|
||||
setNotice({ title: 'DoormileXpress', message: '' });
|
||||
}}
|
||||
/>
|
||||
<IconButton
|
||||
label="Live logs"
|
||||
icon={Radio}
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setLogsRow(row)}
|
||||
/>
|
||||
<IconButton label="Block rider" icon={Ban} variant="ghost" size="sm" onClick={() => setBlockRow(row)} />
|
||||
</div>
|
||||
),
|
||||
},
|
||||
],
|
||||
[navigate]
|
||||
);
|
||||
|
||||
return (
|
||||
<Stack space="lg">
|
||||
<PageHeader
|
||||
icon={Bike}
|
||||
title="Riders"
|
||||
subtitle="Who is on the road, what they are on, and where they are"
|
||||
actions={
|
||||
<Button onClick={() => navigate('/doormile/riders/create')}>
|
||||
<Plus className="mr-1.5 h-4 w-4" /> New rider
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
|
||||
<Grid cols={4}>
|
||||
<KpiCard
|
||||
icon={Users}
|
||||
value={summary?.total ?? counts.all}
|
||||
label="Total riders"
|
||||
tone="brand"
|
||||
index={0}
|
||||
loading={isLoading}
|
||||
onClick={() => setTab('all')}
|
||||
/>
|
||||
<KpiCard
|
||||
icon={CheckCircle2}
|
||||
value={summary?.available ?? 0}
|
||||
label="Available now"
|
||||
tone="success"
|
||||
index={1}
|
||||
loading={isLoading}
|
||||
/>
|
||||
<KpiCard
|
||||
icon={Bike}
|
||||
value={summary?.onDelivery ?? 0}
|
||||
label="On delivery"
|
||||
tone="accent"
|
||||
index={2}
|
||||
loading={isLoading}
|
||||
/>
|
||||
<KpiCard
|
||||
icon={CircleSlash}
|
||||
value={summary?.inactive ?? 0}
|
||||
label="Offline or blocked"
|
||||
tone="neutral"
|
||||
index={3}
|
||||
loading={isLoading}
|
||||
onClick={() => setTab('blocked')}
|
||||
/>
|
||||
</Grid>
|
||||
|
||||
<Stack space="sm">
|
||||
<div className="flex items-center justify-between gap-4 w-full">
|
||||
<Tabs
|
||||
tabs={TABS.map((entry) => ({ ...entry, count: counts[entry.value] }))}
|
||||
value={tab}
|
||||
onChange={setTab}
|
||||
className="shrink-0"
|
||||
/>
|
||||
|
||||
<div className="flex items-center gap-2 shrink-0">
|
||||
<SearchInput
|
||||
value={search}
|
||||
onChange={setSearch}
|
||||
placeholder="Search by name, phone, id or city"
|
||||
className="w-72"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p className="text-caption text-ink-3">
|
||||
{rows.length === (counts[tab] ?? counts.all)
|
||||
? `${counts[tab] ?? counts.all} riders`
|
||||
: `${rows.length} of ${counts[tab] ?? counts.all} riders`}
|
||||
</p>
|
||||
|
||||
<DataTable
|
||||
columns={columns}
|
||||
rows={rows}
|
||||
getRowId={(row) => row.milerprofileid ?? row.userid}
|
||||
loading={isLoading}
|
||||
refreshing={isFetching && !isLoading}
|
||||
pageSize={10}
|
||||
defaultSort={{ key: 'rider', direction: 'asc' }}
|
||||
isFiltered={Boolean(debouncedSearch)}
|
||||
onClearFilters={() => setSearch('')}
|
||||
emptyState={
|
||||
debouncedSearch ? undefined : (
|
||||
<EmptyState
|
||||
icon={Bike}
|
||||
title="No riders here"
|
||||
description="Onboard a rider to start assigning deliveries."
|
||||
action={{ label: 'New rider', icon: Plus, onClick: () => navigate('/doormile/riders/create') }}
|
||||
/>
|
||||
)
|
||||
}
|
||||
/>
|
||||
</Stack>
|
||||
|
||||
<ConfirmModal
|
||||
open={Boolean(blockRow)}
|
||||
onOpenChange={(open) => !open && setBlockRow(null)}
|
||||
title="Block this rider?"
|
||||
description={`${riderName(blockRow || {})} will stop receiving assignments until they are unblocked.`}
|
||||
confirmLabel="Block rider"
|
||||
busy={blockMiler.isPending}
|
||||
onConfirm={() => {
|
||||
blockMiler.mutate(
|
||||
{ id: blockRow.milerprofileid, data: {} },
|
||||
{ onSettled: () => setBlockRow(null) }
|
||||
);
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
open={Boolean(vehicleRow)}
|
||||
onOpenChange={(open) => !open && setVehicleRow(null)}
|
||||
title="Assign a vehicle"
|
||||
description={riderName(vehicleRow || {})}
|
||||
icon={Car}
|
||||
busy={assignVehicle.isPending}
|
||||
footer={
|
||||
<>
|
||||
<Button variant="ghost" onClick={() => setVehicleRow(null)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() =>
|
||||
assignVehicle.mutate(
|
||||
{ id: vehicleRow.milerprofileid, data: { vehicleid: Number(selectedVehicle) } },
|
||||
{ onSuccess: (res) => res?.success !== false && setVehicleRow(null) }
|
||||
)
|
||||
}
|
||||
disabled={!selectedVehicle || assignVehicle.isPending}
|
||||
>
|
||||
Assign vehicle
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<Field label="Vehicle">
|
||||
<Select value={selectedVehicle} onValueChange={setSelectedVehicle}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Choose a vehicle" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{(vehicles || []).map((vehicle) => (
|
||||
<SelectItem key={vehicle.vehicleid} value={String(vehicle.vehicleid)}>
|
||||
{vehicle.vehicleno || `Vehicle #${vehicle.vehicleid}`}
|
||||
{vehicle.vehicletype ? ` · ${vehicle.vehicletype}` : ''}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
</Modal>
|
||||
|
||||
<Modal
|
||||
open={Boolean(notifyRow)}
|
||||
onOpenChange={(open) => !open && setNotifyRow(null)}
|
||||
title="Notify this rider"
|
||||
description={`${riderName(notifyRow || {})} — delivered to the rider app as a push notification.`}
|
||||
icon={Bell}
|
||||
busy={notifyMiler.isPending}
|
||||
footer={
|
||||
<>
|
||||
<Button variant="ghost" onClick={() => setNotifyRow(null)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() =>
|
||||
notifyMiler.mutate(
|
||||
{
|
||||
milerProfileId: notifyRow.milerprofileid,
|
||||
title: notice.title,
|
||||
message: notice.message,
|
||||
},
|
||||
{ onSuccess: (res) => res?.success !== false && setNotifyRow(null) }
|
||||
)
|
||||
}
|
||||
disabled={notifyMiler.isPending || !notice.message.trim()}
|
||||
>
|
||||
Send notification
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<Stack space="default">
|
||||
<Field label="Title">
|
||||
<Input value={notice.title} onChange={(e) => setNotice({ ...notice, title: e.target.value })} />
|
||||
</Field>
|
||||
<Field label="Message">
|
||||
<Textarea
|
||||
rows={4}
|
||||
value={notice.message}
|
||||
onChange={(e) => setNotice({ ...notice, message: e.target.value })}
|
||||
placeholder="Orders have been placed for delivery. Kindly accept and process deliveries."
|
||||
/>
|
||||
</Field>
|
||||
</Stack>
|
||||
</Modal>
|
||||
|
||||
<RiderLogsDrawer rider={logsRow} onClose={() => setLogsRow(null)} />
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The rider's own ping history — position and status over time.
|
||||
*
|
||||
* A log entry has no documented field names, so each row renders whichever of
|
||||
* the plausible ones is present rather than assuming a shape that would render
|
||||
* a column of blanks against a real response.
|
||||
*/
|
||||
function RiderLogsDrawer({ rider, onClose }) {
|
||||
const { data: logs = [], isLoading } = useMilerLogs(rider?.milerprofileid, undefined, undefined, 50);
|
||||
|
||||
return (
|
||||
<Drawer
|
||||
open={Boolean(rider)}
|
||||
onOpenChange={(next) => !next && onClose()}
|
||||
title={`Live logs · ${riderName(rider || {})}`}
|
||||
description="The most recent pings this rider's app has sent."
|
||||
icon={Radio}
|
||||
size="lg"
|
||||
>
|
||||
{isLoading ? (
|
||||
<p className="text-body-sm text-ink-3">Loading logs…</p>
|
||||
) : !logs || logs.length === 0 ? (
|
||||
<EmptyState icon={Radio} size="sm" title="No pings recorded" description="This rider has not reported a position yet." />
|
||||
) : (
|
||||
<Stack space="sm">
|
||||
{logs.map((log, index) => (
|
||||
<Surface key={log.logid ?? index} variant="subtle" padding="sm" radius="lg">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<p className="text-body-sm font-medium text-ink-1">
|
||||
{orDash(log.status ?? log.availabilitystatus ?? log.event)}
|
||||
</p>
|
||||
<p className="text-caption text-ink-3">
|
||||
{log.latitude ?? log.currentlatitude ?? '—'}, {log.longitude ?? log.currentlongitude ?? '—'}
|
||||
</p>
|
||||
</div>
|
||||
<p className="shrink-0 text-caption text-ink-4">
|
||||
{formatDoormileTimestamp(log.logdate ?? log.lastpingat ?? log.createdat, 'DD MMM, hh:mm A')}
|
||||
</p>
|
||||
</div>
|
||||
</Surface>
|
||||
))}
|
||||
</Stack>
|
||||
)}
|
||||
</Drawer>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user