import { AdminPostRegionsReq } from "@medusajs/medusa" import { useAdminCreateRegion } from "medusa-react" import { useState } from "react" import { useForm } from "react-hook-form" import { useNavigate } from "react-router-dom" import { useTranslation } from "react-i18next" import Button from "../../../../components/fundamentals/button" import CrossIcon from "../../../../components/fundamentals/icons/cross-icon" import FocusModal from "../../../../components/molecules/modal/focus-modal" import Accordion from "../../../../components/organisms/accordion" import useNotification from "../../../../hooks/use-notification" import { useFeatureFlag } from "../../../../providers/feature-flag-provider" import { getErrorMessage } from "../../../../utils/error-messages" import { nestedForm } from "../../../../utils/nested-form" import RegionDetailsForm, { RegionDetailsFormType, } from "../components/region-form/region-details-form" import RegionProvidersForm, { RegionProvidersFormType, } from "../components/region-form/region-providers-form" type Props = { onClose: () => void } type NewRegionFormType = { details: RegionDetailsFormType providers: RegionProvidersFormType } const NewRegion = ({ onClose }: Props) => { const { t } = useTranslation() const [sections, setSections] = useState(["details"]) const form = useForm({ defaultValues: getDefaultValues(), }) const { formState: { isDirty }, handleSubmit, reset, } = form const { mutate, isLoading } = useAdminCreateRegion() const navigate = useNavigate() const notification = useNotification() const { isFeatureEnabled } = useFeatureFlag() const closeAndReset = () => { reset(getDefaultValues()) onClose() } const onSubmit = handleSubmit( (data) => { const payload: AdminPostRegionsReq = { name: data.details.name!, countries: data.details.countries.map((c) => c.value), currency_code: data.details.currency_code?.value, fulfillment_providers: data.providers.fulfillment_providers.map( (fp) => fp.value ), payment_providers: data.providers.payment_providers.map( (pp) => pp.value ), tax_rate: data.details.tax_rate!, tax_code: data.details.tax_code || undefined, } if (isFeatureEnabled("tax_inclusive_pricing")) { payload.includes_tax = data.details.includes_tax } mutate(payload, { onSuccess: ({ region }) => { notification( t("new-success", "Success"), t("new-region-created", "Region created"), "success" ) navigate(`/a/settings/regions/${region.id}`) closeAndReset() }, onError: (error) => { notification(t("new-error", "Error"), getErrorMessage(error), "error") }, }) }, (errors) => { if (errors.providers && !sections.includes("providers")) { setSections((oldSections) => [...oldSections, "providers"]) } } ) return (

{t("new-add-the-region-details", "Add the region details.")}

{t( "new-add-which-fulfillment-and-payment-providers-should-be-available-in-this-region", "Add which fulfillment and payment providers should be available in this region." )}

) } export default NewRegion const getDefaultValues = () => { return { details: { countries: [], }, providers: { payment_providers: undefined, fulfillment_providers: undefined, }, } }