--
This commit is contained in:
@@ -4,16 +4,17 @@ import { Badge } from "../components/ui/Badge";
|
||||
import { Button } from "../components/ui/Button";
|
||||
import { Card } from "../components/ui/Card";
|
||||
import { DataTable, type Column } from "../components/ui/DataTable";
|
||||
import { Checkbox, Field, Input } from "../components/ui/Field";
|
||||
import { Checkbox, Field, Input, Select } from "../components/ui/Field";
|
||||
import { Modal } from "../components/ui/Modal";
|
||||
import { SearchInput } from "../components/ui/Misc";
|
||||
import { PageHeader } from "../components/ui/PageHeader";
|
||||
import { EmptyState, ErrorState } from "../components/ui/States";
|
||||
import { Tabs } from "../components/ui/Tabs";
|
||||
import { useToast } from "../components/ui/Toast";
|
||||
import { MasterRequests } from "../components/admin/MasterRequests";
|
||||
import { LIVE, useAsync } from "../hooks/useAsync";
|
||||
import { api } from "../api";
|
||||
import { errorMessage } from "../lib/format";
|
||||
import { errorMessage, isField } from "../lib/format";
|
||||
import { useUser } from "../lib/session";
|
||||
import { maxLength, minLength, required, useValidation } from "../lib/validation";
|
||||
import type { Chemist, Doctor } from "../types";
|
||||
@@ -46,9 +47,13 @@ export function DirectoryPage() {
|
||||
const [tab, setTab] = useState<"doctors" | "chemists">("doctors");
|
||||
const [query, setQuery] = useState("");
|
||||
const q = query.trim().toLowerCase();
|
||||
// Headquarters filter: the list holds only the headquarters this user may see (every one for
|
||||
// the admin and GM, an Area Manager's own), so it offers exactly those. "" = all of them.
|
||||
const [hqFilter, setHqFilter] = useState("");
|
||||
const inHq = (id: string) => !hqFilter || id === hqFilter;
|
||||
|
||||
const doctorRows = useMemo(() => (doctors.data ?? []).filter((d) => !q || `${d.name} ${d.speciality} ${d.clinic_address} ${hqLabel.get(d.headquarters_id) ?? ""}`.toLowerCase().includes(q)), [doctors.data, hqLabel, q]);
|
||||
const chemistRows = useMemo(() => (chemists.data ?? []).filter((c) => !q || `${c.name} ${c.shop_address} ${hqLabel.get(c.headquarters_id) ?? ""}`.toLowerCase().includes(q)), [chemists.data, hqLabel, q]);
|
||||
const doctorRows = useMemo(() => (doctors.data ?? []).filter((d) => inHq(d.headquarters_id) && (!q || `${d.name} ${d.speciality} ${d.clinic_address} ${hqLabel.get(d.headquarters_id) ?? ""}`.toLowerCase().includes(q))), [doctors.data, hqLabel, q, hqFilter]);
|
||||
const chemistRows = useMemo(() => (chemists.data ?? []).filter((c) => inHq(c.headquarters_id) && (!q || `${c.name} ${c.shop_address} ${hqLabel.get(c.headquarters_id) ?? ""}`.toLowerCase().includes(q))), [chemists.data, hqLabel, q, hqFilter]);
|
||||
const active = tab === "doctors" ? doctors : chemists;
|
||||
async function toggleActive(target: Editing) {
|
||||
const next = !target.item.is_active;
|
||||
@@ -88,15 +93,22 @@ export function DirectoryPage() {
|
||||
return (
|
||||
<>
|
||||
<PageHeader title="Doctors & Chemists" description="Masters available within your permitted territory." />
|
||||
{!isField(user.role) && <MasterRequests mode="review" onReviewed={() => { void doctors.reload(); void chemists.reload(); }} />}
|
||||
<Card>
|
||||
<div className="toolbar">
|
||||
<Tabs
|
||||
label="Directory"
|
||||
value={tab}
|
||||
onChange={setTab}
|
||||
items={[{ key: "doctors", label: "Doctors", count: doctors.data?.length }, { key: "chemists", label: "Chemists", count: chemists.data?.length }]}
|
||||
items={[{ key: "doctors", label: "Doctors", count: doctors.data ? doctorRows.length : undefined }, { key: "chemists", label: "Chemists", count: chemists.data ? chemistRows.length : undefined }]}
|
||||
/>
|
||||
<SearchInput value={query} onChange={setQuery} placeholder={`Search ${tab}`} />
|
||||
<div className="toolbar-group">
|
||||
<Select className="hq-filter" value={hqFilter} onChange={(e) => setHqFilter(e.target.value)} aria-label="Filter by headquarters" disabled={!hqs.data}>
|
||||
<option value="">All headquarters</option>
|
||||
{(hqs.data ?? []).map((h) => <option key={h.id} value={h.id}>{h.code} · {h.name}</option>)}
|
||||
</Select>
|
||||
<SearchInput value={query} onChange={setQuery} placeholder={`Search ${tab}`} />
|
||||
</div>
|
||||
</div>
|
||||
{active.error ? <ErrorState message={active.error} onRetry={() => void active.reload()} /> : tab === "doctors" ? (
|
||||
<DataTable columns={doctorColumns} rows={doctorRows} rowKey={(d) => d.id} loading={doctors.loading} minWidth={820} empty={<EmptyState icon="stethoscope" title="No doctors found" />} />
|
||||
|
||||
Reference in New Issue
Block a user