Skip to content

Commit

Permalink
Merge pull request #78 from JooLuiz/feature/merge-quantit-in-order-form
Browse files Browse the repository at this point in the history
merging the quantity being added with the quantity already in the order form, instead of replacing the quantity
  • Loading branch information
wender authored Dec 29, 2021
2 parents b3171c4 + f5236f4 commit 9f9553f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
8 changes: 8 additions & 0 deletions react/AutocompleteBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const AutocompleteBlock: StorefrontFunctionComponent<any &
const { promptOnCustomEvent } = settings

const { setOrderForm }: OrderFormContext = OrderForm.useOrderForm()
const orderForm = OrderForm.useOrderForm()

const translateMessage = (message: MessageDescriptor) => {
return intl.formatMessage(message)
Expand Down Expand Up @@ -115,9 +116,16 @@ const AutocompleteBlock: StorefrontFunctionComponent<any &

const { selectedItem, quantitySelected, unitMultiplier } = state
const callAddToCart = async (items: any) => {
const currentItemsInCart = orderForm.orderForm.items
const mutationResult = await addToCart({
variables: {
items: items.map((item: any) => {
const [existsInCurrentOrder] = currentItemsInCart.filter(
el => el.id === item.id.toString()
)
if (existsInCurrentOrder) {
item.quantity = item.quantity + existsInCurrentOrder.quantity
}
return {
...item,
}
Expand Down
27 changes: 21 additions & 6 deletions react/TextAreaBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const TextAreaBlock: StorefrontFunctionComponent<TextAreaBlockInterface &
const { promptOnCustomEvent } = settings

const { setOrderForm }: OrderFormContext = OrderForm.useOrderForm()
const orderForm = OrderForm.useOrderForm()
const { showToast } = useContext(ToastContext)

const translateMessage = (message: MessageDescriptor) => {
Expand Down Expand Up @@ -97,9 +98,16 @@ const TextAreaBlock: StorefrontFunctionComponent<TextAreaBlockInterface &
}

const callAddToCart = async (items: any) => {
const currentItemsInCart = orderForm.orderForm.items
const mutationResult = await addToCart({
variables: {
items: items.map((item: any) => {
const [existsInCurrentOrder] = currentItemsInCart.filter(
el => el.id === item.id.toString()
)
if (existsInCurrentOrder) {
item.quantity = item.quantity + existsInCurrentOrder.quantity
}
return {
...item,
}
Expand Down Expand Up @@ -211,13 +219,20 @@ const TextAreaBlock: StorefrontFunctionComponent<TextAreaBlockInterface &
seller,
}
})

if (items.length === 0) {
toastMessage({ success: false, isNewItem: false })
return
const merge = internalItems => {
return internalItems.reduce((acc, val) => {
const { id, quantity } = val
const ind = acc.findIndex(el => el.id === id)
if (ind !== -1) {
acc[ind].quantity += quantity
} else {
acc.push(val)
}

callAddToCart(items)
return acc
}, [])
}
const mergedItems = merge(items)
callAddToCart(mergedItems)
}
const onRefidLoading = (data: boolean) => {
setRefIdLoading(data)
Expand Down
27 changes: 21 additions & 6 deletions react/UploadBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const UploadBlock: StorefrontFunctionComponent<UploadBlockInterface &
const { promptOnCustomEvent } = settings

const { setOrderForm }: OrderFormContext = OrderForm.useOrderForm()
const orderForm = OrderForm.useOrderForm()
const { showToast } = useContext(ToastContext)

const translateMessage = (message: MessageDescriptor) => {
Expand Down Expand Up @@ -216,9 +217,16 @@ const UploadBlock: StorefrontFunctionComponent<UploadBlockInterface &
for (let i = 0; i < loopCount; i++) {
const chunk = tempItems.splice(0, splitBy)
if (chunk.length) {
const currentItemsInCart = orderForm.orderForm.items
const mutationChunk = await addToCart({
variables: {
items: chunk.map((item: any) => {
const [existsInCurrentOrder] = currentItemsInCart.filter(
el => el.id === item.id.toString()
)
if (existsInCurrentOrder) {
item.quantity = item.quantity + existsInCurrentOrder.quantity
}
return {
...item,
}
Expand Down Expand Up @@ -289,13 +297,20 @@ const UploadBlock: StorefrontFunctionComponent<UploadBlockInterface &
seller,
}
})

if (items.length === 0) {
toastMessage({ success: false, isNewItem: false })
return
const merge = internalItems => {
return internalItems.reduce((acc, val) => {
const { id, quantity } = val
const ind = acc.findIndex(el => el.id === id)
if (ind !== -1) {
acc[ind].quantity += quantity
} else {
acc.push(val)
}

callAddToCart(items)
return acc
}, [])
}
const mergedItems = merge(items)
callAddToCart(mergedItems)
}

const CSS_HANDLES = [
Expand Down

0 comments on commit 9f9553f

Please sign in to comment.