Files
co-lite/solvway-admin-portal/app/inventory/page.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

193 lines
7.8 KiB
TypeScript

"use client";
import { Suspense, useCallback, useEffect, useState } from "react";
import { Button } from "@/app/components/ui/button";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/app/components/ui/dialog";
import { Sheet, SheetContent, SheetHeader, SheetTitle } from "@/app/components/ui/sheet";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/app/components/ui/tabs";
import { PageHeader } from "@/app/components/ui/page-header";
import { RequireModule } from "@/app/lib/RequireModule";
import { useAdminAuth } from "@/app/lib/AdminAuthContext";
import { INVENTORY_MODULES } from "@/app/lib/inventoryModules";
import { StockOnHandReport } from "@/app/components/inventory/StockOnHandReport";
import { MovementHistoryReport } from "@/app/components/inventory/MovementHistoryReport";
import { StockInForm } from "@/app/components/inventory/StockInForm";
import { TransferForm } from "@/app/components/inventory/TransferForm";
import { AdjustmentForm } from "@/app/components/inventory/AdjustmentForm";
import { StockCountsTab } from "@/app/components/inventory/StockCountsTab";
import { useQueryState } from "@/app/lib/useQueryState";
import { useT } from "@/app/lib/i18n/MessagesProvider";
// One Inventory page (merge-inventory-page): Stock on Hand, History and Stock counts tabs, and the three stock
// forms. Tab, open form and History filters live in the URL. Each part is gated by its own module;
// the server still enforces every endpoint. No side effects on GET/prefetch -- data loads in effects.
type InventoryTab = "stock" | "history" | "counts";
export type InventoryAction = "stock-in" | "transfer" | "adjust";
const ACTIONS: { action: InventoryAction; labelCode: string; module: string }[] = [
{ action: "stock-in", labelCode: "BO_STOCK_IN", module: INVENTORY_MODULES.stockIn },
{ action: "transfer", labelCode: "InvActionTransfer", module: INVENTORY_MODULES.transfer },
{ action: "adjust", labelCode: "InvActionAdjust", module: INVENTORY_MODULES.adjust },
];
const FORMS = { "stock-in": StockInForm, transfer: TransferForm, adjust: AdjustmentForm } as const;
function InventoryView() {
const { hasModule } = useAdminAuth();
const t = useT();
const [params, setSearchParams] = useQueryState();
// The Stock on Hand warehouse filter; the stock forms pre-fill it.
const [warehouseId, setWarehouseId] = useState<string | undefined>();
// Bumped after a successful submit so the open tab reloads behind the panel.
const [reloadKey, setReloadKey] = useState(0);
const [dirty, setDirty] = useState(false);
const [confirmClose, setConfirmClose] = useState(false);
const tabs: InventoryTab[] = [];
if (hasModule(INVENTORY_MODULES.stockOnHand)) tabs.push("stock");
if (hasModule(INVENTORY_MODULES.history)) tabs.push("history");
if (hasModule(INVENTORY_MODULES.count) || hasModule(INVENTORY_MODULES.countApprove)) tabs.push("counts");
const actions = ACTIONS.filter(a => hasModule(a.module));
// A tab or form the user does not hold is ignored, whatever the URL says.
const requestedTab = params.get("tab");
const tab = tabs.find(tab => tab === requestedTab) ?? tabs[0];
const action = actions.find(a => a.action === params.get("action"))?.action;
const legacyVariation = params.get("variation");
const legacyWarehouse = params.get("warehouse");
const setParams = useCallback(
(patch: Record<string, string | undefined>) =>
setSearchParams(prev => {
const next = new URLSearchParams(prev);
for (const [key, value] of Object.entries(patch)) {
if (value === undefined) next.delete(key);
else next.set(key, value);
}
return next;
}),
[setSearchParams],
);
// An external link with the old ?variation= / ?warehouse= params becomes mv.* state once.
useEffect(() => {
if (legacyVariation === null && legacyWarehouse === null) return;
setParams({
...(legacyVariation !== null && { "mv.sku": legacyVariation }),
...(legacyWarehouse !== null && { "mv.warehouse": legacyWarehouse }),
variation: undefined,
warehouse: undefined,
});
}, [legacyVariation, legacyWarehouse, setParams]);
const closePanel = () => {
setDirty(false);
setConfirmClose(false);
setParams({ action: undefined });
};
// Unsaved lines are confirmed in-page before the panel closes (no window.confirm).
const requestClose = () => (dirty ? setConfirmClose(true) : closePanel());
const actionLabel = ACTIONS.find(a => a.action === action)?.labelCode;
const Form = action ? FORMS[action] : null;
return (
<div>
<PageHeader
title={t("Inventory")}
actions={actions.map(a => (
<Button
key={a.action}
variant={a.action === "stock-in" ? "default" : "outline"}
aria-pressed={action === a.action}
onClick={() => setParams({ action: a.action })}
>
{t(a.labelCode)}
</Button>
))}
/>
{tab === undefined ? (
<p className="text-sm text-muted-foreground">{t("InvNoReports")}</p>
) : (
<Tabs value={tab} onValueChange={value => setParams({ tab: value })}>
<TabsList>
{tabs.includes("stock") && <TabsTrigger value="stock">{t("BO_STOCK_ON_HAND")}</TabsTrigger>}
{tabs.includes("history") && <TabsTrigger value="history">{t("InvTabHistory")}</TabsTrigger>}
{tabs.includes("counts") && <TabsTrigger value="counts">{t("BO_STOCK_COUNT")}</TabsTrigger>}
</TabsList>
{tabs.includes("stock") && (
<TabsContent value="stock">
<StockOnHandReport
onWarehouseChange={setWarehouseId}
onViewMovements={skuVariationId =>
setParams({
tab: "history",
"mv.sku": skuVariationId,
"mv.warehouse": warehouseId,
"mv.type": undefined,
"mv.from": undefined,
"mv.to": undefined,
"mv.page": undefined,
})
}
reloadKey={reloadKey}
/>
</TabsContent>
)}
{tabs.includes("history") && (
<TabsContent value="history">
<MovementHistoryReport reloadKey={reloadKey} />
</TabsContent>
)}
{tabs.includes("counts") && (<TabsContent value="counts"><StockCountsTab /></TabsContent>)}
</Tabs>
)}
<Sheet open={Form !== null} onOpenChange={open => { if (!open) requestClose(); }}>
<SheetContent className="sm:max-w-2xl">
<SheetHeader>
<SheetTitle>{actionLabel ? t(actionLabel) : ""}</SheetTitle>
</SheetHeader>
{Form && (
<Form
key={action}
defaultWarehouseId={warehouseId}
onDirtyChange={setDirty}
onSubmitted={() => setReloadKey(k => k + 1)}
/>
)}
</SheetContent>
</Sheet>
<Dialog open={confirmClose} onOpenChange={open => { if (!open) setConfirmClose(false); }}>
<DialogContent>
<DialogHeader>
<DialogTitle>{t("InvDiscardTitle")}</DialogTitle>
</DialogHeader>
<p className="text-sm text-muted-foreground">{t("InvDiscardBody")}</p>
<DialogFooter>
<Button variant="outline" onClick={() => setConfirmClose(false)}>
{t("InvKeepEditing")}
</Button>
<Button variant="destructive" onClick={closePanel}>
{t("SetupDiscard")}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}
export default function InventoryPage() {
const t = useT();
return (
<RequireModule pathname="/inventory">
<Suspense fallback={<PageHeader title={t("Inventory")} />}>
<InventoryView />
</Suspense>
</RequireModule>
);
}