UNPKG

@rsc-labs/medusa-store-analytics

Version:
57 lines (56 loc) 4.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProductsSoldCountCard = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); /* * Copyright 2024 RSC-Labs, https://rsoftcon.com/ * * MIT License * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const ui_1 = require("@medusajs/ui"); const icons_1 = require("@medusajs/icons"); const material_1 = require("@mui/material"); const medusa_react_1 = require("medusa-react"); const icon_comparison_1 = require("../common/icon-comparison"); const percentage_comparison_1 = require("../common/percentage-comparison"); const ProductsSoldCountNumbers = ({ current, previous, compareEnabled }) => { return ((0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, alignItems: 'center', spacing: 2, children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Heading, { level: "h1", children: current }) }), previous && compareEnabled && (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, alignItems: 'center', children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(icon_comparison_1.IconComparison, { current: parseInt(current), previous: previous ? parseInt(previous) : undefined }) }), previous !== undefined && (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(percentage_comparison_1.PercentageComparison, { current: current, label: "", previous: previous }) })] }) })] })); }; const ProductsSoldCount = ({ orderStatuses, dateRange, dateRangeCompareTo, compareEnabled }) => { var _a, _b; const { data, isLoading, isError, error } = (0, medusa_react_1.useAdminCustomQuery)(`/products-analytics/sold-count`, [orderStatuses, dateRange, dateRangeCompareTo], { orderStatuses: Object.values(orderStatuses), dateRangeFrom: dateRange ? dateRange.from.getTime() : undefined, dateRangeTo: dateRange ? dateRange.to.getTime() : undefined, dateRangeFromCompareTo: dateRangeCompareTo ? dateRangeCompareTo.from.getTime() : undefined, dateRangeToCompareTo: dateRangeCompareTo ? dateRangeCompareTo.to.getTime() : undefined, }); if (isLoading) { return (0, jsx_runtime_1.jsx)(material_1.CircularProgress, { size: 12 }); } if (isError) { const trueError = error; const errorText = `Error when loading data. It shouldn't have happened - please raise an issue. For developer: ${(_b = (_a = trueError === null || trueError === void 0 ? void 0 : trueError.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message}`; return (0, jsx_runtime_1.jsx)(ui_1.Alert, { variant: "error", children: errorText }); } if (data.analytics == undefined || data.analytics.current == undefined) { return (0, jsx_runtime_1.jsx)(ui_1.Heading, { level: "h3", children: "Cannot get orders or products" }); } if (data.analytics.dateRangeFrom) { return (0, jsx_runtime_1.jsx)(ProductsSoldCountNumbers, { current: data.analytics.current, previous: data.analytics.previous, compareEnabled: compareEnabled }); } else { return (0, jsx_runtime_1.jsx)(ui_1.Heading, { level: "h3", children: "No products for selected orders" }); } }; const ProductsSoldCountCard = ({ orderStatuses, dateRange, dateRangeCompareTo, compareEnabled }) => { return ((0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, paddingBottom: 2, spacing: 3, children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, xs: 12, md: 12, children: (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, spacing: 2, alignItems: 'center', children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(icons_1.ShoppingBag, {}) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Heading, { level: "h2", children: "Products sold" }) })] }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, xs: 12, md: 12, children: (0, jsx_runtime_1.jsx)(ProductsSoldCount, { orderStatuses: orderStatuses, dateRange: dateRange, dateRangeCompareTo: dateRangeCompareTo, compareEnabled: compareEnabled }) })] })); }; exports.ProductsSoldCountCard = ProductsSoldCountCard;