Skip to content

Commit

Permalink
fix: Save customer address book and include order resources
Browse files Browse the repository at this point in the history
  • Loading branch information
acasazza committed Dec 1, 2021
1 parent 7772a74 commit 4854f64
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
47 changes: 33 additions & 14 deletions src/reducers/OrderReducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Dispatch } from 'react'
import { SetLocalOrder, DeleteLocalOrder } from '#utils/localStorage'
import {
SetLocalOrder,
DeleteLocalOrder,
setCustomerOrderParam,
CustomerOrderParams,
} from '#utils/localStorage'
import { CommerceLayerConfig } from '#context/CommerceLayerContext'
import baseReducer from '#utils/baseReducer'
import { ItemOption, CustomLineItem } from './ItemReducer'
Expand Down Expand Up @@ -87,7 +92,7 @@ export interface UnsetOrderState {
(dispatch: Dispatch<OrderActions>): void
}

type resourceIncluded =
export type ResourceIncluded =
| 'billing_address'
| 'shipping_address'
| 'line_items.line_item_options.sku_option'
Expand All @@ -101,12 +106,15 @@ type resourceIncluded =
| 'available_payment_methods'
| 'payment_method'

type ResourceIncludedLoaded = Partial<Record<ResourceIncluded, boolean>>

export interface OrderPayload {
loading?: boolean
orderId?: string
order?: Order
errors?: BaseError[]
include?: resourceIncluded[]
include?: ResourceIncluded[]
includeLoaded?: ResourceIncludedLoaded
}

export type AddToCartValues = Pick<
Expand Down Expand Up @@ -292,26 +300,37 @@ export const setOrder = (
}

export type AddResourceToInclude = {
resourcesIncluded?: resourceIncluded[]
resourcesIncluded?: ResourceIncluded[]
dispatch?: Dispatch<OrderActions>
newResource: resourceIncluded | resourceIncluded[]
newResource?: ResourceIncluded | ResourceIncluded[]
resourceIncludedLoaded?: ResourceIncludedLoaded
newResourceLoaded?: ResourceIncludedLoaded
}

export function addResourceToInclude({
resourcesIncluded = [],
dispatch,
newResource,
newResourceLoaded,
resourceIncludedLoaded,
}: AddResourceToInclude) {
const include = [
...resourcesIncluded,
...(typeof newResource === 'string' ? [newResource] : newResource),
]
const payload: any = {
include: undefined,
includeLoaded: undefined,
}
if (newResource) {
payload.include = [
...resourcesIncluded,
...(typeof newResource === 'string' ? [newResource] : newResource),
]
} else {
delete payload.include
}
payload.includeLoaded = { ...resourceIncludedLoaded, ...newResourceLoaded }
dispatch &&
dispatch({
type: 'setIncludesResource',
payload: {
include,
},
payload,
})
}

Expand Down Expand Up @@ -432,9 +451,9 @@ export type SaveAddressToCustomerAddressBook = (params: {

export const saveAddressToCustomerAddressBook: SaveAddressToCustomerAddressBook =
({ type, value, dispatch }) => {
const k = `save_${type}_to_customer_book`
const k: CustomerOrderParams = `_save_${type}_to_customer_address_book`
const v = `${value}`
localStorage.setItem(k, v)
setCustomerOrderParam(k, v)
dispatch &&
dispatch({
type: 'setSaveAddressToCustomerAddressBook',
Expand Down
2 changes: 1 addition & 1 deletion src/utils/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const getSaveShippingAddressToAddressBook = (): boolean => {
)
}

type CustomerOrderParams =
export type CustomerOrderParams =
| '_save_payment_source_to_customer_wallet'
| '_save_billing_address_to_customer_address_book'
| '_save_shipping_address_to_customer_address_book'
Expand Down

0 comments on commit 4854f64

Please sign in to comment.