simplifying reduce

This commit is contained in:
Michaela Brydon 2024-04-03 16:31:32 -04:00
parent 628a07b30d
commit 8e97a54fbc
2 changed files with 25 additions and 27 deletions

View file

@ -1125,7 +1125,7 @@ describe('cart-dropdown component', () => {
// Assert // Assert
expect(result).toContainEqual(expect.objectContaining({ expect(result).toContainEqual(expect.objectContaining({
isRecycle: true partType: partTypeStrings.REPLACE_FEE
})); }));
}); });
test('when mobileFee, includes mobile fee', () => { test('when mobileFee, includes mobile fee', () => {
@ -1355,7 +1355,7 @@ describe('cart-dropdown component', () => {
// Assert // Assert
expect(result.length).toBe(3); expect(result.length).toBe(3);
expect(result).toContainEqual(expect.objectContaining({ expect(result).toContainEqual(expect.objectContaining({
isRecycle: true partType: partTypeStrings.REPLACE_FEE
})); }));
expect(result).toContainEqual(expect.objectContaining({ expect(result).toContainEqual(expect.objectContaining({
partType: partTypeStrings.MOBILE_FEE partType: partTypeStrings.MOBILE_FEE

View file

@ -63,7 +63,7 @@
linkType="text" linkType="text"
text="Remove" text="Remove"
href="javascript:void(0)" href="javascript:void(0)"
@clickEvent="removeItem(item.partType, true)"> @clickEvent="removeVap(item.partType)">
<template #after-text> <template #after-text>
<span class="sr-only">{{ item?.name ?? '' }}</span> <span class="sr-only">{{ item?.name ?? '' }}</span>
</template> </template>
@ -81,7 +81,7 @@
class="px-5 py-1 non-packaged-cart-item"> class="px-5 py-1 non-packaged-cart-item">
<div class="d-flex justify-content-between align-items-center"> <div class="d-flex justify-content-between align-items-center">
<span <span
v-if="item.isRecycle" v-if="isRecycle(item.partType)"
id="recycle-fee-label"> id="recycle-fee-label">
<textLink <textLink
linkType="text" linkType="text"
@ -93,11 +93,11 @@
<span>{{ getDisplayed(item?.subTotal ?? 0, false) }}</span> <span>{{ getDisplayed(item?.subTotal ?? 0, false) }}</span>
</div> </div>
<textLink <textLink
v-if="item.isRemovable && (!readOnly || !showAsPaid)" v-if="isVap(item.partType) && (!readOnly || !showAsPaid)"
linkType="text" linkType="text"
text="Remove" text="Remove"
href="javascript:void(0)" href="javascript:void(0)"
@clickEvent="removeItem(item.partType, item.isVap)"> @clickEvent="removeVap(item.partType)">
<template #after-text> <template #after-text>
<span class="sr-only">{{ item?.name ?? '' }}</span> <span class="sr-only">{{ item?.name ?? '' }}</span>
</template> </template>
@ -368,10 +368,7 @@ export default {
? this.getCartItem( ? this.getCartItem(
this.getCmsContent(this.widget.recycleFee, widgetFields.TEXT_BLOCK_WIDGET.TEXT), this.getCmsContent(this.widget.recycleFee, widgetFields.TEXT_BLOCK_WIDGET.TEXT),
[recycleFeeLineItem], [recycleFeeLineItem],
false, partTypeStrings.REPLACE_FEE // NOTE this is not a mistake
partTypeStrings.REPLACE_FEE, // NOTE this is not a mistake
false,
true
) )
: null; : null;
}, },
@ -381,10 +378,7 @@ export default {
? this.getCartItem( ? this.getCartItem(
this.getCmsContent(this.widget.mobileFee, widgetFields.TEXT_BLOCK_WIDGET.TEXT), this.getCmsContent(this.widget.mobileFee, widgetFields.TEXT_BLOCK_WIDGET.TEXT),
[mobileFeeLineItem], [mobileFeeLineItem],
false, partTypeStrings.MOBILE_FEE
partTypeStrings.MOBILE_FEE,
false,
false
) )
: null; : null;
} }
@ -404,7 +398,7 @@ export default {
? VERIFYING_COVERAGE ? VERIFYING_COVERAGE
: formatAmountInDollars(amount); : formatAmountInDollars(amount);
}, },
getCartItem(label, lineItems, isRemovable, partType, isVap, isRecycle) { getCartItem(label, lineItems, partType) {
let cartItem = null; let cartItem = null;
if (lineItems && lineItems.length > 0) { if (lineItems && lineItems.length > 0) {
@ -415,10 +409,7 @@ export default {
salesTax: lineItems?.reduce( salesTax: lineItems?.reduce(
(accumulator, lineItem) => accumulator + lineItem.salesTax, (accumulator, lineItem) => accumulator + lineItem.salesTax,
0 0
) ?? 0, ) ?? 0
isRemovable,
isVap,
isRecycle
}; };
} }
@ -428,15 +419,11 @@ export default {
const label = this.getCmsContentForVapsType(partType); const label = this.getCmsContentForVapsType(partType);
const lineItems = useMainStore().lineItems.vaps const lineItems = useMainStore().lineItems.vaps
?.filter((vapsLineItem) => vapsLineItem.partType === partType) ?? []; ?.filter((vapsLineItem) => vapsLineItem.partType === partType) ?? [];
return this.getCartItem(label, lineItems, true, partType, true, false); return this.getCartItem(label, lineItems, partType);
}, },
removeItem(partType, isVap) { removeVap(partType) {
if (isVap) { const newVaps = useMainStore().lineItems.vaps?.filter((vap) => vap?.partType !== partType) ?? [];
const newVaps = useMainStore().lineItems.vaps?.filter((vap) => vap?.partType !== partType) ?? []; useMainStore().updateVaps(newVaps);
console.log(`new vaps: ${JSON.stringify(newVaps)}`);
useMainStore().updateVaps(newVaps);
}
// NOTE Currently only vaps can be removed
}, },
getCmsContentForVapsType(vapsType) { getCmsContentForVapsType(vapsType) {
const vapsItemDescriptions = this.getCmsContent( const vapsItemDescriptions = this.getCmsContent(
@ -451,6 +438,17 @@ export default {
const vapsTypeDescription = vapsItemDescriptions?.find((entry) => entry?.Name === vapsType); const vapsTypeDescription = vapsItemDescriptions?.find((entry) => entry?.Name === vapsType);
return vapsTypeDescription?.Text ?? ''; return vapsTypeDescription?.Text ?? '';
}, },
isVap(partType) {
const vapPartTypes = [
partTypeStrings.FRONT_WIPER,
partTypeStrings.REAR_WIPER,
partTypeStrings.RAIN_DEFENSE
];
return vapPartTypes.includes(partType);
},
isRecycle(partType) {
return partTypeStrings.REPLACE_FEE === partType;
},
formatAmountInDollars formatAmountInDollars
} }
}; };