From 5ae725a5d5bae35ad927bea9adc207c04850135b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Luiz=20de=20Castro?= Date: Mon, 27 Dec 2021 08:24:26 -0300 Subject: [PATCH 1/4] merging the quantity being added with the quantity already in the order form, instead of replacing the quantity --- react/AutocompleteBlock.tsx | 9 +++++++++ react/TextAreaBlock.tsx | 28 ++++++++++++++++++++++------ react/UploadBlock.tsx | 28 ++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/react/AutocompleteBlock.tsx b/react/AutocompleteBlock.tsx index 7cb66c7b..cbf7e448 100644 --- a/react/AutocompleteBlock.tsx +++ b/react/AutocompleteBlock.tsx @@ -69,6 +69,7 @@ const AutocompleteBlock: StorefrontFunctionComponent { return intl.formatMessage(message) @@ -115,9 +116,17 @@ const AutocompleteBlock: StorefrontFunctionComponent { + let currentItemsInCart = orderForm.orderForm.items const mutationResult = await addToCart({ variables: { items: items.map((item: any) => { + let existsInCurrentOrder = currentItemsInCart.filter( + el => el.id === item.id.toString() + ) + if (existsInCurrentOrder.length > 0) { + item['quantity'] = + item['quantity'] + existsInCurrentOrder[0]['quantity'] + } return { ...item, } diff --git a/react/TextAreaBlock.tsx b/react/TextAreaBlock.tsx index 3597d1fa..94080d14 100644 --- a/react/TextAreaBlock.tsx +++ b/react/TextAreaBlock.tsx @@ -66,6 +66,7 @@ const TextAreaBlock: StorefrontFunctionComponent { @@ -97,9 +98,17 @@ const TextAreaBlock: StorefrontFunctionComponent { + let currentItemsInCart = orderForm.orderForm.items const mutationResult = await addToCart({ variables: { items: items.map((item: any) => { + let existsInCurrentOrder = currentItemsInCart.filter( + el => el.id === item.id.toString() + ) + if (existsInCurrentOrder.length > 0) { + item['quantity'] = + item['quantity'] + existsInCurrentOrder[0]['quantity'] + } return { ...item, } @@ -211,13 +220,20 @@ const TextAreaBlock: StorefrontFunctionComponent { + 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) diff --git a/react/UploadBlock.tsx b/react/UploadBlock.tsx index 776bb495..dc1a4f9f 100644 --- a/react/UploadBlock.tsx +++ b/react/UploadBlock.tsx @@ -64,6 +64,7 @@ const UploadBlock: StorefrontFunctionComponent { @@ -216,9 +217,17 @@ const UploadBlock: StorefrontFunctionComponent { + let existsInCurrentOrder = currentItemsInCart.filter( + el => el.id === item.id.toString() + ) + if (existsInCurrentOrder.length > 0) { + item['quantity'] = + item['quantity'] + existsInCurrentOrder[0]['quantity'] + } return { ...item, } @@ -289,13 +298,20 @@ const UploadBlock: StorefrontFunctionComponent { + 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 = [ From 814136be40795247ffd0058aaf1404d19d0a014d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Luiz=20de=20Castro?= Date: Wed, 29 Dec 2021 00:44:40 -0300 Subject: [PATCH 2/4] [Code Enhancement] Adjusting the code to guarentee a better quality, changing lets to consts and using array destructuring --- react/AutocompleteBlock.tsx | 9 ++++----- react/TextAreaBlock.tsx | 7 +++---- react/UploadBlock.tsx | 7 +++---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/react/AutocompleteBlock.tsx b/react/AutocompleteBlock.tsx index cbf7e448..2ca612af 100644 --- a/react/AutocompleteBlock.tsx +++ b/react/AutocompleteBlock.tsx @@ -116,16 +116,15 @@ const AutocompleteBlock: StorefrontFunctionComponent { - let currentItemsInCart = orderForm.orderForm.items + const currentItemsInCart = orderForm.orderForm.items const mutationResult = await addToCart({ variables: { items: items.map((item: any) => { - let existsInCurrentOrder = currentItemsInCart.filter( + const [existsInCurrentOrder] = currentItemsInCart.filter( el => el.id === item.id.toString() ) - if (existsInCurrentOrder.length > 0) { - item['quantity'] = - item['quantity'] + existsInCurrentOrder[0]['quantity'] + if (existsInCurrentOrder?.length > 0) { + item.quantity = item.quantity + existsInCurrentOrder.quantity } return { ...item, diff --git a/react/TextAreaBlock.tsx b/react/TextAreaBlock.tsx index 94080d14..863f5732 100644 --- a/react/TextAreaBlock.tsx +++ b/react/TextAreaBlock.tsx @@ -102,12 +102,11 @@ const TextAreaBlock: StorefrontFunctionComponent { - let existsInCurrentOrder = currentItemsInCart.filter( + const [existsInCurrentOrder] = currentItemsInCart.filter( el => el.id === item.id.toString() ) - if (existsInCurrentOrder.length > 0) { - item['quantity'] = - item['quantity'] + existsInCurrentOrder[0]['quantity'] + if (existsInCurrentOrder?.length > 0) { + item.quantity = item.quantity + existsInCurrentOrder.quantity } return { ...item, diff --git a/react/UploadBlock.tsx b/react/UploadBlock.tsx index dc1a4f9f..2b345d98 100644 --- a/react/UploadBlock.tsx +++ b/react/UploadBlock.tsx @@ -221,12 +221,11 @@ const UploadBlock: StorefrontFunctionComponent { - let existsInCurrentOrder = currentItemsInCart.filter( + const [existsInCurrentOrder] = currentItemsInCart.filter( el => el.id === item.id.toString() ) - if (existsInCurrentOrder.length > 0) { - item['quantity'] = - item['quantity'] + existsInCurrentOrder[0]['quantity'] + if (existsInCurrentOrder?.length > 0) { + item.quantity = item.quantity + existsInCurrentOrder.quantity } return { ...item, From 6b0abb7bf9b31a534f73efe7c678439412584dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Luiz=20de=20Castro?= Date: Wed, 29 Dec 2021 01:11:55 -0300 Subject: [PATCH 3/4] [Code Enhancement] Changing lets to consts --- react/TextAreaBlock.tsx | 2 +- react/UploadBlock.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/react/TextAreaBlock.tsx b/react/TextAreaBlock.tsx index 863f5732..d0ef40b6 100644 --- a/react/TextAreaBlock.tsx +++ b/react/TextAreaBlock.tsx @@ -98,7 +98,7 @@ const TextAreaBlock: StorefrontFunctionComponent { - let currentItemsInCart = orderForm.orderForm.items + const currentItemsInCart = orderForm.orderForm.items const mutationResult = await addToCart({ variables: { items: items.map((item: any) => { diff --git a/react/UploadBlock.tsx b/react/UploadBlock.tsx index 2b345d98..e3f94ace 100644 --- a/react/UploadBlock.tsx +++ b/react/UploadBlock.tsx @@ -217,7 +217,7 @@ const UploadBlock: StorefrontFunctionComponent { From f5236f4a6c86cc14d1b33f8d5b0ed4b957aa7173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Luiz=20de=20Castro?= Date: Wed, 29 Dec 2021 01:41:06 -0300 Subject: [PATCH 4/4] [Code Enhancement] changing the condition where we check if the item exists --- react/AutocompleteBlock.tsx | 2 +- react/TextAreaBlock.tsx | 2 +- react/UploadBlock.tsx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/react/AutocompleteBlock.tsx b/react/AutocompleteBlock.tsx index 2ca612af..07f12ed2 100644 --- a/react/AutocompleteBlock.tsx +++ b/react/AutocompleteBlock.tsx @@ -123,7 +123,7 @@ const AutocompleteBlock: StorefrontFunctionComponent el.id === item.id.toString() ) - if (existsInCurrentOrder?.length > 0) { + if (existsInCurrentOrder) { item.quantity = item.quantity + existsInCurrentOrder.quantity } return { diff --git a/react/TextAreaBlock.tsx b/react/TextAreaBlock.tsx index d0ef40b6..3ed3dc2f 100644 --- a/react/TextAreaBlock.tsx +++ b/react/TextAreaBlock.tsx @@ -105,7 +105,7 @@ const TextAreaBlock: StorefrontFunctionComponent el.id === item.id.toString() ) - if (existsInCurrentOrder?.length > 0) { + if (existsInCurrentOrder) { item.quantity = item.quantity + existsInCurrentOrder.quantity } return { diff --git a/react/UploadBlock.tsx b/react/UploadBlock.tsx index e3f94ace..8dafc22d 100644 --- a/react/UploadBlock.tsx +++ b/react/UploadBlock.tsx @@ -224,7 +224,7 @@ const UploadBlock: StorefrontFunctionComponent el.id === item.id.toString() ) - if (existsInCurrentOrder?.length > 0) { + if (existsInCurrentOrder) { item.quantity = item.quantity + existsInCurrentOrder.quantity } return {