The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
refactor: replace useStoreCurrencies with useStore in various components and update currency
Jakub Sieradzki705be7190255
diff --git a/packages/vendor/src/hooks/api/store.tsx b/packages/vendor/src/hooks/api/store.tsxindex cddff9222..c467a016b 100644--- a/packages/vendor/src/hooks/api/store.tsx+++ b/packages/vendor/src/hooks/api/store.tsx@@ -10,8 +10,8 @@ import { HttpTypes } from "@medusajs/types"import { queryClient } from "../../lib/query-client"import { queryKeysFactory } from "../../lib/query-key-factory"import { pricePreferencesQueryKeys } from "./price-preferences"-import { fetchQuery } from "../../lib/client"-import { ClientError } from "@mercurjs/client"+import { fetchQuery, sdk } from "../../lib/client"+import { ClientError, InferClientInput } from "@mercurjs/client"const STORE_QUERY_KEY = "store" as constexport const storeQueryKeys = queryKeysFactory(STORE_QUERY_KEY)@@ -38,25 +38,16 @@ export async function retrieveActiveStore(}export const useStore = (- query?: HttpTypes.SelectParams,- options?: Omit<- UseQueryOptions<- HttpTypes.AdminStoreResponse,- ClientError,- HttpTypes.AdminStoreResponse,- QueryKey- >,- "queryFn" | "queryKey"- >+ query?: InferClientInput<typeof sdk.vendor.stores.query>,) => {const { data, ...rest } = useQuery({- queryFn: () => retrieveActiveStore(query),+ queryFn: () => sdk.vendor.stores.query({...query})+ .then((res) => res.stores![0]!),queryKey: storeQueryKeys.details(),- ...options,})return {- ...data,+ store: data,...rest,}}diff --git a/packages/vendor/src/hooks/api/use-store-currencies.tsx b/packages/vendor/src/hooks/api/use-store-currencies.tsxdeleted file mode 100644index b8c167352..000000000--- a/packages/vendor/src/hooks/api/use-store-currencies.tsx+++ /dev/null@@ -1,28 +0,0 @@-import { HttpTypes } from "@medusajs/types"-import { useCurrencies } from "./currencies"-import { useStore } from "./store"--/**- * Returns store's supported currencies with a fallback to all available currencies.- * The vendor /sellers/me endpoint may not return supported_currencies,- * so we fall back to the /vendor/currencies endpoint.- */-export const useStoreCurrencies = () => {- const { store, isPending: isStorePending } = useStore()- const storeCurrencies = store?.supported_currencies-- const { currencies: allCurrencies, isPending: isCurrenciesPending } =- useCurrencies({ limit: 999 }, { enabled: !storeCurrencies && !isStorePending })-- const currencies: HttpTypes.AdminStoreCurrency[] | undefined =- storeCurrencies ??- (allCurrencies?.map((c) => ({- currency_code: c.code,- is_default: false,- currency: c,- })) as HttpTypes.AdminStoreCurrency[] | undefined)-- const isPending = isStorePending || (!storeCurrencies && isCurrenciesPending)-- return { currencies, isPending }-}diff --git a/packages/vendor/src/pages/inventory/[id]/index.tsx b/packages/vendor/src/pages/inventory/[id]/index.tsxindex 9cb3df064..d19898b1c 100644--- a/packages/vendor/src/pages/inventory/[id]/index.tsx+++ b/packages/vendor/src/pages/inventory/[id]/index.tsx@@ -77,15 +77,7 @@ export const Component = () => {}return (- <TwoColumnPage- widgets={{- after: getWidgets("inventory_item.details.after"),- before: getWidgets("inventory_item.details.before"),- sideAfter: getWidgets("inventory_item.details.side.after"),- sideBefore: getWidgets("inventory_item.details.side.before"),- }}- data={inventory_item}- >+ <TwoColumnPage data={inventory_item} ><TwoColumnPage.Main><InventoryItemGeneralSection inventoryItem={inventory_item} /><InventoryItemLocationLevelsSection inventoryItem={inventory_item} />diff --git a/packages/vendor/src/pages/price-lists/common/hooks/use-price-list-currency-data.tsx b/packages/vendor/src/pages/price-lists/common/hooks/use-price-list-currency-data.tsxindex f439861f2..c9baa6930 100644--- a/packages/vendor/src/pages/price-lists/common/hooks/use-price-list-currency-data.tsx+++ b/packages/vendor/src/pages/price-lists/common/hooks/use-price-list-currency-data.tsx@@ -1,23 +1,18 @@-import { HttpTypes } from "@medusajs/types"-import { useRegions } from "@hooks/api/regions"-import { useStoreCurrencies } from "@hooks/api/use-store-currencies"--type UsePriceListCurrencyDataReturn =- | {- isReady: false- currencies: undefined- regions: undefined- pricePreferences: undefined- }- | {- isReady: true- currencies: HttpTypes.AdminStoreCurrency[]- regions: HttpTypes.AdminRegion[]- pricePreferences: HttpTypes.AdminPricePreference[]- }+import { useRegions } from "../../../../hooks/api/regions"+import { useStore } from "../../../../hooks/api/store"+import { usePricePreferences } from "../../../../hooks/api/price-preferences"-export const usePriceListCurrencyData = (): UsePriceListCurrencyDataReturn => {- const { currencies, isPending: isCurrenciesPending } = useStoreCurrencies()+export const usePriceListCurrencyData = () => {+ const {+ store,+ isPending: isStorePending,+ isError: isStoreError,+ error: storeError,+ } = useStore({+ fields: "+supported_currencies",+ })++ const currencies = store?.supported_currenciesconst {regions,@@ -29,12 +24,33 @@ export const usePriceListCurrencyData = (): UsePriceListCurrencyDataReturn => {limit: 999,})- const isReady = !!currencies && !!regions && !isCurrenciesPending && !isRegionsPending+ const {+ price_preferences: pricePreferences,+ isPending: isPreferencesPending,+ isError: isPreferencesError,+ error: preferencesError,+ } = usePricePreferences({})++ const isReady =+ !!currencies &&+ !!regions &&+ !!pricePreferences &&+ !isStorePending &&+ !isRegionsPending &&+ !isPreferencesPendingif (isRegionsError) {throw regionsError}+ if (isStoreError) {+ throw storeError+ }++ if (isPreferencesError) {+ throw preferencesError+ }+if (!isReady) {return {regions: undefined,@@ -44,10 +60,5 @@ export const usePriceListCurrencyData = (): UsePriceListCurrencyDataReturn => {}}- return {- regions,- currencies,- pricePreferences: [],- isReady,- }-}+ return { regions, currencies, pricePreferences, isReady }+}\ No newline at end of filediff --git a/packages/vendor/src/pages/products/create/product-create-variants-form/product-create-variants-form.tsx b/packages/vendor/src/pages/products/create/product-create-variants-form/product-create-variants-form.tsxindex 98344384c..b6c5412b1 100644--- a/packages/vendor/src/pages/products/create/product-create-variants-form/product-create-variants-form.tsx+++ b/packages/vendor/src/pages/products/create/product-create-variants-form/product-create-variants-form.tsx@@ -9,7 +9,6 @@ import {DataGrid,} from "@components/data-grid"import { useRouteModal } from "@components/modals"-import { useStoreCurrencies } from "@hooks/api/use-store-currencies"import {ProductCreateOptionSchema,ProductCreateVariantSchema,@@ -34,11 +33,10 @@ export const ProductCreateVariantsForm = ({pricePreferences,}: ProductCreateVariantsFormProps) => {const { setCloseOnEscape } = useRouteModal()- const { currencies: storeCurrencies } = useStoreCurrencies()const currencyCodes = useMemo(- () => (store?.supported_currencies ?? storeCurrencies)?.map((c) => c.currency_code) || [],- [store, storeCurrencies]+ () => (store?.supported_currencies)?.map((c) => c.currency_code) || [],+ [store])const variants = useWatch({diff --git a/packages/vendor/src/pages/settings/regions/create/index.tsx b/packages/vendor/src/pages/settings/regions/create/index.tsxindex eb19f30cb..8c14117da 100644--- a/packages/vendor/src/pages/settings/regions/create/index.tsx+++ b/packages/vendor/src/pages/settings/regions/create/index.tsx@@ -1,13 +1,15 @@import { RouteFocusModal } from "@components/modals/route-focus-modal"import { usePaymentProviders } from "@hooks/api/payments"-import { useStoreCurrencies } from "@hooks/api/use-store-currencies"+import { useStore } from "@hooks/api/store"import { currencies } from "@lib/data/currencies"import { CreateRegionForm } from "./_components/create-region-form"const RegionCreate = () => {- const { currencies: storeCurrencyList, isPending: isLoading } = useStoreCurrencies()+ const { store, isPending: isLoading, } = useStore({+ fields: "+supported_currencies",+ });- const storeCurrencies = (storeCurrencyList ?? []).map(+ const storeCurrencies = (store?.supported_currencies ?? []).map((c) => currencies[c.currency_code.toUpperCase()])const { payment_providers: paymentProviders = [] } = usePaymentProviders({@@ -16,7 +18,7 @@ const RegionCreate = () => {return (<RouteFocusModal>- {!isLoading && storeCurrencyList && (+ {!isLoading && (<CreateRegionFormcurrencies={storeCurrencies}paymentProviders={paymentProviders}diff --git a/packages/vendor/src/pages/settings/seller/index.tsx b/packages/vendor/src/pages/settings/seller/index.tsxindex 989b7eaca..88e9d9b14 100644--- a/packages/vendor/src/pages/settings/seller/index.tsx+++ b/packages/vendor/src/pages/settings/seller/index.tsx@@ -6,7 +6,6 @@ import { sellerLoader } from "./loader"import { SingleColumnPageSkeleton } from "@components/common/skeleton"import { SingleColumnPage } from "@components/layout/pages"-import { useDashboardExtension } from "@/extensions"const SellerDetail = () => {const initialData = useLoaderData() as Awaited<ReturnType<typeof sellerLoader>>@@ -15,7 +14,6 @@ const SellerDetail = () => {initialData,})- const { getWidgets } = useDashboardExtension()if (isPending || !store) {return <SingleColumnPageSkeleton sections={1} showJSON showMetadata />@@ -27,10 +25,6 @@ const SellerDetail = () => {return (<SingleColumnPage- widgets={{- before: getWidgets("seller.details.before"),- after: getWidgets("seller.details.after"),- }}data={store}hasOutletshowMetadata