fix(dashboard): payment providers select (#13592)
**What** - use lazy loading for payment providers select on region create/edit
This commit is contained in:
+18
-10
@@ -17,7 +17,7 @@ import { useForm, useWatch } from "react-hook-form"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import * as zod from "zod"
|
||||
|
||||
import { PaymentProviderDTO, RegionCountryDTO } from "@medusajs/types"
|
||||
import { RegionCountryDTO } from "@medusajs/types"
|
||||
|
||||
import { Form } from "../../../../../components/common/form"
|
||||
import { Combobox } from "../../../../../components/inputs/combobox"
|
||||
@@ -38,10 +38,11 @@ import { useCountries } from "../../../common/hooks/use-countries"
|
||||
import { useCountryTableColumns } from "../../../common/hooks/use-country-table-columns"
|
||||
import { useCountryTableQuery } from "../../../common/hooks/use-country-table-query"
|
||||
import { useDocumentDirection } from "../../../../../hooks/use-document-direction"
|
||||
import { useComboboxData } from "../../../../../hooks/use-combobox-data"
|
||||
import { sdk } from "../../../../../lib/client"
|
||||
|
||||
type CreateRegionFormProps = {
|
||||
currencies: CurrencyInfo[]
|
||||
paymentProviders: PaymentProviderDTO[]
|
||||
}
|
||||
|
||||
const CreateRegionSchema = zod.object({
|
||||
@@ -58,10 +59,7 @@ const PAGE_SIZE = 50
|
||||
|
||||
const STACKED_MODAL_ID = "countries-modal"
|
||||
|
||||
export const CreateRegionForm = ({
|
||||
currencies,
|
||||
paymentProviders,
|
||||
}: CreateRegionFormProps) => {
|
||||
export const CreateRegionForm = ({ currencies }: CreateRegionFormProps) => {
|
||||
const { setIsOpen } = useStackedModal()
|
||||
const [rowSelection, setRowSelection] = useState<RowSelectionState>({})
|
||||
const { handleSuccess } = useRouteModal()
|
||||
@@ -181,6 +179,17 @@ export const CreateRegionForm = ({
|
||||
setRowSelection({})
|
||||
}
|
||||
|
||||
const comboboxProviders = useComboboxData({
|
||||
queryFn: (params) =>
|
||||
sdk.admin.payment.listPaymentProviders({ ...params, is_enabled: true }),
|
||||
queryKey: ["payment_providers"],
|
||||
getOptions: (data) =>
|
||||
data.payment_providers.map((pp) => ({
|
||||
label: formatProvider(pp.id),
|
||||
value: pp.id,
|
||||
})),
|
||||
})
|
||||
|
||||
return (
|
||||
<RouteFocusModal.Form form={form}>
|
||||
<KeyboundForm
|
||||
@@ -415,10 +424,9 @@ export const CreateRegionForm = ({
|
||||
</Form.Label>
|
||||
<Form.Control>
|
||||
<Combobox
|
||||
options={paymentProviders.map((pp) => ({
|
||||
label: formatProvider(pp.id),
|
||||
value: pp.id,
|
||||
}))}
|
||||
forceHideInput
|
||||
options={comboboxProviders.options}
|
||||
fetchNextPage={comboboxProviders.fetchNextPage}
|
||||
{...field}
|
||||
/>
|
||||
</Form.Control>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { RouteFocusModal } from "../../../components/modals/route-focus-modal"
|
||||
import { usePaymentProviders } from "../../../hooks/api/payments"
|
||||
import { useStore } from "../../../hooks/api/store"
|
||||
import { currencies } from "../../../lib/data/currencies"
|
||||
import { CreateRegionForm } from "./components/create-region-form"
|
||||
@@ -10,9 +9,6 @@ export const RegionCreate = () => {
|
||||
const storeCurrencies = (store?.supported_currencies ?? []).map(
|
||||
(c) => currencies[c.currency_code.toUpperCase()]
|
||||
)
|
||||
const { payment_providers: paymentProviders = [] } = usePaymentProviders({
|
||||
is_enabled: true,
|
||||
})
|
||||
|
||||
if (isError) {
|
||||
throw error
|
||||
@@ -20,12 +16,7 @@ export const RegionCreate = () => {
|
||||
|
||||
return (
|
||||
<RouteFocusModal>
|
||||
{!isLoading && store && (
|
||||
<CreateRegionForm
|
||||
currencies={storeCurrencies}
|
||||
paymentProviders={paymentProviders}
|
||||
/>
|
||||
)}
|
||||
{!isLoading && store && <CreateRegionForm currencies={storeCurrencies} />}
|
||||
</RouteFocusModal>
|
||||
)
|
||||
}
|
||||
|
||||
+21
-8
@@ -5,7 +5,7 @@ import { useTranslation } from "react-i18next"
|
||||
import * as zod from "zod"
|
||||
|
||||
import { Form } from "../../../../../components/common/form/index.ts"
|
||||
import { Combobox } from "../../../../../components/inputs/combobox/index.ts"
|
||||
import { Combobox } from "../../../../../components/inputs/combobox"
|
||||
import {
|
||||
RouteDrawer,
|
||||
useRouteModal,
|
||||
@@ -15,11 +15,12 @@ import { useUpdateRegion } from "../../../../../hooks/api/regions.tsx"
|
||||
import { CurrencyInfo } from "../../../../../lib/data/currencies.ts"
|
||||
import { formatProvider } from "../../../../../lib/format-provider.ts"
|
||||
import { useDocumentDirection } from "../../../../../hooks/use-document-direction"
|
||||
import { useComboboxData } from "../../../../../hooks/use-combobox-data.tsx"
|
||||
import { sdk } from "../../../../../lib/client/index.ts"
|
||||
|
||||
type EditRegionFormProps = {
|
||||
region: HttpTypes.AdminRegion
|
||||
currencies: CurrencyInfo[]
|
||||
paymentProviders: PaymentProviderDTO[]
|
||||
pricePreferences: HttpTypes.AdminPricePreference[]
|
||||
}
|
||||
|
||||
@@ -34,7 +35,6 @@ const EditRegionSchema = zod.object({
|
||||
export const EditRegionForm = ({
|
||||
region,
|
||||
currencies,
|
||||
paymentProviders,
|
||||
pricePreferences,
|
||||
}: EditRegionFormProps) => {
|
||||
const { t } = useTranslation()
|
||||
@@ -54,6 +54,17 @@ export const EditRegionForm = ({
|
||||
},
|
||||
})
|
||||
|
||||
const comboboxProviders = useComboboxData({
|
||||
queryKey: ["payment_providers"],
|
||||
queryFn: (params) =>
|
||||
sdk.admin.payment.listPaymentProviders({ ...params, is_enabled: true }),
|
||||
getOptions: (data) =>
|
||||
data.payment_providers.map((pp) => ({
|
||||
label: formatProvider(pp.id),
|
||||
value: pp.id,
|
||||
})),
|
||||
})
|
||||
|
||||
const { mutateAsync: updateRegion, isPending: isPendingRegion } =
|
||||
useUpdateRegion(region.id)
|
||||
|
||||
@@ -80,7 +91,10 @@ export const EditRegionForm = ({
|
||||
|
||||
return (
|
||||
<RouteDrawer.Form form={form}>
|
||||
<KeyboundForm onSubmit={handleSubmit} className="flex flex-1 flex-col overflow-hidden">
|
||||
<KeyboundForm
|
||||
onSubmit={handleSubmit}
|
||||
className="flex flex-1 flex-col overflow-hidden"
|
||||
>
|
||||
<RouteDrawer.Body className="overflow-y-auto">
|
||||
<div className="flex flex-col gap-y-8">
|
||||
<div className="flex flex-col gap-y-4">
|
||||
@@ -205,10 +219,9 @@ export const EditRegionForm = ({
|
||||
<Form.Label>{t("fields.paymentProviders")}</Form.Label>
|
||||
<Form.Control>
|
||||
<Combobox
|
||||
options={paymentProviders.map((pp) => ({
|
||||
label: formatProvider(pp.id),
|
||||
value: pp.id,
|
||||
}))}
|
||||
forceHideInput
|
||||
options={comboboxProviders.options}
|
||||
fetchNextPage={comboboxProviders.fetchNextPage}
|
||||
{...field}
|
||||
/>
|
||||
</Form.Control>
|
||||
|
||||
@@ -48,10 +48,6 @@ export const RegionEdit = () => {
|
||||
const storeCurrencies = (store?.supported_currencies ?? []).map(
|
||||
(c) => currencies[c.currency_code.toUpperCase()]
|
||||
)
|
||||
const { payment_providers: paymentProviders = [] } = usePaymentProviders({
|
||||
limit: 999,
|
||||
is_enabled: true,
|
||||
})
|
||||
|
||||
if (isRegionError) {
|
||||
throw regionError
|
||||
@@ -74,7 +70,6 @@ export const RegionEdit = () => {
|
||||
<EditRegionForm
|
||||
region={region}
|
||||
currencies={storeCurrencies}
|
||||
paymentProviders={paymentProviders}
|
||||
pricePreferences={pricePreferences}
|
||||
/>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user