Skip to content

Commit

Permalink
fix: Lose order data when reload the thank you page
Browse files Browse the repository at this point in the history
  • Loading branch information
acasazza committed May 13, 2022
1 parent b310f6f commit c7b5935
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
41 changes: 29 additions & 12 deletions src/components/OrderContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import OrderStorageContext from '#context/OrderStorageContext'
import { OrderCreate, Order } from '@commercelayer/sdk'
import { BaseError } from '#typings/errors'
import compareObjAttribute from '#utils/compareObjAttribute'
import checkIncludeResources from '#utils/checkIncludeResource'

const propTypes = components.OrderContainer.propTypes
const defaultProps = components.OrderContainer.defaultProps
Expand Down Expand Up @@ -58,29 +59,45 @@ const OrderContainer: React.FunctionComponent<OrderContainerProps> = (
)
if (config.accessToken) {
const localOrder = persistKey ? getLocalOrder(persistKey) : orderId
if (localOrder) {
dispatch({
type: 'setOrderId',
payload: {
orderId: localOrder,
},
if (
localOrder &&
!state.order &&
startRequest.length === state.include?.length
) {
const removeOrderPlaced = !!(persistKey && clearWhenPlaced)
getApiOrder({
id: localOrder,
dispatch,
config,
persistKey,
clearWhenPlaced: removeOrderPlaced,
deleteLocalOrder,
state,
})
} else if (state?.order && state?.include) {
const allIncluded = checkIncludeResources({
order: state.order,
resourceInclude: state.include,
})
if (!state.order && startRequest.length === state.include?.length) {
const removeOrderPlaced = !!(persistKey && clearWhenPlaced)
if (!allIncluded) {
getApiOrder({
id: localOrder,
id: state.orderId,
dispatch,
config,
persistKey,
clearWhenPlaced: removeOrderPlaced,
deleteLocalOrder,
state,
})
}
}
}
return (): void => unsetOrderState(dispatch)
}, [config.accessToken, orderId, state.includeLoaded, state.include])
}, [
config.accessToken,
state.includeLoaded,
state.include,
orderId,
state.order,
])
useEffect(() => {
if (state.orderId && attributes && state.order) {
const updateAttributes = compareObjAttribute({
Expand Down
9 changes: 2 additions & 7 deletions src/components/utils/BaseOrderPrice.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import OrderContext from '#context/OrderContext'
import getAmount from '#utils/getAmount'
import Parent from './Parent'
import React, {
FunctionComponent,
useState,
useEffect,
useContext,
} from 'react'
import { FunctionComponent, useState, useEffect, useContext } from 'react'
import { PropsType } from '#utils/PropsType'
import { baseOrderPricePropTypes } from '#typings'
import { isEmpty } from 'lodash'
import isEmpty from 'lodash/isEmpty'

export type BaseOrderPriceProps = PropsType<typeof baseOrderPricePropTypes> &
JSX.IntrinsicElements['span']
Expand Down
2 changes: 1 addition & 1 deletion src/reducers/OrderReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export const getApiOrder: GetOrder = async (params) => {
})
}
return order
} catch (error: any) {
} catch (error: unknown) {
const errors = getErrors(error, 'orders')
console.error('Retrieve order', errors)
if (dispatch)
Expand Down
26 changes: 26 additions & 0 deletions src/utils/checkIncludeResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ResourceIncluded } from '#reducers/OrderReducer'
import type { Order } from '@commercelayer/sdk'

type Params = {
order: Order
resourceInclude: ResourceIncluded[]
}

export default function checkIncludeResources({
order,
resourceInclude,
}: Params) {
const checkKeys: boolean[] = []
resourceInclude.forEach((v) => {
const isMultiKey = v.includes('.')
if (isMultiKey) {
const [first] = v.split('.')
const resource = order?.[first as keyof Order]
if (resource === undefined) checkKeys.push(false)
} else {
const resource = order?.[v as keyof Order]
if (resource === undefined) checkKeys.push(false)
}
})
return checkKeys.length === 0
}

0 comments on commit c7b5935

Please sign in to comment.