@rsc-labs/medusa-store-analytics-v2
Version:
Get analytics data about your store
63 lines (62 loc) • 3.3 kB
JavaScript
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 { RepeatCustomerRateNummber } from "./customers-repeat-customer-rate-number.js";
import { OrderFrequencyDistribution } from "./order-frequency-distribution.js";
import { deduceDateUrlParams } from "../../utils/helpers.js";
import { useState, useEffect } from "react";
const RepeatCustomerRateDetails = ({ 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/customers-analytics/repeat-customer-rate?${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 && data.analytics == void 0) {
return /* @__PURE__ */ jsx(Heading, { level: "h3", children: "Cannot get orders or customers" });
}
if (data && data.analytics.dateRangeFrom) {
return /* @__PURE__ */ jsxs(Grid, { container: true, children: [
/* @__PURE__ */ jsx(Grid, { item: true, xs: 12, md: 12, children: /* @__PURE__ */ jsx(RepeatCustomerRateNummber, { repeatCustomerRateResponse: data, compareEnabled }) }),
/* @__PURE__ */ jsx(Grid, { item: true, xs: 12, md: 12, children: /* @__PURE__ */ jsx(OrderFrequencyDistribution, { repeatCustomerRateResponse: data, compareEnabled }) })
] });
} else {
return /* @__PURE__ */ jsx(Heading, { level: "h3", children: "No orders or customers" });
}
};
const CustomersRepeatCustomerRate = ({ 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: "Repeat customer rate" }) })
] }) }),
/* @__PURE__ */ jsx(Grid, { item: true, xs: 12, md: 12, children: /* @__PURE__ */ jsx(RepeatCustomerRateDetails, { orderStatuses, dateRange, dateRangeCompareTo, compareEnabled }) })
] });
};
export {
CustomersRepeatCustomerRate
};