@rsc-labs/medusa-store-analytics-v2
Version:
Get analytics data about your store
59 lines (58 loc) • 2.95 kB
JavaScript
import { jsxs, jsx } from "react/jsx-runtime";
import { Heading, Alert } from "@medusajs/ui";
import { Grid, CircularProgress } from "@mui/material";
import { Cash } from "@medusajs/icons";
import { OrdersPaymentProviderPieChart } from "./orders-payment-provider-chart.js";
import { useState, useEffect } from "react";
import { deduceDateUrlParams } from "../utils/helpers.js";
const OrdersPaymentProviderDetails = ({ 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]);
useEffect(() => {
if (!isLoading) {
return;
}
fetch(`/admin/orders-analytics/payment-provider?${deduceDateUrlParams(dateRange, dateRangeCompareTo).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" });
}
if (data && data.analytics.dateRangeFrom) {
return /* @__PURE__ */ jsx(OrdersPaymentProviderPieChart, { ordersPaymentProviderResponse: data, compareEnabled });
} else {
return /* @__PURE__ */ jsx(Heading, { level: "h3", children: "No orders" });
}
};
const OrdersPaymentProviderCard = ({ 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(Cash, {}) }),
/* @__PURE__ */ jsx(Grid, { item: true, children: /* @__PURE__ */ jsx(Heading, { level: "h2", children: "Payment provider popularity" }) })
] }) }),
/* @__PURE__ */ jsx(Grid, { item: true, xs: 12, md: 12, children: /* @__PURE__ */ jsx(Grid, { container: true, direction: "column", alignItems: "center", paddingTop: 3, children: /* @__PURE__ */ jsx(Grid, { item: true, children: /* @__PURE__ */ jsx(OrdersPaymentProviderDetails, { dateRange, dateRangeCompareTo, compareEnabled }) }) }) })
] });
};
export {
OrdersPaymentProviderCard
};