UNPKG

@rsc-labs/medusa-documents

Version:
186 lines (185 loc) 10.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeAmount = exports.getDecimalDigits = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const moment_1 = __importDefault(require("moment")); const react_1 = require("react"); const ui_1 = require("@medusajs/ui"); const currencies_1 = require("./utils/currencies"); const material_1 = require("@mui/material"); const actions_dropdown_1 = require("../../actions-dropdown/actions-dropdown"); const invoice_number_from_order_1 = __importDefault(require("./invoice-number-from-order")); const packing_slip_number_1 = __importDefault(require("./packing-slip-number")); const icons_1 = require("@medusajs/icons"); const Link_1 = __importDefault(require("@mui/material/Link")); /** * Checks the list of currencies and returns the divider/multiplier * that should be used to calculate the persited and display amount. * @param currency * @return {number} */ function getDecimalDigits(currency) { const divisionDigits = currencies_1.currencies[currency.toUpperCase()].decimal_digits; return Math.pow(10, divisionDigits); } exports.getDecimalDigits = getDecimalDigits; function normalizeAmount(currency, amount) { const divisor = getDecimalDigits(currency); return Math.floor(amount) / divisor; } exports.normalizeAmount = normalizeAmount; function formatAmountWithSymbol({ amount, currency, digits, tax = 0, }) { let locale = "en-US"; // We need this to display 'Kr' instead of 'DKK' if (currency.toLowerCase() === "dkk") { locale = "da-DK"; } digits = digits !== null && digits !== void 0 ? digits : currencies_1.currencies[currency.toUpperCase()].decimal_digits; const normalizedAmount = normalizeAmount(currency, amount); const taxRate = tax instanceof Array ? tax.reduce((acc, curr) => acc + curr.rate, 0) : tax; return new Intl.NumberFormat(locale, { style: "currency", currency, minimumFractionDigits: digits, }).format(normalizedAmount * (1 + taxRate / 100)); } const useOrderTableColums = () => { const decideStatus = (status) => { switch (status) { case "captured": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "green", children: "Paid" })); case "awaiting": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "grey", children: "Awaiting" })); case "requires_action": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "red", children: "Requires action" })); case "canceled": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "orange", children: "Canceled" })); default: return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "purple", children: "N/A" })); } }; const decideFullfillmentStatus = (status) => { switch (status) { case "not_fulfilled": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "grey", children: "Not fulfilled" })); case "partially_fulfilled": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "blue", children: "Partially fulfilled" })); case "fulfilled": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "green", children: "Fulfilled" })); case "partially_shipped": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "blue", children: "Partially shipped" })); case "shipped": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "green", children: "Shipped" })); case "partially_returned": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "blue", children: "Partially returned" })); case "returned": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "green", children: "Returned" })); case "canceled": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "red", children: "Canceled" })); case "requires_action": return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "purple", children: "Requires action" })); default: return ((0, jsx_runtime_1.jsx)(ui_1.StatusBadge, { color: "grey", children: "N/A" })); } }; const columns = (0, react_1.useMemo)(() => [ { Header: (0, jsx_runtime_1.jsx)("div", { className: "pl-2", children: "Order" }), accessor: "display_id", Cell: ({ cell: { value } }) => ((0, jsx_runtime_1.jsx)("p", { className: "text-grey-90 group-hover:text-violet-60 pl-2", children: `#${value}` })), }, { Header: ("Date added"), accessor: "created_at", Cell: ({ cell: { value } }) => { return ((0, jsx_runtime_1.jsx)(ui_1.Tooltip, { content: (0, jsx_runtime_1.jsx)(ui_1.Text, { children: (0, moment_1.default)(value).format("DD MMM YYYY hh:mm a") }), children: (0, jsx_runtime_1.jsx)("p", { className: "text-grey-90 group-hover:text-violet-60 min-w-[40px]", children: (0, moment_1.default)(value).format("DD MMM YYYY") }) })); } }, { Header: ("Customer"), accessor: "customer", Cell: ({ row, cell: { value } }) => { var _a, _b; const customer = { first_name: (value === null || value === void 0 ? void 0 : value.first_name) || ((_a = row.original.shipping_address) === null || _a === void 0 ? void 0 : _a.first_name), last_name: (value === null || value === void 0 ? void 0 : value.last_name) || ((_b = row.original.shipping_address) === null || _b === void 0 ? void 0 : _b.last_name), email: row.original.email, }; return ((0, jsx_runtime_1.jsx)("p", { className: "text-grey-90 group-hover:text-violet-60 min-w-[100px]", children: `${(customer.first_name || customer.last_name) ? `${customer.first_name} ${customer.last_name}` : (customer.email ? customer.email : "-")}` })); }, }, { Header: ("Fulfillment"), accessor: "fulfillment_status", Cell: ({ cell: { value } }) => decideFullfillmentStatus(value), }, { Header: ("Payment status"), accessor: "payment_status", Cell: ({ cell: { value } }) => decideStatus(value), }, { Header: () => ((0, jsx_runtime_1.jsx)("div", { className: "text-right", children: ("Total") })), accessor: "total", Cell: ({ row, cell: { value } }) => ((0, jsx_runtime_1.jsx)("div", { className: "text-grey-90 group-hover:text-violet-60 text-right", children: formatAmountWithSymbol({ amount: value, currency: row.original.currency_code, digits: 2, }) })), }, // { // Header: "", // accessor: "currency_code", // Cell: ({ cell: { value } }) => ( // <div className="text-grey-40 text-right">{value.toUpperCase()}</div> // ), // }, // { // Header: "", // accessor: "country_code", // Cell: ({ row }) => ( // <div className="pr-2"> // <div className="rounded-rounded flex w-full justify-end"> // <Tooltip // content={ // row.original.shipping_address?.country_code?.toUpperCase() // } // > // <ReactCountryFlag // className={"rounded"} // svg // countryCode={row.original.shipping_address?.country_code} // /> // </Tooltip> // </div> // </div> // ), // }, { Header: () => ((0, jsx_runtime_1.jsx)("div", { style: { textAlign: 'center' }, children: ("Documents") })), id: "invoice_number", Cell: ({ row }) => { return ((0, jsx_runtime_1.jsx)("p", { className: "text-grey-90 group-hover:text-violet-60 pl-2", children: (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, justifyContent: 'flex-start', direction: 'column', spacing: 1, children: [row.original.metadata['invoice_id'] !== undefined && (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(invoice_number_from_order_1.default, { invoiceId: row.original.metadata['invoice_id'] }) }), row.original.metadata['packing_slip_id'] !== undefined && (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(packing_slip_number_1.default, { packingSlipId: row.original.metadata['packing_slip_id'] }) })] }) })); } }, { Header: () => ((0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, justifyContent: "flex-end", alignItems: "flex-end", spacing: 1, children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Tooltip, { content: (0, jsx_runtime_1.jsxs)(material_1.Grid, { item: true, children: [(0, jsx_runtime_1.jsx)(ui_1.Text, { size: "small", children: "We do not store documents. " }), (0, jsx_runtime_1.jsx)(Link_1.default, { fontSize: 12, href: 'https://github.com/RSC-Labs/medusa-documents?tab=readme-ov-file#what-means-we-do-not-store-documents', children: "Learn more what it means. " })] }), children: (0, jsx_runtime_1.jsx)(icons_1.InformationCircle, {}) }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: ("Actions") })] })), id: "actions", Cell: ({ row }) => { return ((0, jsx_runtime_1.jsx)(material_1.Grid, { container: true, justifyContent: 'flex-end', children: (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(actions_dropdown_1.ActionsDropdown, { order: row.original }) }) })); } }, ], []); return [columns]; }; exports.default = useOrderTableColums;