UNPKG

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

Version:
69 lines (68 loc) 3.83 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 { IconComparison } from "../common/icon-comparison.js"; import { PercentageComparison } from "../common/percentage-comparison.js"; import { useState, useEffect } from "react"; import { deduceDateUrlParams } from "../utils/helpers.js"; const ProductsSoldCountNumbers = ({ current, previous, compareEnabled }) => { return /* @__PURE__ */ jsxs(Grid, { container: true, alignItems: "center", spacing: 2, children: [ /* @__PURE__ */ jsx(Grid, { item: true, children: /* @__PURE__ */ jsx(Heading, { level: "h1", children: current }) }), previous && compareEnabled && /* @__PURE__ */ jsx(Grid, { item: true, children: /* @__PURE__ */ jsxs(Grid, { container: true, alignItems: "center", children: [ /* @__PURE__ */ jsx(Grid, { item: true, children: /* @__PURE__ */ jsx(IconComparison, { current: parseInt(current), previous: previous ? parseInt(previous) : void 0 }) }), previous !== void 0 && /* @__PURE__ */ jsx(Grid, { item: true, children: /* @__PURE__ */ jsx(PercentageComparison, { current, label: "", previous }) }) ] }) }) ] }); }; const ProductsSoldCount = ({ 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/products-analytics/sold-count?${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 (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 == void 0 || data.analytics == void 0 || data.analytics.current == void 0) { return /* @__PURE__ */ jsx(Heading, { level: "h3", children: "Cannot get orders or products" }); } if (data && data.analytics.dateRangeFrom) { return /* @__PURE__ */ jsx(ProductsSoldCountNumbers, { current: data.analytics.current, previous: data.analytics.previous, compareEnabled }); } else { return /* @__PURE__ */ jsx(Heading, { level: "h3", children: "No products for selected orders" }); } }; const ProductsSoldCountCard = ({ 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, alignItems: "center", children: [ /* @__PURE__ */ jsx(Grid, { item: true, children: /* @__PURE__ */ jsx(ShoppingBag, {}) }), /* @__PURE__ */ jsx(Grid, { item: true, children: /* @__PURE__ */ jsx(Heading, { level: "h2", children: "Products sold" }) }) ] }) }), /* @__PURE__ */ jsx(Grid, { item: true, xs: 12, md: 12, children: /* @__PURE__ */ jsx(ProductsSoldCount, { orderStatuses, dateRange, dateRangeCompareTo, compareEnabled }) }) ] }); }; export { ProductsSoldCountCard };