New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Checkout These Talons! #1775
Merged
Merged
Checkout These Talons! #1775
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d634cfb
Remove unnecessary cart component from checkout. Remove unnecessary p…
sirugh 763d3e0
Export functionality to talons and use within checkout components
sirugh eb1047c
trim the talons
sirugh f516683
Oops forgot some edits
sirugh 6f509cf
Move anchorRef back to component and install informed peer dependency
sirugh 43ff1f7
Merge branch 'develop' into rugh/checkout-these-talons
sirugh 67d35f3
Merge branch 'develop' into rugh/checkout-these-talons
sirugh dabcf21
Wrap callbacks in talons before returning to component
sirugh ef6fd8d
Update yarn lock because of informed peer dep I guess
sirugh f5da336
Merge branch 'develop' into rugh/checkout-these-talons
jimbo ade1ba7
Merge branch 'develop' into rugh/checkout-these-talons
dpatil-magento File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/peregrine/lib/talons/Checkout/Receipt/useReceipt.js
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
import { useCheckoutContext } from '@magento/peregrine/lib/context/checkout'; | ||
import { useUserContext } from '@magento/peregrine/lib/context/user'; | ||
import { useAppContext } from '@magento/peregrine/lib/context/app'; | ||
|
||
export const useReceipt = props => { | ||
// TODO replace with useHistory from Router 5.1 | ||
const { history, onClose } = props; | ||
|
||
const [{ drawer }] = useAppContext(); | ||
const [, { createAccount, resetReceipt }] = useCheckoutContext(); | ||
const [{ isSignedIn }] = useUserContext(); | ||
|
||
// When the drawer is closed reset the state of the receipt. We use a ref | ||
// because drawer can change if the mask is clicked. Mask updates drawer. | ||
const prevDrawer = useRef(null); | ||
useEffect(() => { | ||
if (prevDrawer.current === 'cart' && drawer !== 'cart') { | ||
resetReceipt(); | ||
onClose(); | ||
} | ||
prevDrawer.current = drawer; | ||
}, [drawer, onClose, resetReceipt]); | ||
|
||
const handleCreateAccount = useCallback(() => { | ||
createAccount(history); | ||
}, [createAccount, history]); | ||
|
||
const handleViewOrderDetails = useCallback(() => { | ||
// TODO: Implement/connect/redirect to order details page. | ||
}, []); | ||
|
||
return { | ||
handleCreateAccount, | ||
handleViewOrderDetails, | ||
isSignedIn | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useMemo } from 'react'; | ||
|
||
/** | ||
* Returns values used to render an AddressForm component. | ||
* @param {Object} props | ||
* @param {Object[]} props.fields an array of fields to reduce over for initial values | ||
* @param {Object} props.initialValues Object containing some initial values from state | ||
* @returns {Object} initialValues a map of form fields and corresponding initial values. | ||
*/ | ||
export const useAddressForm = props => { | ||
const { fields, initialValues } = props; | ||
|
||
const values = useMemo( | ||
() => | ||
fields.reduce((acc, key) => { | ||
acc[key] = initialValues[key]; | ||
return acc; | ||
}, {}), | ||
[fields, initialValues] | ||
); | ||
|
||
return { | ||
initialValues: values | ||
}; | ||
sirugh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useCallback } from 'react'; | ||
|
||
export const useEditableForm = props => { | ||
const { | ||
editing, | ||
isSubmitting, | ||
setEditing, | ||
shippingAddressError, | ||
submitPaymentMethodAndBillingAddress, | ||
submitShippingAddress, | ||
submitShippingMethod, | ||
checkout: { countries } | ||
} = props; | ||
|
||
const handleCancel = useCallback(() => { | ||
setEditing(null); | ||
}, [setEditing]); | ||
|
||
const handleSubmitAddressForm = useCallback( | ||
async formValues => { | ||
await submitShippingAddress({ | ||
formValues | ||
}); | ||
setEditing(null); | ||
}, | ||
[setEditing, submitShippingAddress] | ||
); | ||
|
||
const handleSubmitPaymentsForm = useCallback( | ||
async formValues => { | ||
await submitPaymentMethodAndBillingAddress({ | ||
formValues | ||
}); | ||
setEditing(null); | ||
}, | ||
[setEditing, submitPaymentMethodAndBillingAddress] | ||
); | ||
|
||
const handleSubmitShippingForm = useCallback( | ||
async formValues => { | ||
await submitShippingMethod({ | ||
formValues | ||
}); | ||
setEditing(null); | ||
}, | ||
[setEditing, submitShippingMethod] | ||
); | ||
|
||
return { | ||
countries, | ||
editing, | ||
handleCancel, | ||
handleSubmitAddressForm, | ||
handleSubmitPaymentsForm, | ||
handleSubmitShippingForm, | ||
isSubmitting, | ||
shippingAddressError | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { useCallback } from 'react'; | ||
import { useCartContext } from '@magento/peregrine/lib/context/cart'; | ||
import { useCheckoutContext } from '@magento/peregrine/lib/context/checkout'; | ||
import isObjectEmpty from '../../util/isObjectEmpty'; | ||
|
||
const isCheckoutReady = checkout => { | ||
const { | ||
billingAddress, | ||
paymentData, | ||
shippingAddress, | ||
shippingMethod | ||
} = checkout; | ||
|
||
const objectsHaveData = [ | ||
billingAddress, | ||
paymentData, | ||
shippingAddress | ||
].every(data => { | ||
return !!data && !isObjectEmpty(data); | ||
}); | ||
|
||
const stringsHaveData = !!shippingMethod && shippingMethod.length > 0; | ||
|
||
return objectsHaveData && stringsHaveData; | ||
}; | ||
|
||
export const useFlow = props => { | ||
const [cartState] = useCartContext(); | ||
const [ | ||
checkoutState, | ||
{ | ||
beginCheckout, | ||
cancelCheckout, | ||
submitOrder, | ||
submitPaymentMethodAndBillingAddress, | ||
submitShippingAddress, | ||
submitShippingMethod | ||
} | ||
] = useCheckoutContext(); | ||
const { onSubmitError, step, setStep } = props; | ||
|
||
const handleBeginCheckout = useCallback(async () => { | ||
await beginCheckout(); | ||
setStep('form'); | ||
}, [beginCheckout, setStep]); | ||
|
||
const handleCancelCheckout = useCallback(async () => { | ||
await cancelCheckout(); | ||
setStep('cart'); | ||
}, [cancelCheckout, setStep]); | ||
|
||
const handleSubmitOrder = useCallback(async () => { | ||
try { | ||
await submitOrder(); | ||
setStep('receipt'); | ||
} catch (e) { | ||
onSubmitError(e); | ||
} | ||
}, [onSubmitError, setStep, submitOrder]); | ||
|
||
const handleCloseReceipt = useCallback(() => { | ||
setStep('cart'); | ||
}, [setStep]); | ||
|
||
return { | ||
cartState, | ||
checkoutDisabled: checkoutState.isSubmitting || cartState.isEmpty, | ||
checkoutState, | ||
isReady: isCheckoutReady(checkoutState), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect. 👍 |
||
submitPaymentMethodAndBillingAddress, | ||
submitShippingAddress, | ||
submitShippingMethod, | ||
handleBeginCheckout, | ||
handleCancelCheckout, | ||
handleCloseReceipt, | ||
handleSubmitOrder, | ||
step | ||
}; | ||
sirugh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useCallback } from 'react'; | ||
|
||
/** | ||
* Returns props to render an Overview component. | ||
* | ||
* @param {Object} props.cart cart state object | ||
* @param {boolean} props.isSubmitting is the form already submitting | ||
* @param {boolean} props.ready is the form ready to submit | ||
* @param {function} props.setEditing set editing state object | ||
*/ | ||
export const useOverview = props => { | ||
const { cart, isSubmitting, ready, setEditing } = props; | ||
|
||
const handleAddressFormClick = useCallback(() => { | ||
setEditing('address'); | ||
}, [setEditing]); | ||
|
||
const handlePaymentFormClick = useCallback(() => { | ||
setEditing('paymentMethod'); | ||
}, [setEditing]); | ||
|
||
const handleShippingFormClick = useCallback(() => { | ||
setEditing('shippingMethod'); | ||
}, [setEditing]); | ||
|
||
const currencyCode = | ||
(cart && cart.totals && cart.totals.quote_currency_code) || 'USD'; | ||
const numItems = (cart && cart.details && cart.details.items_qty) || 0; | ||
const subtotal = (cart && cart.totals && cart.totals.subtotal) || 0; | ||
|
||
return { | ||
currencyCode, | ||
handleAddressFormClick, | ||
handlePaymentFormClick, | ||
handleShippingFormClick, | ||
isSubmitDisabled: isSubmitting || !ready, | ||
numItems, | ||
subtotal | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useCallback, useState } from 'react'; | ||
import isObjectEmpty from '../../util/isObjectEmpty'; | ||
|
||
const DEFAULT_FORM_VALUES = { | ||
addresses_same: true | ||
}; | ||
|
||
/** | ||
* Returns props necessary to render a PaymentsForm component. | ||
* | ||
* @param {Object} props.initialValues initial values from state | ||
*/ | ||
export const usePaymentsForm = props => { | ||
const { initialValues } = props; | ||
const [isSubmitting, setIsSubmitting] = useState(false); | ||
|
||
const handleSubmit = useCallback(() => { | ||
setIsSubmitting(true); | ||
}, [setIsSubmitting]); | ||
|
||
let initialFormValues; | ||
if (isObjectEmpty(initialValues)) { | ||
initialFormValues = DEFAULT_FORM_VALUES; | ||
} else { | ||
if (initialValues.sameAsShippingAddress) { | ||
// If the addresses are the same, don't populate any fields | ||
// other than the checkbox with an initial value. | ||
initialFormValues = { | ||
addresses_same: true | ||
}; | ||
} else { | ||
// The addresses are not the same, populate the other fields. | ||
initialFormValues = { | ||
addresses_same: false, | ||
...initialValues | ||
}; | ||
delete initialFormValues.sameAsShippingAddress; | ||
} | ||
} | ||
|
||
return { | ||
handleSubmit, | ||
initialValues: initialFormValues, | ||
isSubmitting, | ||
setIsSubmitting | ||
}; | ||
}; |
88 changes: 88 additions & 0 deletions
88
packages/peregrine/lib/talons/Checkout/usePaymentsFormItems.js
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
// TODO install informed? | ||
sirugh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { useFormState } from 'informed'; | ||
|
||
/** | ||
* | ||
* @param {boolean} props.isSubmitting whether or not the payment form items are | ||
* @param {function} props.setIsSubmitting callback for setting submitting state | ||
* @param {function} props.onSubmit submit callback | ||
*/ | ||
export const usePaymentsFormItems = props => { | ||
const [isReady, setIsReady] = useState(false); | ||
|
||
const { isSubmitting, setIsSubmitting, onSubmit } = props; | ||
|
||
// Currently form state toggles dirty from false to true because of how | ||
// informed is implemented. This effectively causes this child components | ||
// to re-render multiple times. Keep tabs on the following issue: | ||
// https://github.com/joepuzzo/informed/issues/138 | ||
// If they resolve it or we move away from informed we can probably get some | ||
// extra performance. | ||
const formState = useFormState(); | ||
const anchorRef = useRef(null); | ||
const addressDiffers = formState.values.addresses_same === false; | ||
|
||
const handleError = useCallback(() => { | ||
setIsSubmitting(false); | ||
}, [setIsSubmitting]); | ||
|
||
// The success callback. Unfortunately since form state is created first and | ||
// then modified when using initialValues any component who uses this | ||
// callback will be rendered multiple times on first render. See above | ||
// comments for more info. | ||
const handleSuccess = useCallback( | ||
value => { | ||
setIsSubmitting(false); | ||
const sameAsShippingAddress = formState.values['addresses_same']; | ||
let billingAddress; | ||
if (!sameAsShippingAddress) { | ||
billingAddress = { | ||
city: formState.values['city'], | ||
email: formState.values['email'], | ||
firstname: formState.values['firstname'], | ||
lastname: formState.values['lastname'], | ||
postcode: formState.values['postcode'], | ||
region_code: formState.values['region_code'], | ||
street: formState.values['street'], | ||
telephone: formState.values['telephone'] | ||
}; | ||
} else { | ||
billingAddress = { | ||
sameAsShippingAddress | ||
}; | ||
} | ||
onSubmit({ | ||
billingAddress, | ||
paymentMethod: { | ||
code: 'braintree', | ||
data: value | ||
} | ||
}); | ||
}, | ||
[formState.values, setIsSubmitting, onSubmit] | ||
); | ||
|
||
// When the address checkbox is unchecked, additional fields are rendered. | ||
// This causes the form to grow, and potentially to overflow, so the new | ||
// fields may go unnoticed. To reveal them, we scroll them into view. | ||
useEffect(() => { | ||
if (addressDiffers) { | ||
const { current: element } = anchorRef; | ||
|
||
if (element instanceof HTMLElement) { | ||
element.scrollIntoView({ behavior: 'smooth' }); | ||
} | ||
} | ||
}, [addressDiffers]); | ||
sirugh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { | ||
addressDiffers, | ||
anchorRef, | ||
handleError, | ||
handleSuccess, | ||
isDisabled: !isReady || isSubmitting, | ||
isSubmitting, | ||
setIsReady | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄