Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing SGR #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions react/Summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const Summary: FC<Props> = ({ classes }) => {
} = useOrderForm()

const [packagesSkuIds, setPackagesSkuIds] = useState<string[]>([])
const [sgrSkuIds, setSgrSkuIds] = useState<string[]>([])

useEffect(() => {
let isSubscribed = true
Expand All @@ -27,7 +28,19 @@ const Summary: FC<Props> = ({ classes }) => {
(res: PackagesSkuIds) => {
if (res && isSubscribed) {
try {
setPackagesSkuIds(Object.values(res.data))
const { bagsSettings, sgrSettings } = res?.data ?? {}

setPackagesSkuIds(Object.values(bagsSettings))

const allSkuIds: string[] = []

Object.values(sgrSettings).forEach(sgrType => {
if (sgrType?.skuIds) {
allSkuIds.push(...sgrType.skuIds)
}
})

setSgrSkuIds(allSkuIds)
} catch (error) {
console.error('Error in packages feature.', error)
}
Expand All @@ -54,13 +67,26 @@ const Summary: FC<Props> = ({ classes }) => {
}, 0)
}, [items, packagesSkuIds])

let newTotalizers = totalizers
const sgrValue = useMemo(() => {
if (!sgrSkuIds.length) {
return
}
return items.reduce((total: number, item: OrderFormItem) => {
if (sgrSkuIds.includes(item.id)) {
return (
total + ((item?.listPrice as number) ?? 0) * (item?.quantity ?? 1)
)
}
return total
}, 0)
}, [items, sgrSkuIds])

if (flegValue && typeof flegValue === 'number') {
newTotalizers = JSON.parse(JSON.stringify(totalizers))
let newTotalizers = totalizers

const totalizerItems = newTotalizers.find((t: any) => t.id === 'Items')
newTotalizers = JSON.parse(JSON.stringify(totalizers))
const totalizerItems = newTotalizers.find((t: any) => t.id === 'Items')

if (flegValue && typeof flegValue === 'number') {
newTotalizers.push({
id: 'Packaging',
name: 'Taxa ambalare',
Expand All @@ -71,6 +97,16 @@ const Summary: FC<Props> = ({ classes }) => {
totalizerItems.value -= flegValue ?? 0
}

if (sgrValue && typeof sgrValue === 'number') {
newTotalizers.push({
id: 'SGR',
name: 'Garantie',
value: sgrValue,
__typename: 'Totalizer',
})
totalizerItems.value -= sgrValue ?? 0
}

const { handles } = useCssHandles(CSS_HANDLES, { classes })

const originalValue =
Expand Down
24 changes: 20 additions & 4 deletions react/components/MinicartIconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ interface Props {
const countCartItems = (
countMode: MinicartTotalItemsType,
allItems: OrderFormItem[],
packagesSkuIds: string[]
packagesSkuIds: string[],
sgrSkuIds: string[]
) => {
// Filter only main products, remove assembly items from the count
const items = allItems.filter(
item =>
item.parentItemIndex === null &&
item.productId &&
!packagesSkuIds.includes(item.productId)
!packagesSkuIds.includes(item.productId) &&
!sgrSkuIds.includes(item.productId)
)

if (countMode === 'distinctAvailable') {
Expand Down Expand Up @@ -69,6 +71,7 @@ const MinicartIconButton: React.FC<Props> = props => {
const { open, openBehavior, openOnHoverProp } = useMinicartState()
const dispatch = useMinicartDispatch()
const [packagesSkuIds, setPackagesSkuIds] = useState<string[]>([])
const [sgrSkuIds, setSgrSkuIds] = useState<string[]>([])

useEffect(() => {
let isSubscribed = true
Expand All @@ -77,7 +80,19 @@ const MinicartIconButton: React.FC<Props> = props => {
(res: PackagesSkuIds) => {
if (res && isSubscribed) {
try {
setPackagesSkuIds(Object.values(res.data))
const { bagsSettings, sgrSettings } = res?.data ?? {}

setPackagesSkuIds(Object.values(bagsSettings))

const allSkuIds: string[] = []

Object.values(sgrSettings).forEach(sgrType => {
if (sgrType?.skuIds) {
allSkuIds.push(...sgrType.skuIds)
}
})

setSgrSkuIds(allSkuIds)
} catch (error) {
console.error('Error in packages feature.', error)
}
Expand All @@ -92,7 +107,8 @@ const MinicartIconButton: React.FC<Props> = props => {
const quantity = countCartItems(
itemCountMode,
orderForm.items,
packagesSkuIds
packagesSkuIds,
sgrSkuIds
)
const itemQuantity = loading ? 0 : quantity
const { url: checkoutUrl } = useCheckoutURL()
Expand Down
8 changes: 5 additions & 3 deletions react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@
"@vtex/test-tools": "^3.1.0",
"@vtex/tsconfig": "^0.4.4",
"apollo-cache-inmemory": "^1.6.5",
"auchan.checkout-summary": "https://narcissgr--auchanqa.myvtex.com/_v/private/typings/linked/v1/[email protected]+build1702379716/public/@types/auchan.checkout-summary",
"auchan.minicart": "https://narcissgr--auchanqa.myvtex.com/_v/private/typings/linked/v1/[email protected]+build1702379665/public/@types/auchan.minicart",
"auchan.product-list": "https://narcissgr--auchanqa.myvtex.com/_v/private/typings/linked/v1/[email protected]+build1702379804/public/@types/auchan.product-list",
"babel-eslint": "^10.1.0",
"typescript": "3.9.7",
"vtex.checkout-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.checkout-graphql",
"vtex.checkout-resources": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.checkout-resources",
"vtex.checkout-summary": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.checkout-summary",
"vtex.css-handles": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.css-handles",
"vtex.device-detector": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.device-detector",
"vtex.flex-layout": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.flex-layout",
"vtex.order-items": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.order-items",
"vtex.order-manager": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.order-manager",
"vtex.pixel-manager": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.pixel-manager",
"vtex.product-summary": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.product-summary@2.85.0/public/@types/vtex.product-summary",
"vtex.product-summary": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.product-summary@2.87.0/public/@types/vtex.product-summary",
"vtex.react-portal": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.react-portal",
"vtex.render-runtime": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.render-runtime",
"vtex.responsive-values": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.responsive-values",
"vtex.rich-text": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.rich-text",
"vtex.sandbox": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.sandbox",
"vtex.store-components": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-components@3.169.5/public/@types/vtex.store-components",
"vtex.store-components": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.store-components@3.170.0/public/@types/vtex.store-components",
"vtex.store-drawer": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-drawer",
"vtex.store-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-graphql",
"vtex.store-icons": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-icons",
Expand Down
24 changes: 20 additions & 4 deletions react/typings/packages-sku-ids.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
interface PackagesSkuIds {
data: {
bioFlegBagId: string
insulationBagId: string
plasticBagId: string
paperBagId: string
bagsSettings: {
bioFlegBagId: string
insulationBagId: string
plasticBagId: string
paperBagId: string
}
sgrSettings: {
aluminumCanProducts: {
label: string
skuIds: string[]
}
glassBottleProducts: {
label: string
skuIds: string[]
}
plasticBottleProducts: {
label: string
skuIds: string[]
}
}
}
error: null | string
}