Skip to content

Commit

Permalink
fix: Callback response from Errors component
Browse files Browse the repository at this point in the history
  • Loading branch information
acasazza committed Apr 12, 2022
1 parent 2b9bbbd commit eac98be
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/components/GiftCardOrCouponForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ const GiftCardOrCouponForm: FunctionComponent<GiftCardOrCouponFormProps> = (
errors,
(i) => i.field === camelCase(inputName)
) as BaseError[]
setOrderErrors({ errors: err })
onSubmit && onSubmit({ value: values[inputName]?.value, success: true })
setOrderErrors(err)
onSubmit && onSubmit({ value: values[inputName]?.value, success: false })
}
if (values[inputName]?.value === '') {
setOrderErrors([])
onSubmit && onSubmit({ value: values[inputName]?.value, success: false })
}
}, [values])
Expand Down
9 changes: 6 additions & 3 deletions src/components/OrderContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, {
useEffect,
FunctionComponent,
useReducer,
useContext,
ReactNode,
Expand All @@ -25,6 +24,7 @@ import components from '#config/components'
import { BaseMetadataObject } from '#typings'
import OrderStorageContext from '#context/OrderStorageContext'
import { OrderCreate, Order } from '@commercelayer/sdk'
import { BaseError } from '#typings/errors'

const propTypes = components.OrderContainer.propTypes
const defaultProps = components.OrderContainer.defaultProps
Expand All @@ -37,7 +37,9 @@ type OrderContainerProps = {
orderId?: string
}

const OrderContainer: FunctionComponent<OrderContainerProps> = (props) => {
const OrderContainer: React.FunctionComponent<OrderContainerProps> = (
props
) => {
const { orderId, children, metadata, attributes } = props
const [state, dispatch] = useReducer(orderReducer, orderInitialState)
const config = useContext(CommerceLayerContext)
Expand Down Expand Up @@ -83,7 +85,8 @@ const OrderContainer: FunctionComponent<OrderContainerProps> = (props) => {
setOrder: (order: Order) => setOrder(order, dispatch),
getOrder: (id: string): Promise<void | Order> =>
getApiOrder({ id, dispatch, config, state }),
setOrderErrors: (errors: any) => setOrderErrors({ dispatch, errors }),
setOrderErrors: (errors: BaseError[]) =>
setOrderErrors({ dispatch, errors }),
createOrder: async (): Promise<string> =>
await createOrder({
persistKey,
Expand Down
7 changes: 4 additions & 3 deletions src/components/utils/getAllErrors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ const getAllErrors: GetAllErrors = (params) => {
} = params
return allErrors.map((v, k): ReactNode | void => {
const objMsg = customMessages(messages, v)
const text =
(objMsg?.message || v.message) && v.title !== v.detail
let text =
v?.title && !v.detail?.includes(v.title)
? `${v.title} - ${v.detail}`
: `${v.title}`
: `${v.detail}`
if (objMsg?.message) text = objMsg?.message
if (field) {
if (v.resource === 'line_items') {
if (lineItem && v.id === lineItem['id']) {
Expand Down
3 changes: 2 additions & 1 deletion src/context/OrderContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import {
updateOrder,
getOrderContext,
} from '#reducers/OrderReducer'
import { BaseError } from '#typings/errors'

type DefaultContext = {
createOrder: CreateOrder
addToCart: AddToCart
setOrderErrors: (errors: unknown) => void
setOrderErrors: (errors: BaseError[]) => void
setGiftCardOrCouponCode: SetGiftCardOrCouponCode
removeGiftCardOrCouponCode: RemoveGiftCardOrCouponCode
saveAddressToCustomerAddressBook: SaveAddressToCustomerAddressBook
Expand Down
11 changes: 7 additions & 4 deletions src/reducers/OrderReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export async function updateOrder({
// NOTE: Retrieve doesn't response with attributes updated
const order = await getApiOrder({ id, config, dispatch, state })
dispatch && order && dispatch({ type: 'setOrder', payload: { order } })
return { success: true }
} catch (error) {
const errors = getErrors(error, 'orders')
if (dispatch) {
Expand All @@ -289,6 +290,7 @@ export async function updateOrder({
},
})
}
return { success: false, error }
}
}

Expand Down Expand Up @@ -435,10 +437,10 @@ export const unsetOrderState: UnsetOrderState = (dispatch) => {

type OrderErrors = {
dispatch?: Dispatch<OrderActions>
errors: any
errors: BaseError[]
}

export function setOrderErrors({ dispatch, errors }: OrderErrors) {
export function setOrderErrors({ dispatch, errors = [] }: OrderErrors) {
dispatch &&
dispatch({
type: 'setErrors',
Expand Down Expand Up @@ -493,21 +495,22 @@ export const setGiftCardOrCouponCode: SetGiftCardOrCouponCode = async ({
const attributes: Omit<OrderUpdate, 'id'> = {
[codeType]: code,
}
await updateOrder({
const { success, error } = await updateOrder({
id: order.id,
attributes,
config,
include,
dispatch,
state,
})
if (!success) throw error
dispatch({
type: 'setErrors',
payload: {
errors: [],
},
})
return { success: true }
return { success }
}
return { success: false }
} catch (error: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/reducers/PlaceOrderReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export type SetPlaceOrder = (args: {
config?: CommerceLayerConfig
order?: Order
state?: PlaceOrderState
setOrderErrors?: (collection: unknown) => void
setOrderErrors?: (errors: BaseError[]) => void
paymentSource?: PaymentSourceType & { approval_url?: string }
include?: string[]
setOrder?: (order: Order) => void
Expand Down

0 comments on commit eac98be

Please sign in to comment.