From 08b6cfb505b7fdb532c52400770b2ccac63da145 Mon Sep 17 00:00:00 2001 From: "cyril.tsui" Date: Sun, 24 Aug 2025 00:47:36 +0800 Subject: [PATCH] [PO] add mail template function --- src/app/api/mailTemplate/actions.ts | 17 +++++++++++++++++ src/app/utils/fetchUtil.ts | 7 ++++++- src/components/PoDetail/PoDetail.tsx | 11 ++++++++++- src/components/PoDetail/PoInputGrid.tsx | 9 ++++++--- src/components/PoDetail/QcStockInModalVer2.tsx | 2 ++ 5 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src/app/api/mailTemplate/actions.ts diff --git a/src/app/api/mailTemplate/actions.ts b/src/app/api/mailTemplate/actions.ts new file mode 100644 index 0000000..66a78f2 --- /dev/null +++ b/src/app/api/mailTemplate/actions.ts @@ -0,0 +1,17 @@ +"use server"; + +import { serverFetchBlob } from "@/app/utils/fetchUtil"; +import { BASE_API_URL } from "@/config/api"; +import { cache } from "react"; + +export const getMailTemplateForStockInLine = cache(async (stockInLineId: number) => { + console.log("stockInLineId", stockInLineId) + return serverFetchBlob(`${BASE_API_URL}/mailTemplates/getMailTemplateForStockInLine/${stockInLineId}`, + { + method: "GET", + headers: { "Content-Type": "application/json" }, + next: { + tags: ["mailTemplateForStockInLine"], + }, + }) +}) \ No newline at end of file diff --git a/src/app/utils/fetchUtil.ts b/src/app/utils/fetchUtil.ts index 3f01795..f9d82b5 100644 --- a/src/app/utils/fetchUtil.ts +++ b/src/app/utils/fetchUtil.ts @@ -3,6 +3,11 @@ import { getServerSession } from "next-auth"; import { headers } from "next/headers"; import { redirect } from "next/navigation"; +export interface BlobResponse { + filename: string; + blobValue: Uint8Array; +} + export interface Pageable { pageSize?: number; pageNum?: number; @@ -86,7 +91,7 @@ export async function serverFetchJson(...args: FetchParams) { } } -export async function serverFetchBlob(...args: FetchParams) { +export async function serverFetchBlob(...args: FetchParams) { const response = await serverFetch(...args); if (response.ok) { diff --git a/src/components/PoDetail/PoDetail.tsx b/src/components/PoDetail/PoDetail.tsx index 1a9a04a..fdf737d 100644 --- a/src/components/PoDetail/PoDetail.tsx +++ b/src/components/PoDetail/PoDetail.tsx @@ -68,7 +68,7 @@ import PoQcStockInModal from "./PoQcStockInModal"; import QrModal from "./QrModal"; import { PlayArrow } from "@mui/icons-material"; import DoneIcon from "@mui/icons-material/Done"; -import { getCustomWidth } from "@/app/utils/commonUtil"; +import { downloadFile, getCustomWidth } from "@/app/utils/commonUtil"; import PoInfoCard from "./PoInfoCard"; import { decimalFormatter, integerFormatter } from "@/app/utils/formatUtil"; import { List, ListItem, ListItemButton, ListItemText, Divider } from "@mui/material"; @@ -78,6 +78,7 @@ import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"; import { DatePicker, LocalizationProvider, zhHK } from "@mui/x-date-pickers"; import { debounce } from "lodash"; import LoadingComponent from "../General/LoadingComponent"; +import { getMailTemplateForStockInLine } from "@/app/api/mailTemplate/actions"; //import { useRouter } from "next/navigation"; @@ -333,6 +334,13 @@ const PoDetail: React.FC = ({ po, qc, warehouse }) => { setPurchaseOrder(newPo); }, [purchaseOrder.id]); + const handleMailTemplateForStockInLine = useCallback(async (stockInLineId: number) => { + const response = await getMailTemplateForStockInLine(stockInLineId) + if (response) { + downloadFile(new Uint8Array(response.blobValue), response.filename); + } + }, []) + useEffect(() => { setRows(purchaseOrder.pol || []); }, [purchaseOrder]); @@ -849,6 +857,7 @@ const PoDetail: React.FC = ({ po, qc, warehouse }) => { itemDetail={selectedRow} warehouse={warehouse} fetchPoDetail={fetchPoDetail} + handleMailTemplateForStockInLine={handleMailTemplateForStockInLine} /> diff --git a/src/components/PoDetail/PoInputGrid.tsx b/src/components/PoDetail/PoInputGrid.tsx index 3a41fcd..88dcbc3 100644 --- a/src/components/PoDetail/PoInputGrid.tsx +++ b/src/components/PoDetail/PoInputGrid.tsx @@ -74,6 +74,7 @@ interface Props { stockInLine: StockInLine[]; warehouse: WarehouseResult[]; fetchPoDetail: (poId: string) => void; + handleMailTemplateForStockInLine: (stockInLineId: number) => void; } export type StockInLineEntryError = { @@ -112,7 +113,8 @@ function PoInputGrid({ itemDetail, stockInLine, warehouse, - fetchPoDetail + fetchPoDetail, + handleMailTemplateForStockInLine }: Props) { console.log(itemDetail); const { t } = useTranslation("purchaseOrder"); @@ -595,7 +597,7 @@ const closeNewModal = useCallback(() => { variant="contained" color="primary" sx={{ width: '150px' }} - // onClick={formProps.handleSubmit(onSubmitEmailSupplier)} + onClick={() => handleMailTemplateForStockInLine(params.row.id as number)} > {t("email supplier")} } @@ -604,7 +606,7 @@ const closeNewModal = useCallback(() => { // color: "primary.main", // marginRight: 1, }} - onClick={handleNewQC(params.row.id, params)} + // onClick={handleNewQC(params.row.id, params)} color="inherit" key="edit" />, @@ -914,6 +916,7 @@ const closeNewModal = useCallback(() => { open={newOpen} onClose={closeNewModal} itemDetail={modalInfo} + handleMailTemplateForStockInLine={handleMailTemplateForStockInLine} /> ) diff --git a/src/components/PoDetail/QcStockInModalVer2.tsx b/src/components/PoDetail/QcStockInModalVer2.tsx index 1f0998b..8451db6 100644 --- a/src/components/PoDetail/QcStockInModalVer2.tsx +++ b/src/components/PoDetail/QcStockInModalVer2.tsx @@ -54,6 +54,7 @@ interface CommonProps extends Omit { qc?: QcItemWithChecks[]; warehouse?: any[]; // type: "qc" | "stockIn" | "escalation" | "putaway" | "reject"; + handleMailTemplateForStockInLine: (stockInLineId: number) => void; } interface Props extends CommonProps { itemDetail: StockInLine & { qcResult?: PurchaseQcResult[] }; @@ -69,6 +70,7 @@ const PoQcStockInModalVer2: React.FC = ({ setItemDetail, qc, warehouse, + handleMailTemplateForStockInLine, }) => { const { t,