From 62dacdda75dc40298dd78030d54dce34a3fff7e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Tue, 25 Jun 2024 21:19:09 +0200 Subject: [PATCH] fix: refreshing inventory locations list (#7828) --- .../components/manage-locations-form.tsx | 13 +++++++++++-- .../manage-locations/manage-locations-drawer.tsx | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/admin-next/dashboard/src/routes/inventory/inventory-detail/components/manage-locations/components/manage-locations-form.tsx b/packages/admin-next/dashboard/src/routes/inventory/inventory-detail/components/manage-locations/components/manage-locations-form.tsx index edb226e652..03acfa2334 100644 --- a/packages/admin-next/dashboard/src/routes/inventory/inventory-detail/components/manage-locations/components/manage-locations-form.tsx +++ b/packages/admin-next/dashboard/src/routes/inventory/inventory-detail/components/manage-locations/components/manage-locations-form.tsx @@ -13,6 +13,7 @@ import { useBatchUpdateInventoryLevels } from "../../../../../../hooks/api/inven import { useFieldArray, useForm } from "react-hook-form" import { LocationItem } from "./location-item" +import { useEffect, useMemo } from "react" type EditInventoryItemAttributeFormProps = { item: AdminInventoryItem @@ -46,8 +47,9 @@ export const ManageLocationsForm = ({ item, locations, }: EditInventoryItemAttributeFormProps) => { - const existingLocationLevels = new Set( - item.location_levels?.map((l) => l.location_id) ?? [] + const existingLocationLevels = useMemo( + () => new Set(item.location_levels?.map((l) => l.location_id) ?? []), + item.location_levels ) const { t } = useTranslation() @@ -63,6 +65,13 @@ export const ManageLocationsForm = ({ name: "locations", }) + useEffect(() => { + form.setValue( + "locations", + getDefaultValues(locations, existingLocationLevels).locations + ) + }, [existingLocationLevels, locations]) + const { mutateAsync } = useBatchUpdateInventoryLevels(item.id) const handleSubmit = form.handleSubmit(async ({ locations }) => { diff --git a/packages/admin-next/dashboard/src/routes/inventory/inventory-detail/components/manage-locations/manage-locations-drawer.tsx b/packages/admin-next/dashboard/src/routes/inventory/inventory-detail/components/manage-locations/manage-locations-drawer.tsx index 6145c737b1..22800bf6d0 100644 --- a/packages/admin-next/dashboard/src/routes/inventory/inventory-detail/components/manage-locations/manage-locations-drawer.tsx +++ b/packages/admin-next/dashboard/src/routes/inventory/inventory-detail/components/manage-locations/manage-locations-drawer.tsx @@ -1,10 +1,11 @@ import { Heading } from "@medusajs/ui" +import { useTranslation } from "react-i18next" + import { ManageLocationsForm } from "./components/manage-locations-form" import { RouteDrawer } from "../../../../../components/route-modal" import { useInventoryItem } from "../../../../../hooks/api/inventory" import { useParams } from "react-router-dom" import { useStockLocations } from "../../../../../hooks/api/stock-locations" -import { useTranslation } from "react-i18next" export const ManageLocationsDrawer = () => { const { id } = useParams()