import { useState } from "react"; import { Badge } from "../ui/Badge"; import { Button } from "../ui/Button"; import { Card, CardBody, CardHeader } from "../ui/Card"; import { DataTable, type Column } from "../ui/DataTable"; import { Field, Textarea } from "../ui/Field"; import { Modal } from "../ui/Modal"; import { ErrorState } from "../ui/States"; import { useToast } from "../ui/Toast"; import { LIVE, useAsync } from "../../hooks/useAsync"; import { api } from "../../api"; import { errorMessage, formatDate } from "../../lib/format"; import { useUser } from "../../lib/session"; import type { Chemist, Doctor } from "../../types"; type Request = { kind: "doctor" | "chemist"; item: Doctor | Chemist }; const address = (r: Request) => (r.kind === "doctor" ? (r.item as Doctor).clinic_address : (r.item as Chemist).shop_address); /** * Doctors and chemists an MR added that wait for the Area Manager. * `mine`: the MR's own requests, with any rejection and its reason. * `review`: what waits for this Area Manager (or the admin) to approve or reject; the GM only looks. * Shows nothing when there is nothing to show. */ export function MasterRequests({ mode, onReviewed }: { mode: "mine" | "review"; onReviewed?: () => void }) { const user = useUser(); const toast = useToast(); const canAct = mode === "review" && (user.role === "AM" || user.role === "ADMIN"); const data = useAsync(async () => { const [doctors, chemists] = await Promise.all([api.doctorRequests(), api.chemistRequests()]); return [...doctors.map((item) => ({ kind: "doctor" as const, item })), ...chemists.map((item) => ({ kind: "chemist" as const, item }))] .sort((a, b) => b.item.created_at.localeCompare(a.item.created_at)); }, [], { poll: LIVE }); const [acting, setActing] = useState(null); const [rejecting, setRejecting] = useState(null); const [reason, setReason] = useState(""); const [reasonError, setReasonError] = useState(null); const rows = data.data ?? []; async function decide(r: Request, approve: boolean, why?: string) { setActing(r.item.id); try { await api.reviewMaster(r.kind, r.item.id, approve, why); toast.success(approve ? `${r.item.name} approved and added to the list` : `${r.item.name} rejected`); setRejecting(null); await data.reload(); onReviewed?.(); } catch (e) { toast.error(errorMessage(e, approve ? "Could not approve" : "Could not reject")); } finally { setActing(null); } } if (data.error) return void data.reload()} />; if (rows.length === 0) return null; const columns: Column[] = [ { key: "kind", header: "Type", render: (r) => {r.kind === "doctor" ? "Doctor" : "Chemist"}, nowrap: true }, { key: "name", header: "Name", render: (r) => {r.item.name} }, { key: "address", header: "Address", render: address }, ...(mode === "review" ? [{ key: "by", header: "Added by", render: (r: Request) => r.item.requested_by_name ?? "—", nowrap: true }] : []), { key: "on", header: "Added on", render: (r) => formatDate(r.item.created_at), nowrap: true }, mode === "mine" ? { key: "status", header: "Status", render: (r) => r.item.approval_status === "REJECTED" ? Rejected{r.item.rejection_reason && — {r.item.rejection_reason}} : Waiting for Area Manager, } : { key: "_actions", header: "", align: "right", nowrap: true, render: (r) => canAct ? ( ) : Waiting for Area Manager, }, ]; const pending = rows.filter((r) => r.item.approval_status === "PENDING").length; return ( <> `${r.kind}-${r.item.id}`} loading={data.loading} minWidth={720} /> setRejecting(null)} footer={<>} >