From f7e98298815b0ffb4ce1b6d2d2e1f26ca5e01049 Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Thu, 2 Nov 2023 17:05:08 +0100 Subject: [PATCH] fix(admin-ui): Prevent oversized product request in Price List domain (#5535) * fix(admin-ui): Prevent oversized product request in Price List domain * add changeset --- .changeset/wicked-worms-kneel.md | 5 + .../ui/src/domain/pricing/edit/edit.tsx | 4 +- .../edit/prices/add-products-modal.tsx | 106 +++++++++-------- .../price-list-prices-form.tsx | 4 +- .../use-prices-form-data.tsx | 16 ++- .../price-list-products-form.tsx | 2 +- .../ui/src/domain/pricing/new/new.tsx | 108 +++++++++++------- 7 files changed, 147 insertions(+), 98 deletions(-) create mode 100644 .changeset/wicked-worms-kneel.md diff --git a/.changeset/wicked-worms-kneel.md b/.changeset/wicked-worms-kneel.md new file mode 100644 index 0000000000..e03cc88f7e --- /dev/null +++ b/.changeset/wicked-worms-kneel.md @@ -0,0 +1,5 @@ +--- +"@medusajs/admin-ui": patch +--- + +fix(admin-ui): Fix issue were Price List domain would fetch all products on initial load" diff --git a/packages/admin-ui/ui/src/domain/pricing/edit/edit.tsx b/packages/admin-ui/ui/src/domain/pricing/edit/edit.tsx index c3ac16b1b2..985c6f2676 100644 --- a/packages/admin-ui/ui/src/domain/pricing/edit/edit.tsx +++ b/packages/admin-ui/ui/src/domain/pricing/edit/edit.tsx @@ -59,11 +59,11 @@ const PriceListEdit = () => { ) })} {getWidgets("price_list.details.after").map((w, i) => { diff --git a/packages/admin-ui/ui/src/domain/pricing/edit/prices/add-products-modal.tsx b/packages/admin-ui/ui/src/domain/pricing/edit/prices/add-products-modal.tsx index 13e7e4f602..002c8d1995 100644 --- a/packages/admin-ui/ui/src/domain/pricing/edit/prices/add-products-modal.tsx +++ b/packages/admin-ui/ui/src/domain/pricing/edit/prices/add-products-modal.tsx @@ -9,11 +9,11 @@ import { Text, usePrompt, } from "@medusajs/ui" +import { useAdminCreatePriceListPrices } from "medusa-react" import * as React from "react" import { useForm } from "react-hook-form" -import * as z from "zod" -import { useAdminCreatePriceListPrices } from "medusa-react" import { useTranslation } from "react-i18next" +import * as z from "zod" import { Form } from "../../../../components/helpers/form" import useNotification from "../../../../hooks/use-notification" @@ -621,52 +621,68 @@ const AddProductsModal = ({ productIds={productIds} /> - {isLoading ? ( -
- -
- ) : isError || isNotFound ? ( -
-
- - - {t( - "price-list-add-products-modal-error", - "An error occurred while preparing the form. Reload the page and try again. If the issue persists, try again later." - )} - + + {isLoading ? ( +
+
-
- ) : ( - - - +
+ + + {t( + "price-list-add-products-modal-error", + "An error occurred while preparing the form. Reload the page and try again. If the issue persists, try again later." + )} + +
+
+ ) : ( + + )} + + {product && ( + + {isLoading ? ( +
+ +
+ ) : isError || isNotFound ? ( +
+
+ + + {t( + "price-list-add-products-modal-error", + "An error occurred while preparing the form. Reload the page and try again. If the issue persists, try again later." + )} + +
+
+ ) : ( + -
- {product && ( - - - )} - + )} diff --git a/packages/admin-ui/ui/src/domain/pricing/forms/price-list-prices-form/price-list-prices-form.tsx b/packages/admin-ui/ui/src/domain/pricing/forms/price-list-prices-form/price-list-prices-form.tsx index aadb88dd82..6b0ec46d3b 100644 --- a/packages/admin-ui/ui/src/domain/pricing/forms/price-list-prices-form/price-list-prices-form.tsx +++ b/packages/admin-ui/ui/src/domain/pricing/forms/price-list-prices-form/price-list-prices-form.tsx @@ -5,11 +5,11 @@ import { useAdminProducts } from "medusa-react" import * as React from "react" import { useTranslation } from "react-i18next" +import { Form } from "../../../../components/helpers/form" import { useDebounce } from "../../../../hooks/use-debounce" import { NestedForm } from "../../../../utils/nested-form" import { ProductFilter, ProductFilterMenu } from "../../components" import { PriceListPricesSchema } from "./types" -import { Form } from "../../../../components/helpers/form" type PriceListPricesFormProps = { form: NestedForm @@ -46,13 +46,13 @@ const PriceListPricesForm = ({ { id: productIds, q: debouncedQuery, - limit: productIds?.length, price_list_id: priceListId ? [priceListId] : undefined, ...filters, expand: "variants.prices", }, { keepPreviousData: true, + enabled: !!productIds?.length, } ) diff --git a/packages/admin-ui/ui/src/domain/pricing/forms/price-list-prices-form/use-prices-form-data.tsx b/packages/admin-ui/ui/src/domain/pricing/forms/price-list-prices-form/use-prices-form-data.tsx index 063399b71f..c6f457d595 100644 --- a/packages/admin-ui/ui/src/domain/pricing/forms/price-list-prices-form/use-prices-form-data.tsx +++ b/packages/admin-ui/ui/src/domain/pricing/forms/price-list-prices-form/use-prices-form-data.tsx @@ -5,11 +5,17 @@ import * as React from "react" type UsePricesFormDataProps = { priceList?: PriceList productIds: string[] + enable?: { + products?: boolean + } } const usePricesFormData = ({ priceList, productIds, + enable = { + products: true, + }, }: UsePricesFormDataProps) => { const { products, @@ -18,11 +24,11 @@ const usePricesFormData = ({ } = useAdminProducts( { id: productIds, - limit: productIds?.length, price_list_id: priceList ? [priceList.id] : undefined, }, { keepPreviousData: true, + enabled: !!productIds?.length && enable.products, } ) @@ -53,9 +59,11 @@ const usePricesFormData = ({ } ) - const isLoading = isLoadingProducts || isLoadingStore || isLoadingRegions - const isError = isErrorProducts || isStoreError || isErrorRegions - const isNotFound = !products || !regions || !currencies + const isLoading = + (enable.products && isLoadingProducts) || isLoadingStore || isLoadingRegions + const isError = + (enable.products && isErrorProducts) || isStoreError || isErrorRegions + const isNotFound = (enable.products && !products) || !regions || !currencies return { isError, diff --git a/packages/admin-ui/ui/src/domain/pricing/forms/price-list-products-form/price-list-products-form.tsx b/packages/admin-ui/ui/src/domain/pricing/forms/price-list-products-form/price-list-products-form.tsx index d315013477..1dfc30dd98 100644 --- a/packages/admin-ui/ui/src/domain/pricing/forms/price-list-products-form/price-list-products-form.tsx +++ b/packages/admin-ui/ui/src/domain/pricing/forms/price-list-products-form/price-list-products-form.tsx @@ -280,7 +280,7 @@ const PriceListProductsForm = ({ const { products, count, isLoading, isError } = useAdminProducts( { - limit: PAGE_SIZE, + limit: 20, offset, expand: "variants,sales_channels,collection", q: debouncedQuery, diff --git a/packages/admin-ui/ui/src/domain/pricing/new/new.tsx b/packages/admin-ui/ui/src/domain/pricing/new/new.tsx index 94e453b8e8..79d578a116 100644 --- a/packages/admin-ui/ui/src/domain/pricing/new/new.tsx +++ b/packages/admin-ui/ui/src/domain/pricing/new/new.tsx @@ -11,7 +11,6 @@ import * as React from "react" import { useForm, useWatch } from "react-hook-form" import * as z from "zod" -import { ExclamationCircle, Spinner } from "@medusajs/icons" import { Product } from "@medusajs/medusa" import { useAdminCreatePriceList } from "medusa-react" import { Form } from "../../../components/helpers/form" @@ -41,6 +40,7 @@ import { type PriceListProductPricesSchema, } from "../forms/price-list-product-prices-form" +import { ExclamationCircle, Spinner } from "@medusajs/icons" import { useTranslation } from "react-i18next" import { PriceListProductsForm, @@ -159,6 +159,9 @@ const PriceListNew = () => { const { isLoading, isError, isNotFound, regions, currencies } = usePricesFormData({ productIds: selectedIds, + enable: { + products: false, + }, }) const onCloseModal = React.useCallback(() => { @@ -690,52 +693,69 @@ const PriceListNew = () => { > - {isLoading ? ( -
- -
- ) : isError || isNotFound ? ( -
-
- - - {t( - "price-list-new-form-error-loading-products", - "An error occurred while preparing the form. Reload the page and try again. If the issue persists, try again later." - )} - + + + {isLoading ? ( +
+
-
- ) : ( - - - +
+ + + {t( + "price-list-new-form-error-loading-products", + "An error occurred while preparing the form. Reload the page and try again. If the issue persists, try again later." + )} + +
+
+ ) : ( + + )} + + {product && ( + + {isLoading ? ( +
+ +
+ ) : isError || isNotFound ? ( +
+
+ + + {t( + "price-list-new-form-error-loading-products", + "An error occurred while preparing the form. Reload the page and try again. If the issue persists, try again later." + )} + +
+
+ ) : ( + -
- {product && ( - - - )} - + )}