fix(admin): draft order shipping details (#3500)
* wip: fix draft order shipping details screen * fix: reset address, refactor and cleanup * fix: use const * Create .changeset/violet-sloths-train.md * fix: required fields, reset province --------- Co-authored-by: fPolic <frane@medusajs.com> Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
3 files changed
+48
-29
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/admin-ui": patch
|
||||||
|
"@medusajs/admin": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(admin-ui): draft order shipping details
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
import qs from "query-string"
|
import qs from "query-string"
|
||||||
import { useContext, useEffect, useMemo, useState } from "react"
|
import { useContext, useEffect, useMemo, useState } from "react"
|
||||||
import Spinner from "../../../../components/atoms/spinner"
|
import { Controller, useWatch } from "react-hook-form"
|
||||||
|
|
||||||
|
import { useAdminCustomer } from "medusa-react"
|
||||||
|
|
||||||
import Button from "../../../../components/fundamentals/button"
|
import Button from "../../../../components/fundamentals/button"
|
||||||
import AddressForm, {
|
import AddressForm, {
|
||||||
AddressType,
|
AddressType,
|
||||||
} from "../../../../components/templates/address-form"
|
} from "../../../../components/templates/address-form"
|
||||||
import Medusa from "../../../../services/api"
|
import Medusa from "../../../../services/api"
|
||||||
|
|
||||||
import { useAdminCustomer, useAdminCustomers } from "medusa-react"
|
|
||||||
import { Controller, useWatch } from "react-hook-form"
|
|
||||||
import LockIcon from "../../../../components/fundamentals/icons/lock-icon"
|
import LockIcon from "../../../../components/fundamentals/icons/lock-icon"
|
||||||
import InputField from "../../../../components/molecules/input"
|
import InputField from "../../../../components/molecules/input"
|
||||||
import { SteppedContext } from "../../../../components/molecules/modal/stepped-modal"
|
import { SteppedContext } from "../../../../components/molecules/modal/stepped-modal"
|
||||||
@@ -18,14 +18,13 @@ import { Option } from "../../../../types/shared"
|
|||||||
import isNullishObject from "../../../../utils/is-nullish-object"
|
import isNullishObject from "../../../../utils/is-nullish-object"
|
||||||
import mapAddressToForm from "../../../../utils/map-address-to-form"
|
import mapAddressToForm from "../../../../utils/map-address-to-form"
|
||||||
import { nestedForm } from "../../../../utils/nested-form"
|
import { nestedForm } from "../../../../utils/nested-form"
|
||||||
|
import { isValidEmail } from "../../../../utils/email"
|
||||||
import { useNewOrderForm } from "../form"
|
import { useNewOrderForm } from "../form"
|
||||||
|
|
||||||
const ShippingDetails = () => {
|
const ShippingDetails = () => {
|
||||||
const [addNew, setAddNew] = useState(false)
|
const [addNew, setAddNew] = useState(false)
|
||||||
const { disableNextPage, enableNextPage } = useContext(SteppedContext)
|
const { disableNextPage, enableNextPage } = useContext(SteppedContext)
|
||||||
|
|
||||||
const { customers } = useAdminCustomers()
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
context: { validCountries },
|
context: { validCountries },
|
||||||
form,
|
form,
|
||||||
@@ -57,7 +56,7 @@ const ShippingDetails = () => {
|
|||||||
name: "customer_id",
|
name: "customer_id",
|
||||||
})
|
})
|
||||||
|
|
||||||
const { customer, isLoading } = useAdminCustomer(customerId?.value!, {
|
const { customer } = useAdminCustomer(customerId?.value!, {
|
||||||
enabled: !!customerId?.value,
|
enabled: !!customerId?.value,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -72,13 +71,15 @@ const ShippingDetails = () => {
|
|||||||
({ country_code }) =>
|
({ country_code }) =>
|
||||||
!country_code || validCountryCodes.includes(country_code)
|
!country_code || validCountryCodes.includes(country_code)
|
||||||
)
|
)
|
||||||
}, [customer])
|
}, [customer, validCountries])
|
||||||
|
|
||||||
const onCustomerSelect = (val: Option) => {
|
const onCustomerSelect = (val: Option) => {
|
||||||
const email = /\(([^()]*)\)$/.exec(val?.label)
|
const email = /\(([^()]*)\)$/.exec(val?.label)
|
||||||
|
|
||||||
if (email) {
|
if (email) {
|
||||||
form.setValue("email", email[1])
|
form.setValue("email", email[1])
|
||||||
|
} else {
|
||||||
|
form.setValue("email", "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,23 +105,17 @@ const ShippingDetails = () => {
|
|||||||
name: "email",
|
name: "email",
|
||||||
})
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!email) {
|
|
||||||
disableNextPage()
|
|
||||||
} else {
|
|
||||||
enableNextPage()
|
|
||||||
}
|
|
||||||
}, [email])
|
|
||||||
|
|
||||||
const shippingAddress = useWatch({
|
const shippingAddress = useWatch({
|
||||||
control: form.control,
|
control: form.control,
|
||||||
name: "shipping_address",
|
name: "shipping_address",
|
||||||
})
|
})
|
||||||
|
|
||||||
const [requiredFields, setRequiredFields] = useState(false)
|
/**
|
||||||
|
* Effect used to enable next step.
|
||||||
|
* A user can go to the next step if valid email is provided and all required address info is filled.
|
||||||
|
*/
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!email) {
|
if (!email || !isValidEmail(email)) {
|
||||||
disableNextPage()
|
disableNextPage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -135,16 +130,30 @@ const ShippingDetails = () => {
|
|||||||
!shippingAddress.postal_code
|
!shippingAddress.postal_code
|
||||||
) {
|
) {
|
||||||
disableNextPage()
|
disableNextPage()
|
||||||
setRequiredFields(true)
|
|
||||||
} else {
|
} else {
|
||||||
enableNextPage()
|
enableNextPage()
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
enableNextPage()
|
|
||||||
setRequiredFields(false)
|
|
||||||
}
|
}
|
||||||
}, [shippingAddress, email])
|
}, [shippingAddress, email])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// reset shipping address info when a different customer is selected
|
||||||
|
// or when "Create new" is clicked
|
||||||
|
form.setValue("shipping_address.first_name", "")
|
||||||
|
form.setValue("shipping_address.last_name", "")
|
||||||
|
form.setValue("shipping_address.phone", "")
|
||||||
|
form.setValue("shipping_address.address_1", "")
|
||||||
|
form.setValue("shipping_address.address_2", "")
|
||||||
|
form.setValue("shipping_address.city", "")
|
||||||
|
form.setValue("shipping_address.country_code", null)
|
||||||
|
form.setValue("shipping_address.province", "")
|
||||||
|
form.setValue("shipping_address.postal_code", "")
|
||||||
|
}, [customerId?.value, addNew])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setAddNew(false)
|
||||||
|
}, [customerId?.value])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-[705px] flex-col gap-y-8">
|
<div className="flex min-h-[705px] flex-col gap-y-8">
|
||||||
<div>
|
<div>
|
||||||
@@ -192,11 +201,7 @@ const ShippingDetails = () => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{isLoading ? (
|
{validAddresses.length && !addNew ? (
|
||||||
<div>
|
|
||||||
<Spinner variant="primary" />
|
|
||||||
</div>
|
|
||||||
) : validAddresses.length && !addNew ? (
|
|
||||||
<div>
|
<div>
|
||||||
<span className="inter-base-semibold">Choose existing addresses</span>
|
<span className="inter-base-semibold">Choose existing addresses</span>
|
||||||
<Controller
|
<Controller
|
||||||
@@ -244,7 +249,6 @@ const ShippingDetails = () => {
|
|||||||
form={nestedForm(form, "shipping_address")}
|
form={nestedForm(form, "shipping_address")}
|
||||||
countryOptions={validCountries}
|
countryOptions={validCountries}
|
||||||
type={AddressType.SHIPPING}
|
type={AddressType.SHIPPING}
|
||||||
required={requiredFields}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
const EMAIL_REGEX =
|
||||||
|
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate if provided string could be an email
|
||||||
|
*/
|
||||||
|
export function isValidEmail(email: string) {
|
||||||
|
return email?.match(EMAIL_REGEX)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user