Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix: enhance error handling in various sections

Jakub Sieradzkic13170c7d5cd
diff --git a/packages/admin/src/pages/categories/category-detail/components/category-product-section/category-product-section.tsx b/packages/admin/src/pages/categories/category-detail/components/category-product-section/category-product-section.tsx
index ae9152b3c..50155d3ff 100644
--- a/packages/admin/src/pages/categories/category-detail/components/category-product-section/category-product-section.tsx
+++ b/packages/admin/src/pages/categories/category-detail/components/category-product-section/category-product-section.tsx
@@ -1,10 +1,11 @@
-import { PlusMini } from "@medusajs/icons";
+import { ExclamationCircle, PlusMini } from "@medusajs/icons";
import { HttpTypes } from "@medusajs/types";
import {
Checkbox,
CommandBar,
Container,
Heading,
+ Text,
toast,
usePrompt,
} from "@medusajs/ui";
@@ -103,10 +104,6 @@ export const CategoryProductSection = ({
);
};
- if (isError) {
- throw error;
- }
-
return (
<Container className="divide-y p-0">
<div className="flex items-center justify-between px-6 py-4">
@@ -125,6 +122,14 @@ export const CategoryProductSection = ({
]}
/>
</div>
+ {isError ? (
+ <div className="flex items-center gap-x-2 px-6 py-4">
+ <ExclamationCircle className="text-ui-fg-subtle" />
+ <Text size="small" className="text-ui-fg-subtle">
+ {error?.message || t("general.error")}
+ </Text>
+ </div>
+ ) : (
<_DataTable
table={table}
filters={filters}
@@ -143,6 +148,7 @@ export const CategoryProductSection = ({
message: t("categories.products.list.noRecordsMessage"),
}}
/>
+ )}
<CommandBar open={!!Object.keys(selection).length}>
<CommandBar.Bar>
<CommandBar.Value>
diff --git a/packages/admin/src/pages/locations/location-detail/components/location-fulfillment-providers-section/location-fulfillment-providers-section.tsx b/packages/admin/src/pages/locations/location-detail/components/location-fulfillment-providers-section/location-fulfillment-providers-section.tsx
index 586ed7cf1..50182fe20 100644
--- a/packages/admin/src/pages/locations/location-detail/components/location-fulfillment-providers-section/location-fulfillment-providers-section.tsx
+++ b/packages/admin/src/pages/locations/location-detail/components/location-fulfillment-providers-section/location-fulfillment-providers-section.tsx
@@ -50,7 +50,7 @@ function LocationsFulfillmentProvidersSection({
{fulfillment_providers?.length ? (
<div className="flex flex-col gap-y-4 pt-4" data-testid="location-fulfillment-providers-section-content">
<div className="grid grid-cols-[28px_1fr] items-center gap-x-3 gap-y-3" data-testid="location-fulfillment-providers-section-list">
- {fulfillment_providers?.map((fulfillmentProvider) => {
+ {fulfillment_providers?.map((fulfillmentProvider: { id: string }) => {
return (
<Fragment key={fulfillmentProvider.id}>
<IconAvatar data-testid={`location-fulfillment-providers-section-provider-icon-${fulfillmentProvider.id}`}>
diff --git a/packages/admin/src/pages/locations/location-service-zone-shipping-option-create/location-service-zone-shipping-option-create.tsx b/packages/admin/src/pages/locations/location-service-zone-shipping-option-create/location-service-zone-shipping-option-create.tsx
index 606f48f52..d4a9894e9 100644
--- a/packages/admin/src/pages/locations/location-service-zone-shipping-option-create/location-service-zone-shipping-option-create.tsx
+++ b/packages/admin/src/pages/locations/location-service-zone-shipping-option-create/location-service-zone-shipping-option-create.tsx
@@ -25,7 +25,7 @@ const Root = ({ children }: { children?: ReactNode }) => {
})
const fulfillmentSet = stock_location?.fulfillment_sets?.find(
- (f) => f.id === fset_id
+ (f: { id: string }) => f.id === fset_id
)
if (!isPending && !isFetching && !fulfillmentSet) {
@@ -35,7 +35,7 @@ const Root = ({ children }: { children?: ReactNode }) => {
)
}
- const zone = fulfillmentSet?.service_zones?.find((z) => z.id === zone_id)
+ const zone = fulfillmentSet?.service_zones?.find((z: { id: string }) => z.id === zone_id)
if (!isPending && !isFetching && !zone) {
throw new Response(
diff --git a/packages/admin/src/pages/orders/order-create-edit/components/order-edit-create-form/order-edit-create-form.tsx b/packages/admin/src/pages/orders/order-create-edit/components/order-edit-create-form/order-edit-create-form.tsx
index d0fd2621d..d192cd189 100644
--- a/packages/admin/src/pages/orders/order-create-edit/components/order-edit-create-form/order-edit-create-form.tsx
+++ b/packages/admin/src/pages/orders/order-create-edit/components/order-edit-create-form/order-edit-create-form.tsx
@@ -37,11 +37,9 @@ export const OrderEditCreateForm = ({ order, preview }: ReturnCreateFormProps) =
* FORM
*/
const form = useForm<CreateOrderEditSchemaType>({
- defaultValues: () => {
- return Promise.resolve({
- note: '', // TODO: add note when update edit route is added
- send_notification: false // TODO: not supported in the API ATM
- });
+ defaultValues: {
+ note: '',
+ send_notification: false,
},
resolver: zodResolver(OrderEditCreateSchema)
});
diff --git a/packages/admin/src/pages/orders/order-detail/components/order-activity-section/order-timeline.tsx b/packages/admin/src/pages/orders/order-detail/components/order-activity-section/order-timeline.tsx
index d29e9a7c7..55946e8d0 100644
--- a/packages/admin/src/pages/orders/order-detail/components/order-activity-section/order-timeline.tsx
+++ b/packages/admin/src/pages/orders/order-detail/components/order-activity-section/order-timeline.tsx
@@ -30,6 +30,7 @@ import { useCancelReturn, useReturns } from "../../../../../hooks/api/returns"
import { useDate } from "../../../../../hooks/use-date"
import { getFormattedAddress } from "../../../../../lib/addresses"
import { getStylizedAmount } from "../../../../../lib/money-amount-helpers"
+import { getDisplayFulfillmentItems } from "../../../../../lib/order-item"
import { getPaymentsFromOrder } from "../../../../../lib/orders"
import ActivityItems from "./activity-items"
import ChangeDetailsTooltip from "./change-details-tooltip"
@@ -258,14 +259,18 @@ const useActivityItems = (order: AdminOrder): Activity[] => {
items.push({
title: t("orders.activity.events.fulfillment.created"),
timestamp: fulfillment.created_at,
- children: <FulfillmentCreatedBody fulfillment={fulfillment} />,
+ children: (
+ <FulfillmentCreatedBody fulfillment={fulfillment} itemsMap={itemsMap} />
+ ),
})
if (fulfillment.delivered_at) {
items.push({
title: t("orders.activity.events.fulfillment.delivered"),
timestamp: fulfillment.delivered_at,
- children: <FulfillmentCreatedBody fulfillment={fulfillment} />,
+ children: (
+ <FulfillmentCreatedBody fulfillment={fulfillment} itemsMap={itemsMap} />
+ ),
})
}
@@ -274,7 +279,11 @@ const useActivityItems = (order: AdminOrder): Activity[] => {
title: t("orders.activity.events.fulfillment.shipped"),
timestamp: fulfillment.shipped_at,
children: (
- <FulfillmentCreatedBody fulfillment={fulfillment} isShipment />
+ <FulfillmentCreatedBody
+ fulfillment={fulfillment}
+ isShipment
+ itemsMap={itemsMap}
+ />
),
})
}
@@ -733,14 +742,17 @@ const OrderActivityCollapsible = ({
const FulfillmentCreatedBody = ({
fulfillment,
+ itemsMap,
}: {
fulfillment: AdminFulfillment
+ itemsMap: Map<string, AdminOrderLineItem>
}) => {
const { t } = useTranslation()
- const numberOfItems = fulfillment.items.reduce((acc, item) => {
- return acc + item.quantity
- }, 0)
+ const numberOfItems = getDisplayFulfillmentItems(
+ fulfillment.items || [],
+ itemsMap
+ ).reduce((acc, item) => acc + item.quantity, 0)
return (
<div>
diff --git a/packages/admin/src/pages/orders/order-detail/components/order-fulfillment-section/order-fulfillment-section.tsx b/packages/admin/src/pages/orders/order-detail/components/order-fulfillment-section/order-fulfillment-section.tsx
index 3d63fd7c6..785b0d4cc 100644
--- a/packages/admin/src/pages/orders/order-detail/components/order-fulfillment-section/order-fulfillment-section.tsx
+++ b/packages/admin/src/pages/orders/order-detail/components/order-fulfillment-section/order-fulfillment-section.tsx
@@ -18,6 +18,7 @@ import {
usePrompt,
} from "@medusajs/ui"
import { format } from "date-fns"
+import { useMemo } from "react"
import { useTranslation } from "react-i18next"
import { Link, useNavigate } from "react-router-dom"
import { ActionMenu } from "../../../../../components/common/action-menu"
@@ -30,6 +31,7 @@ import {
import { useStockLocation } from "../../../../../hooks/api/stock-locations"
import { formatProvider } from "../../../../../lib/format-provider"
import { getLocaleAmount } from "../../../../../lib/money-amount-helpers"
+import { getDisplayFulfillmentItems } from "../../../../../lib/order-item"
import { FulfillmentSetType } from "../../../../locations/common/constants"
type OrderFulfillmentSectionProps = {
@@ -214,6 +216,14 @@ const Fulfillment = ({
const navigate = useNavigate()
const showLocation = !!fulfillment.location_id
+ const orderItemsMap = useMemo(
+ () => new Map((order.items || []).map((item) => [item.id, item])),
+ [order.items]
+ )
+ const displayFulfillmentItems = useMemo(
+ () => getDisplayFulfillmentItems(fulfillment.items || [], orderItemsMap),
+ [fulfillment.items, orderItemsMap]
+ )
const isPickUpFulfillment =
fulfillment.shipping_option?.service_zone.fulfillment_set.type ===
@@ -367,7 +377,7 @@ const Fulfillment = ({
{t("orders.fulfillment.itemsLabel")}
</Text>
<ul>
- {fulfillment.items.map((f_item) => (
+ {displayFulfillmentItems.map((f_item) => (
<li key={f_item.line_item_id}>
<Text size="small" leading="compact">
{f_item.quantity}x {f_item.title}
diff --git a/packages/admin/src/pages/orders/order-detail/components/order-summary-section/order-summary-section.tsx b/packages/admin/src/pages/orders/order-detail/components/order-summary-section/order-summary-section.tsx
index 54ff3aec3..dfb5df058 100644
--- a/packages/admin/src/pages/orders/order-detail/components/order-summary-section/order-summary-section.tsx
+++ b/packages/admin/src/pages/orders/order-detail/components/order-summary-section/order-summary-section.tsx
@@ -658,7 +658,7 @@ const CostBreakdown = ({
/>
<Cost
label={
- <div
+ <span
onClick={() => setIsShippingOpen((o) => !o)}
className="flex cursor-pointer items-center gap-1"
>
@@ -668,7 +668,7 @@ const CostBreakdown = ({
transform: `rotate(${isShippingOpen ? 0 : -90}deg)`,
}}
/>
- </div>
+ </span>
}
value={getLocaleAmount(order.shipping_subtotal, order.currency_code)}
/>
@@ -829,7 +829,7 @@ const DiscountAndTotalBreakdown = ({
>
<Cost
label={
- <div
+ <span
onClick={() => hasDiscount && setIsDiscountOpen((o) => !o)}
className={clx("flex items-center gap-1", {
"cursor-pointer": hasDiscount,
@@ -844,7 +844,7 @@ const DiscountAndTotalBreakdown = ({
}}
/>
)}
- </div>
+ </span>
}
value={getLocaleAmount(order.discount_total, order.currency_code)}
/>
@@ -876,7 +876,7 @@ const DiscountAndTotalBreakdown = ({
<>
<Cost
label={
- <div
+ <span
onClick={() => setIsCreditLinesOpen((o) => !o)}
className="flex cursor-pointer items-center gap-1"
>
@@ -886,7 +886,7 @@ const DiscountAndTotalBreakdown = ({
transform: `rotate(${isCreditLinesOpen ? 0 : -90}deg)`,
}}
/>
- </div>
+ </span>
}
value={getLocaleAmount(
order.credit_line_total,
diff --git a/packages/admin/src/pages/price-lists/price-list-detail/components/price-list-configuration-section/price-list-configuration-section.tsx b/packages/admin/src/pages/price-lists/price-list-detail/components/price-list-configuration-section/price-list-configuration-section.tsx
index 56f75fe86..28040cbbd 100644
--- a/packages/admin/src/pages/price-lists/price-list-detail/components/price-list-configuration-section/price-list-configuration-section.tsx
+++ b/packages/admin/src/pages/price-lists/price-list-detail/components/price-list-configuration-section/price-list-configuration-section.tsx
@@ -70,11 +70,7 @@ const CustomerGroupDisplay = ({
}
)
- if (isError) {
- throw error
- }
-
- if (!customerGroupIds?.length) {
+ if (isError || !customerGroupIds?.length) {
return null
}
diff --git a/packages/admin/src/pages/price-lists/price-list-detail/components/price-list-product-section/price-list-product-section.tsx b/packages/admin/src/pages/price-lists/price-list-detail/components/price-list-product-section/price-list-product-section.tsx
index 7d61157b9..ce3c4eecf 100644
--- a/packages/admin/src/pages/price-lists/price-list-detail/components/price-list-product-section/price-list-product-section.tsx
+++ b/packages/admin/src/pages/price-lists/price-list-detail/components/price-list-product-section/price-list-product-section.tsx
@@ -1,6 +1,6 @@
-import { PencilSquare, Plus, Trash } from "@medusajs/icons"
+import { ExclamationCircle, PencilSquare, Plus, Trash } from "@medusajs/icons"
import type { HttpTypes } from "@medusajs/types"
-import { Checkbox, Container, Heading, toast, usePrompt } from "@medusajs/ui"
+import { Checkbox, Container, Heading, Text, toast, usePrompt } from "@medusajs/ui"
import { keepPreviousData } from "@tanstack/react-query"
import type { RowSelectionState } from "@tanstack/react-table"
import { createColumnHelper } from "@tanstack/react-table"
@@ -107,10 +107,6 @@ export const PriceListProductSection = ({
navigate(`products/edit?ids[]=${ids}`)
}
- if (isError) {
- throw error
- }
-
return (
<Container className="divide-y p-0" data-testid="price-list-product-section-container">
<div className="flex items-center justify-between px-6 py-4" data-testid="price-list-product-section-header">
@@ -137,6 +133,14 @@ export const PriceListProductSection = ({
data-testid="price-list-product-section-action-menu"
/>
</div>
+ {isError ? (
+ <div className="flex items-center gap-x-2 px-6 py-4" data-testid="price-list-product-section-error">
+ <ExclamationCircle className="text-ui-fg-subtle" />
+ <Text size="small" className="text-ui-fg-subtle">
+ {error?.message || t("general.error")}
+ </Text>
+ </div>
+ ) : (
<_DataTable
table={table}
filters={filters}
@@ -168,6 +172,7 @@ export const PriceListProductSection = ({
queryObject={raw}
data-testid="price-list-product-section-table"
/>
+ )}
</Container>
)
}
diff --git a/packages/admin/src/pages/product-tags/product-tag-detail/components/product-tag-product-section/product-tag-product-section.tsx b/packages/admin/src/pages/product-tags/product-tag-detail/components/product-tag-product-section/product-tag-product-section.tsx
index a8e4a0711..990db0828 100644
--- a/packages/admin/src/pages/product-tags/product-tag-detail/components/product-tag-product-section/product-tag-product-section.tsx
+++ b/packages/admin/src/pages/product-tags/product-tag-detail/components/product-tag-product-section/product-tag-product-section.tsx
@@ -1,5 +1,6 @@
+import { ExclamationCircle } from "@medusajs/icons"
import type { HttpTypes } from "@medusajs/types"
-import { Container, Heading } from "@medusajs/ui"
+import { Container, Heading, Text } from "@medusajs/ui"
import { useTranslation } from "react-i18next"
import { _DataTable } from "../../../../../components/table/data-table"
import { useProducts } from "../../../../../hooks/api"
@@ -42,15 +43,19 @@ export const ProductTagProductSection = ({
prefix: PREFIX,
})
- if (isError) {
- throw error
- }
-
return (
<Container className="divide-y px-0 py-0" data-testid="product-tag-product-section-container">
<div className="px-6 py-4" data-testid="product-tag-product-section-header">
<Heading level="h2" data-testid="product-tag-product-section-heading">{t("products.domain")}</Heading>
</div>
+ {isError ? (
+ <div className="flex items-center gap-x-2 px-6 py-4" data-testid="product-tag-product-section-error">
+ <ExclamationCircle className="text-ui-fg-subtle" />
+ <Text size="small" className="text-ui-fg-subtle">
+ {error?.message || t("general.error")}
+ </Text>
+ </div>
+ ) : (
<_DataTable
table={table}
filters={filters}
@@ -70,6 +75,7 @@ export const ProductTagProductSection = ({
]}
data-testid="product-tag-product-section-table"
/>
+ )}
</Container>
)
}
diff --git a/packages/admin/src/pages/products/product-create-variant/components/create-product-variant-form/create-product-variant-form.tsx b/packages/admin/src/pages/products/product-create-variant/components/create-product-variant-form/create-product-variant-form.tsx
index 279587780..bc3699379 100644
--- a/packages/admin/src/pages/products/product-create-variant/components/create-product-variant-form/create-product-variant-form.tsx
+++ b/packages/admin/src/pages/products/product-create-variant/components/create-product-variant-form/create-product-variant-form.tsx
@@ -60,7 +60,7 @@ export const CreateProductVariantForm = ({
}
return regions.reduce(
- (acc, reg) => {
+ (acc: Record<string, string>, reg: { id: string; currency_code: string }) => {
acc[reg.id] = reg.currency_code
return acc
},
@@ -136,11 +136,11 @@ export const CreateProductVariantForm = ({
return ret
})
- .filter(Boolean),
+ .filter((x): x is AdminCreateProductVariantPrice => Boolean(x)),
inventory_items: (data.inventory || [])
.map((i) => {
if (!i.required_quantity || !i.inventory_item_id) {
- return false
+ return false as const
}
return {
@@ -148,7 +148,7 @@ export const CreateProductVariantForm = ({
required_quantity: castNumber(i.required_quantity),
}
})
- .filter(Boolean),
+ .filter((x): x is Exclude<typeof x, false> => Boolean(x)),
},
{
onSuccess: () => {
diff --git a/packages/core-plugin/src/api/admin/middlewares.ts b/packages/core-plugin/src/api/admin/middlewares.ts
index 1d97e30fd..99005a009 100644
--- a/packages/core-plugin/src/api/admin/middlewares.ts
+++ b/packages/core-plugin/src/api/admin/middlewares.ts
@@ -13,41 +13,43 @@ import { AdminGetOrderGroupParams } from "./order-groups/validators"
import { adminPayoutsMiddlewares } from "./payouts/middlewares"
import { adminSellersMiddlewares } from "./sellers/middlewares"
import { adminCommissionRatesMiddlewares } from "./commission-rates/middlewares"
-import { AdminGetProductsParams } from "./products/validators"
-import { listProductQueryConfig } from "@medusajs/medusa/api/admin/products/query-config"
-import { AdminGetOrdersParams } from "./orders/validators"
-import { listTransformQueryConfig as listOrderQueryConfig } from "@medusajs/medusa/api/admin/orders/query-config"
-const maybeApplySellerProductFilter = (
+const extractSellerIdFromQuery = (
req: AuthenticatedMedusaRequest,
- res: MedusaResponse,
+ _res: MedusaResponse,
next: MedusaNextFunction
) => {
- if (!req.filterableFields.seller_id) {
- return next()
+ const sellerId = req.query.seller_id
+ if (sellerId) {
+ delete req.query.seller_id
+ ;(req as any).__seller_id = sellerId
}
-
- return maybeApplyLinkFilter({
- entryPoint: "product_seller",
- resourceId: "product_id",
- filterableField: "seller_id",
- })(req, res, next)
+ next()
}
-const maybeApplySellerOrderFilter = (
- req: AuthenticatedMedusaRequest,
- res: MedusaResponse,
- next: MedusaNextFunction
+const injectSellerIdFilter = (
+ entryPoint: string,
+ resourceId: string
) => {
- if (!req.filterableFields.seller_id) {
- return next()
- }
+ return (
+ req: AuthenticatedMedusaRequest,
+ res: MedusaResponse,
+ next: MedusaNextFunction
+ ) => {
+ const sellerId = (req as any).__seller_id
+ if (!sellerId) {
+ return next()
+ }
+
+ delete (req as any).__seller_id
+ req.filterableFields.seller_id = sellerId
- return maybeApplyLinkFilter({
- entryPoint: "order_seller",
- resourceId: "order_id",
- filterableField: "seller_id",
- })(req, res, next)
+ return maybeApplyLinkFilter({
+ entryPoint,
+ resourceId,
+ filterableField: "seller_id",
+ })(req, res, next)
+ }
}
export const adminMiddlewares: MiddlewareRoute[] = [
@@ -65,26 +67,22 @@ export const adminMiddlewares: MiddlewareRoute[] = [
...adminPayoutsMiddlewares,
...adminSellersMiddlewares,
...adminCommissionRatesMiddlewares,
+ {
+ matcher: "/admin/products",
+ middlewares: [extractSellerIdFromQuery],
+ },
+ {
+ matcher: "/admin/orders",
+ middlewares: [extractSellerIdFromQuery],
+ },
{
method: ["GET"],
matcher: "/admin/products",
- middlewares: [
- validateAndTransformQuery(
- AdminGetProductsParams,
- listProductQueryConfig
- ),
- maybeApplySellerProductFilter,
- ],
+ middlewares: [injectSellerIdFilter("product_seller", "product_id")],
},
{
method: ["GET"],
matcher: "/admin/orders",
- middlewares: [
- validateAndTransformQuery(
- AdminGetOrdersParams,
- listOrderQueryConfig
- ),
- maybeApplySellerOrderFilter,
- ],
+ middlewares: [injectSellerIdFilter("order_seller", "order_id")],
},
]