Skip to content
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

[Hooks integration] Checkout Payment Section + Checkout Confirmation Page #1075

Merged
merged 45 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
8178130
render payment form
kevinxh Mar 16, 2023
e72e43e
refactor payment form
kevinxh Mar 18, 2023
d266c0b
remove console log
kevinxh Mar 21, 2023
f03735c
guest payment working
kevinxh Mar 21, 2023
858903a
fix bug
kevinxh Mar 21, 2023
a61cd1e
create order
kevinxh Mar 21, 2023
92668e0
work on order confirmation
kevinxh Mar 21, 2023
108da50
fix lint
kevinxh Mar 21, 2023
6723c47
simplify checkout context
kevinxh Mar 21, 2023
11af7c9
simplify checkout context 2
kevinxh Mar 21, 2023
cfaf287
fix confirmation register form
kevinxh Mar 21, 2023
e205e18
finish checkout context refactor
kevinxh Mar 21, 2023
23a7976
Merge branch 'feature/integrate-commerce-sdk-react' into checkout-pay…
kevinxh Mar 21, 2023
f3d6fe0
minor fixes
kevinxh Mar 21, 2023
fe6bc8a
fix bug
kevinxh Mar 21, 2023
2033968
fix test
kevinxh Mar 22, 2023
47ddb56
lint
kevinxh Mar 22, 2023
e3ad4db
commit translations
kevinxh Mar 22, 2023
97b494e
use issueNumber
kevinxh Mar 22, 2023
74f44eb
Merge branch 'feature/integrate-commerce-sdk-react' into checkout-pay…
kevinxh Mar 22, 2023
5bf30f0
minor fixes
kevinxh Mar 22, 2023
98bdd53
fix bug
kevinxh Mar 22, 2023
edafe63
confirmation fetch product details
kevinxh Mar 22, 2023
8fd4c65
remove console logs
kevinxh Mar 22, 2023
b6aee0b
bug fix
kevinxh Mar 22, 2023
a5af363
fix undefined basket
kevinxh Mar 22, 2023
81df103
use currency hook in checkout options
kevinxh Mar 22, 2023
208585a
lint
kevinxh Mar 22, 2023
353d014
Merge branch 'feature/integrate-commerce-sdk-react' into checkout-pay…
kevinxh Mar 22, 2023
358eda9
fix currency issues
kevinxh Mar 23, 2023
df0fa11
Merge branch 'checkout-payments' of https://github.com/SalesforceComm…
kevinxh Mar 23, 2023
c0a1644
guard undefined
kevinxh Mar 23, 2023
8c23d48
lint
kevinxh Mar 23, 2023
49c39fb
Update packages/template-retail-react-app/app/pages/checkout/partials…
kevinxh Mar 23, 2023
e31cf79
fix useEffect deps
kevinxh Mar 23, 2023
44a8f94
Merge branch 'checkout-payments' of https://github.com/SalesforceComm…
kevinxh Mar 23, 2023
fb31c24
fix undefined prices
kevinxh Mar 23, 2023
5fc4636
fix undefined currency
kevinxh Mar 23, 2023
ead8ccb
remove guard
kevinxh Mar 23, 2023
595c286
pr feedback
kevinxh Mar 23, 2023
b8af41c
Update packages/template-retail-react-app/app/pages/checkout/partials…
kevinxh Mar 23, 2023
cb6a6b8
guard error type
kevinxh Mar 23, 2023
e889214
Merge branch 'checkout-payments' of https://github.com/SalesforceComm…
kevinxh Mar 23, 2023
1be9e1e
revert cart changes
kevinxh Mar 23, 2023
39ef577
Merge branch 'feature/integrate-commerce-sdk-react' into checkout-pay…
kevinxh Mar 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const ItemAttributes = ({includeQuantity, currency, ...props}) => {
<Text as="span" color="green.500">
<FormattedNumber
style="currency"
currency={currency || basket.currency || activeCurrency}
currency={currency || basket?.currency || activeCurrency}
value={variant.priceAdjustments[0].price}
/>
</Text>
Expand Down
kevinxh marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -483,23 +483,19 @@ const useEinstein = () => {
return einstein.getRecommenders(...args)
},
async getRecommendations(recommenderName, products, args) {
console.log('getRecommendations')
setIsLoading(true)
const reco = await einstein.getRecommendations(recommenderName, products, args)
reco.recommenderName = recommenderName
const recommendations = await fetchRecProductDetails(reco)
setIsLoading(false)
setRecommendations(recommendations)
console.log(recommendations)
},
async getZoneRecommendations(zoneName, products, args) {
console.log('getZoneRecommendations')
setIsLoading(true)
const reco = await einstein.getZoneRecommendations(zoneName, products, args)
const recommendations = await fetchRecProductDetails(reco)
setIsLoading(false)
setRecommendations(recommendations)
console.log(recommendations)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/template-retail-react-app/app/pages/cart/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ const Cart = () => {
return <CartSkeleton />
}

if (!isLoading && !basket?.productItems?.length) {
if (!basket?.productItems?.length) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we check loading here, we won't need to guard basket below

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loading is always false here since it is checked on line 310

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is false, it indicates that basketData should be defined, we technically don't need to guard the basket here. Guarding is a good way to make sure things don't break, but if we know we have guarded other cases, we should not throw in the ? too much. 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, removed the guard

Copy link
Collaborator

@adamraya adamraya Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexvuong @kevinxh FYI it looks the change broke the cart page when the cart is empty.
http://localhost:3000/global/en-GB/cart

TypeError: Cannot read properties of undefined (reading 'length')
    at Cart (http://localhost:3000/mobify/bundle/development/pages-cart.js:394:28)

This is a regression that was fixed in this PR: #1062

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok... i'm just gonna revert the change in cart.js.... it's out of scope for this PR and i thought it's a small diff improvement. but turns out to be too much hassle

return <EmptyCart isRegistered={isRegistered} />
}
return (
Expand All @@ -332,7 +332,7 @@ const Cart = () => {
>
<GridItem>
<Stack spacing={4}>
{basket.productItems?.map((productItem, idx) => {
{basket?.productItems?.map((productItem, idx) => {
return (
<ProductItem
key={productItem.productId}
Expand Down
kevinxh marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React, {useEffect, useState, Fragment} from 'react'
import React, {Fragment, useEffect} from 'react'
import {FormattedMessage, FormattedNumber} from 'react-intl'
import {
Box,
Expand All @@ -21,9 +21,9 @@ import {
Divider
} from '@chakra-ui/react'
import {useForm} from 'react-hook-form'
import {useParams} from 'react-router-dom'
import {useOrder, useProducts, useAuthHelper, AuthHelpers} from 'commerce-sdk-react-preview'
import {getCreditCardIcon} from '../../utils/cc-utils'
import useBasket from '../../commerce-api/hooks/useBasket'
import useCustomer from '../../commerce-api/hooks/useCustomer'
import useNavigation from '../../hooks/use-navigation'
import Link from '../../components/link'
import AddressDisplay from '../../components/address-display'
Expand All @@ -34,32 +34,35 @@ import CartItemVariantImage from '../../components/item-variant/item-image'
import CartItemVariantName from '../../components/item-variant/item-name'
import CartItemVariantAttributes from '../../components/item-variant/item-attributes'
import CartItemVariantPrice from '../../components/item-variant/item-price'
import {useCurrentCustomer} from '../../hooks/use-current-customer'

const onClient = typeof window !== 'undefined'

const CheckoutConfirmation = () => {
kevinxh marked this conversation as resolved.
Show resolved Hide resolved
const {orderNo} = useParams()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's cool now the confirmation page is appending the order id to the url.

const navigate = useNavigation()
const basket = useBasket()
const customer = useCustomer()

// The order data will initially be stored as our basket when first coming to this
// page. We capture it in local state to use for our UI. A new basket will be automatically
// created so we should only reference our captured order data here.
const [order] = useState(basket)

const form = useForm({
defaultValues: {
email: customer?.email || order?.customerInfo?.email || '',
password: '',
firstName: customer.firstName || order?.billingAddress?.firstName,
lastName: customer.lastName || order?.billingAddress?.lastName
const {data: customer} = useCurrentCustomer()
const register = useAuthHelper(AuthHelpers.Register)
const {data: order} = useOrder(
{
parameters: {orderNo}
},
{
enabled: !!orderNo && onClient
}
})
)
const itemIds = order?.productItems.map((item) => item.productId)
const {data: products} = useProducts({parameters: {ids: itemIds?.join(',')}})
kevinxh marked this conversation as resolved.
Show resolved Hide resolved
const productItemsMap = products?.data.reduce((map, item) => ({...map, [item.id]: item}), {})
const form = useForm()

// If we don't have an order object on first render we need to transition back to a
// different page. Fow now, we push to the homepage.
useEffect(() => {
if (!order || order._type !== 'order') {
navigate('/')
}
form.reset({
email: order?.customerInfo?.email || '',
password: '',
firstName: order?.billingAddress?.firstName,
lastName: order?.billingAddress?.lastName
})
}, [order])

if (!order || !order.orderNo) {
Expand All @@ -70,8 +73,21 @@ const CheckoutConfirmation = () => {

const submitForm = async (data) => {
try {
await customer.registerCustomer(data)
const body = {
customer: {
firstName: data.firstName,
lastName: data.lastName,
email: data.email,
login: data.email
},
password: data.password
}
await register.mutateAsync(body)

navigate(`/account`)
} catch (error) {
const json = await error.response.json()
kevinxh marked this conversation as resolved.
Show resolved Hide resolved

const existingAccountMessage = (
<Fragment>
<FormattedMessage
Expand All @@ -88,18 +104,12 @@ const CheckoutConfirmation = () => {
</Fragment>
)

const message = /the login is already in use/i.test(error.message)
const message = /the login is already in use/i.test(json.detail)
? existingAccountMessage
: error.message
: json.detail
kevinxh marked this conversation as resolved.
Show resolved Hide resolved

form.setError('global', {type: 'manual', message})
return
}

// Customer is successfully registered with a new account,
// and the recent order would be associated with this account too.
// Now redirect to the Account page.
navigate(`/account`)
}

return (
Expand Down Expand Up @@ -277,14 +287,14 @@ const CheckoutConfirmation = () => {
divider={<Divider />}
>
{order.productItems?.map((product, idx) => {
const productDetail =
productItemsMap?.[product.productId] || {}
const variant = {
...product,
...(order._productItemsDetail &&
order._productItemsDetail[
product.productId
]),
...productDetail,
price: product.price
}

return (
<ItemVariantProvider
key={product.productId}
Expand Down
Loading