import { useState } from "react"; import { z } from "zod"; import Alert from "@mui/material/Alert"; import { coupon, couponInput, pricing, pricingInput } from "@/lib/commerce"; import { usePlatform } from "@/lib/platform"; import { WorkspaceShell } from "@/components/WorkspaceShell"; import { ActionForm, Field, Pager, Panel, QueryView, Table, usePrivateQuery, readText, readNumber, } from "@/components/WorkspaceTools"; import { ConfirmAction } from "@/components/ConfirmAction"; const currencies = ["INR", "USD", "EUR", "GBP"].map((value) => ({ value, label: value, })); function Coupons() { const { request } = usePlatform(); const [offset, setOffset] = useState(0), [kind, setKind] = useState("PERCENT"); const query = usePrivateQuery( `/coupons?limit=25&offset=${offset}`, z.array(coupon), ); return ( <> {(items) => ( <> item.id)} rows={items.map((c) => [ c.code, c.kind === "FIXED" ? `${c.currency} ${c.amount}` : `${(c.percentBps || 0) / 100}%`, `${new Date(c.startsAt).toLocaleString()} – ${new Date(c.endsAt).toLocaleString()}`, c.active ? "Active" : "Inactive", { await request(`/coupons/${c.id}/status`, coupon, { method: "PATCH", body: { active: !c.active }, }); await query.refetch(); }} />, ])} /> )} Rules cannot be edited after creation. Dates must include an explicit timezone. Campaign-linked coupons belong to the growth phase.
({ code: readText(data, "code"), currency: readText(data, "currency"), minimumSubtotal: readText(data, "minimumSubtotal"), maxUses: readNumber(data, "maxUses"), perUserLimit: readNumber(data, "perUserLimit"), startsAt: readText(data, "startsAt"), endsAt: readText(data, "endsAt"), kind, ...(kind === "FIXED" ? { amount: readText(data, "amount") } : { percentBps: readNumber(data, "percentBps") }), })} submit={async (value) => { await request("/coupons", coupon, { method: "POST", body: couponInput.parse(value), }); await query.refetch(); }} > {kind === "FIXED" ? ( ) : ( )}
); } function Pricing() { const { request } = usePlatform(); const [offset, setOffset] = useState(0); const query = usePrivateQuery( `/admin/pricing-policies?limit=25&offset=${offset}`, z.array(pricing), ); return ( <> Use approved tax and shipping rules. New versions do not reprice existing orders and do not enable payment. {(items) => ( <>
item.id)} rows={items.map((p) => [ p.name, `${p.currency} · ${p.countryCode} · ${p.region || "Country fallback"}`, p.taxMode, p.active ? "Active" : "Inactive", { await request( `/admin/pricing-policies/${p.id}/status`, pricing, { method: "PATCH", body: { active: !p.active } }, ); await query.refetch(); }} />, ])} /> )} ({ name: readText(data, "name"), currency: readText(data, "currency"), countryCode: readText(data, "countryCode"), region: readText(data, "region"), taxMode: readText(data, "taxMode"), merchandiseTaxBps: readNumber(data, "merchandiseTaxBps"), shippingTaxBps: readNumber(data, "shippingTaxBps"), shippingFee: readText(data, "shippingFee"), freeShippingMinimum: readText(data, "freeShippingMinimum") || null, })} submit={async (value) => { await request("/admin/pricing-policies", pricing, { method: "POST", body: pricingInput.parse(value), }); await query.refetch(); }} > ({ value, label: value, }))} /> ); } export default function OffersPage({ pricing: showPricing = false, }: { pricing?: boolean; }) { return ( {showPricing ? : } ); }