177 lines
5.7 KiB
TypeScript
177 lines
5.7 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { z } from "zod";
|
|
import Alert from "@mui/material/Alert";
|
|
import { order, orderSummary, money, uuid } from "@/lib/commerce";
|
|
import { usePlatform } from "@/lib/platform";
|
|
import { WorkspaceShell } from "@/components/WorkspaceShell";
|
|
import {
|
|
Pager,
|
|
Panel,
|
|
QueryView,
|
|
Table,
|
|
usePrivateQuery,
|
|
WorkspaceLink,
|
|
} from "@/components/WorkspaceTools";
|
|
import { ConfirmAction } from "@/components/ConfirmAction";
|
|
function OrderList({ admin }: { admin: boolean }) {
|
|
const [offset, setOffset] = useState(0);
|
|
const path = admin ? "/admin/orders" : "/orders";
|
|
const query = usePrivateQuery(
|
|
`${path}?limit=25&offset=${offset}`,
|
|
z.array(orderSummary),
|
|
);
|
|
const { refetch } = query;
|
|
useEffect(() => {
|
|
const timer = setInterval(() => {
|
|
void refetch();
|
|
}, 30000);
|
|
return () => clearInterval(timer);
|
|
}, [refetch]);
|
|
return (
|
|
<Panel title="Order history">
|
|
<QueryView query={query}>
|
|
{(items) => (
|
|
<>
|
|
<Table
|
|
head={["Order", "Status", "Merchandise", "Created"]}
|
|
rowKeys={items.map((item) => item.id)}
|
|
rows={items.map((item) => [
|
|
<WorkspaceLink href={`${path}/${item.id}`}>
|
|
{item.id}
|
|
</WorkspaceLink>,
|
|
item.status,
|
|
`${item.currency} ${money(item.merchandiseTotal)}`,
|
|
new Date(item.createdAt).toLocaleString(),
|
|
])}
|
|
/>
|
|
<Pager offset={offset} count={items.length} onPage={setOffset} />
|
|
</>
|
|
)}
|
|
</QueryView>
|
|
</Panel>
|
|
);
|
|
}
|
|
function OrderDetail({ admin, id }: { admin: boolean; id: string }) {
|
|
const { request, can } = usePlatform(),
|
|
path = `${admin ? "/admin/orders" : "/orders"}/${id}`,
|
|
query = usePrivateQuery(path, order);
|
|
const { refetch } = query;
|
|
useEffect(() => {
|
|
if (!query.data || query.data.status !== "PENDING_PAYMENT") return;
|
|
const timeout = setTimeout(
|
|
() => {
|
|
void refetch();
|
|
},
|
|
Math.max(1000, Date.parse(query.data.expiresAt) - Date.now() + 250),
|
|
);
|
|
return () => clearTimeout(timeout);
|
|
}, [query.data, refetch]);
|
|
return (
|
|
<Panel title="Order details">
|
|
<QueryView query={query}>
|
|
{(item) => (
|
|
<>
|
|
<p className="workspace-id">
|
|
Order {item.id} · {item.status}
|
|
</p>
|
|
<p>Hold expires: {new Date(item.expiresAt).toLocaleString()}</p>
|
|
<Table
|
|
head={["Candle", "SKU", "Quantity", "Unit price", "Line total"]}
|
|
rowKeys={item.lines.map((line) => line.variantId)}
|
|
rows={item.lines.map((l) => [
|
|
`${l.productName} · ${l.variantName}`,
|
|
l.sku,
|
|
l.quantity,
|
|
`${item.currency} ${money(l.unitPrice)}`,
|
|
`${item.currency} ${money(l.lineTotal)}`,
|
|
])}
|
|
/>
|
|
<dl className="order-totals">
|
|
<dt>Subtotal</dt>
|
|
<dd>
|
|
{item.currency} {money(item.subtotal)}
|
|
</dd>
|
|
<dt>Discount</dt>
|
|
<dd>
|
|
{item.currency} {money(item.discount)}
|
|
</dd>
|
|
<dt>Merchandise total</dt>
|
|
<dd>
|
|
{item.currency} {money(item.merchandiseTotal)}
|
|
</dd>
|
|
<dt>Tax</dt>
|
|
<dd>
|
|
{item.taxTotal === null
|
|
? "Unfinalized"
|
|
: `${item.currency} ${money(item.taxTotal)}`}
|
|
</dd>
|
|
<dt>Shipping</dt>
|
|
<dd>
|
|
{item.shippingTotal === null
|
|
? "Unfinalized"
|
|
: `${item.currency} ${money(item.shippingTotal)}`}
|
|
</dd>
|
|
<dt>Server payable total</dt>
|
|
<dd>
|
|
{item.payableTotal === null
|
|
? "Unfinalized"
|
|
: `${item.currency} ${money(item.payableTotal)}`}
|
|
</dd>
|
|
</dl>
|
|
<Alert severity="info">
|
|
{item.pricingStatus === "UNFINALIZED"
|
|
? "Tax and shipping have not been finalized. "
|
|
: "Tax may already be included in merchandise prices. "}
|
|
Payment collection is unavailable.
|
|
</Alert>
|
|
<h3>Delivery snapshot</h3>
|
|
<p>
|
|
{item.address.recipient}
|
|
<br />
|
|
{item.address.line1} {item.address.line2}
|
|
<br />
|
|
{item.address.city}, {item.address.region}{" "}
|
|
{item.address.postalCode}
|
|
<br />
|
|
{item.address.countryCode} · {item.address.phone}
|
|
</p>
|
|
{item.status === "PENDING_PAYMENT" &&
|
|
(!admin || can("orders.manage")) && (
|
|
<ConfirmAction
|
|
label="Cancel order"
|
|
message="Cancel this order and release its stock holds? Cancellation does not rebuild your cart."
|
|
onConfirm={async () => {
|
|
await request(`${path}/cancel`, order, { method: "POST" });
|
|
await query.refetch();
|
|
}}
|
|
/>
|
|
)}
|
|
</>
|
|
)}
|
|
</QueryView>
|
|
</Panel>
|
|
);
|
|
}
|
|
export default function OrdersPage({
|
|
admin = false,
|
|
id,
|
|
}: {
|
|
admin?: boolean;
|
|
id?: string;
|
|
}) {
|
|
return (
|
|
<WorkspaceShell
|
|
title={admin ? "Organization Orders" : "My Orders"}
|
|
permission={admin ? "orders.read" : undefined}
|
|
>
|
|
{id && !uuid.safeParse(id).success ? (
|
|
<Alert severity="error">Invalid order identifier.</Alert>
|
|
) : id ? (
|
|
<OrderDetail key={uuid.parse(id)} admin={admin} id={uuid.parse(id)} />
|
|
) : (
|
|
<OrderList admin={admin} />
|
|
)}
|
|
</WorkspaceShell>
|
|
);
|
|
}
|