UNPKG

@rsc-labs/medusa-store-analytics-v2

Version:
79 lines (78 loc) 3.75 kB
import { jsxs, jsx } from "react/jsx-runtime"; import { Heading, Alert } from "@medusajs/ui"; import { ShoppingBag } from "@medusajs/icons"; import { Grid, CircularProgress } from "@mui/material"; import { PopularityTable } from "../common/popularity-table.js"; import { useState, useEffect } from "react"; import { deduceDateUrlParams } from "../utils/helpers.js"; function transformToPopularityTable(result) { const currentMap = /* @__PURE__ */ new Map(); const previousMap = /* @__PURE__ */ new Map(); result.current.forEach((currentItem) => { const currentCount = currentMap.get(currentItem.regionName) || 0; currentMap.set(currentItem.regionName, currentCount + parseInt(currentItem.orderCount)); }); result.previous.forEach((previousItem) => { const previousCount = previousMap.get(previousItem.regionName) || 0; previousMap.set(previousItem.regionName, previousCount + parseInt(previousItem.orderCount)); }); return Array.from(currentMap.keys()).map((name) => ({ name, current: String(currentMap.get(name) || 0), previous: String(previousMap.get(name) || 0) })); } const RegionsPopularityDetails = ({ orderStatuses, dateRange, dateRangeCompareTo, compareEnabled }) => { var _a, _b; const [data, setData] = useState(void 0); const [error, setError] = useState(void 0); const [isLoading, setLoading] = useState(true); useEffect(() => { setLoading(true); }, [dateRange, dateRangeCompareTo, orderStatuses]); useEffect(() => { if (!isLoading) { return; } fetch(`/admin/sales-analytics/regions-popularity?${deduceDateUrlParams(dateRange, dateRangeCompareTo, orderStatuses).toString()}`, { credentials: "include" }).then((res) => res.json()).then((result) => { setData(result); setLoading(false); }).catch((error2) => { setError(error2); console.error(error2); }); }, [isLoading]); if (isLoading) { return /* @__PURE__ */ jsx(CircularProgress, { size: 12 }); } if (isLoading) { return /* @__PURE__ */ jsx(CircularProgress, { size: 12 }); } if (error) { const trueError = error; const errorText = `Error when loading data. It shouldn't have happened - please raise an issue. For developer: ${(_b = (_a = trueError == null ? void 0 : trueError.response) == null ? void 0 : _a.data) == null ? void 0 : _b.message}`; return /* @__PURE__ */ jsx(Alert, { variant: "error", children: errorText }); } if (data && data.analytics == void 0) { return /* @__PURE__ */ jsx(Heading, { level: "h3", children: "Cannot get orders" }); } if (data && data.analytics.dateRangeFrom) { return /* @__PURE__ */ jsx(PopularityTable, { valueColumnName: "Orders", tableRows: transformToPopularityTable(data.analytics), enableComparing: compareEnabled && dateRangeCompareTo !== void 0 }); } else { return /* @__PURE__ */ jsx(Heading, { level: "h3", children: "No orders" }); } }; const RegionsPopularityCard = ({ orderStatuses, dateRange, dateRangeCompareTo, compareEnabled }) => { return /* @__PURE__ */ jsxs(Grid, { container: true, paddingBottom: 2, spacing: 3, children: [ /* @__PURE__ */ jsx(Grid, { item: true, xs: 12, md: 12, children: /* @__PURE__ */ jsxs(Grid, { container: true, spacing: 2, children: [ /* @__PURE__ */ jsx(Grid, { item: true, children: /* @__PURE__ */ jsx(ShoppingBag, {}) }), /* @__PURE__ */ jsx(Grid, { item: true, children: /* @__PURE__ */ jsx(Heading, { level: "h2", children: "Regions popularity" }) }) ] }) }), /* @__PURE__ */ jsx(Grid, { item: true, xs: 12, md: 12, children: /* @__PURE__ */ jsx(RegionsPopularityDetails, { orderStatuses, dateRange, dateRangeCompareTo, compareEnabled }) }) ] }); }; export { RegionsPopularityCard };