-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17ac742
commit e510542
Showing
5 changed files
with
206 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ import { ShippingContextProps } from "Apps/Order/Routes/Shipping2/ShippingContex | |
import { useHandleUserAddressUpdates } from "Apps/Order/Routes/Shipping2/Hooks/useHandleUserAddressUpdates" | ||
import { useRouter } from "System/Router/useRouter" | ||
import { useOrderTracking } from "Apps/Order/Hooks/useOrderTracking" | ||
import { FormikHelpers } from "formik" | ||
|
||
const logger = createLogger("Routes/Shipping2/FulfillmentDetails.tsx") | ||
|
||
|
@@ -154,6 +155,118 @@ export const FulfillmentDetails: FC<FulfillmentDetailsProps> = ({ | |
setVerifyAddressNow(false) | ||
} | ||
|
||
const submitFulfillmentDetailsWithAddressVerificationAndUpdates = async ( | ||
values: FulfillmentValues, | ||
helpers: FormikHelpers<FulfillmentValues> | ||
) => { | ||
// Assume the form environment will handle validation. If the nested | ||
// SavedAddress formik context is handling values, the outer form validation | ||
// will not be triggered (Currently by passing an empty validation schema). | ||
// Alternative: Putting all form values in a single context? | ||
|
||
// Trigger address verification and return early if appropriate | ||
if (shouldVerifyAddressOnSubmit(values)) { | ||
setVerifyAddressNow(true) | ||
return | ||
} | ||
|
||
try { | ||
let fulfillmentMutationValues: CommerceSetShippingInput | ||
let requiresArtsyShippingToDestination: boolean | ||
|
||
shippingContext.actions.setIsPerformingOperation(true) | ||
|
||
if (values.fulfillmentType === FulfillmentType.PICKUP) { | ||
fulfillmentMutationValues = { | ||
id: orderData.internalID, | ||
fulfillmentType: FulfillmentType.PICKUP, | ||
phoneNumber: values.attributes.phoneNumber, | ||
shipping: { | ||
addressLine1: "", | ||
addressLine2: "", | ||
country: "", | ||
name: "", | ||
city: "", | ||
postalCode: "", | ||
region: "", | ||
phoneNumber: "", | ||
}, | ||
} | ||
} else { | ||
requiresArtsyShippingToDestination = shippingContext.orderData.requiresArtsyShippingTo( | ||
values.attributes.country | ||
) | ||
|
||
const { phoneNumber, ...addressValues } = values.attributes | ||
|
||
// TODO: Ponder, should we be using the same values for the fulfillment mutation | ||
// in all cases, or should we use the result of the user address action if it is | ||
// used? We are assuming the result of the user address action | ||
// will be consistent with the values in the form which seems fair. OTOH the | ||
// result there could include a saved address ID... Seems unumportant for today. | ||
|
||
fulfillmentMutationValues = { | ||
id: orderData.internalID, | ||
fulfillmentType: requiresArtsyShippingToDestination | ||
? "SHIP_ARTA" | ||
: FulfillmentType.SHIP, | ||
phoneNumber, | ||
shipping: { ...addressValues, phoneNumber: "" }, | ||
} | ||
|
||
if (values.meta.addressVerifiedBy) { | ||
fulfillmentMutationValues.addressVerifiedBy = | ||
values.meta.addressVerifiedBy | ||
} | ||
|
||
switch (values.meta.savedAddressAction?.action) { | ||
case "edit": { | ||
const userAddressID = values.meta.savedAddressAction.addressID | ||
// Do & await the user update address mutation | ||
break | ||
} | ||
case "create": { | ||
// Do & await the user create address mutation | ||
break | ||
} | ||
default: { | ||
// Do nothing, user address is not persisted | ||
// TODO: Where/does it need to be un-saved? Can we combine that logic with | ||
// the delete address mutation logic from SavedAddresses? | ||
} | ||
} | ||
} | ||
const result = await saveFulfillmentDetails.submitMutation({ | ||
variables: { input: fulfillmentMutationValues }, | ||
}) | ||
|
||
const orderOrError = result.commerceSetShipping?.orderOrError | ||
|
||
if (orderOrError?.__typename === "CommerceOrderWithMutationFailure") { | ||
shippingContext.actions.setIsPerformingOperation(false) | ||
|
||
shippingContext.actions.handleExchangeError(orderOrError.error, logger) | ||
return | ||
} | ||
|
||
handleFulfillmentDetailsSaved({ | ||
requiresArtsyShipping: requiresArtsyShippingToDestination, | ||
}) | ||
} catch (error) { | ||
orderTracking.errorMessageViewed({ | ||
error_code: null, | ||
title: "An error occurred", | ||
message: | ||
"Something went wrong. Please try again or contact [email protected].", | ||
flow: "user selects a shipping option", | ||
}) | ||
|
||
shippingContext.actions.dialog.showErrorDialog() | ||
} finally { | ||
shippingContext.actions.setIsPerformingOperation(false) | ||
} | ||
} | ||
|
||
const submitFulfillmentDetails = async ({ | ||
performUserAddressUpdates, | ||
formValues, | ||
|
@@ -245,17 +358,17 @@ export const FulfillmentDetails: FC<FulfillmentDetailsProps> = ({ | |
} | ||
} | ||
|
||
const handleSubmit = values => { | ||
if (shouldVerifyAddressOnSubmit(values)) { | ||
setVerifyAddressNow(true) | ||
return | ||
} else { | ||
return submitFulfillmentDetails({ | ||
performUserAddressUpdates: shippingMode === "new_address", | ||
formValues: values, | ||
}) | ||
} | ||
} | ||
// const handleSubmit = values => { | ||
// if (shouldVerifyAddressOnSubmit(values)) { | ||
// setVerifyAddressNow(true) | ||
// return | ||
// } else { | ||
// return submitFulfillmentDetails({ | ||
// performUserAddressUpdates: shippingMode === "new_address", | ||
// formValues: values, | ||
// }) | ||
// } | ||
// } | ||
|
||
return ( | ||
<FulfillmentDetailsForm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters