Skip to content

Commit

Permalink
Improved: simplified logic to update selected features (hotwax#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
amansinghbais committed Apr 3, 2024
1 parent a5d39ce commit a48fde6
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions src/views/catalog-product-details.vue
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,9 @@ export default defineComponent({
})
return isVariantAvailable
})
availableVariants[0].featureHierarchy.map((featureItem: any) => {
if(featureItem.startsWith('1/')){
const featureItemSplitted = featureItem.split("/")
selectedFeatures[featureItemSplitted[1]] = featureItemSplitted[2]
}
})
this.updateSeletedFeatures(availableVariants[0])
this.currentVariant = availableVariants[0]
this.selectedFeatures = selectedFeatures
showToast(translate("Selected variant not available. Reseting to first variant."))
}
Expand All @@ -528,27 +522,14 @@ export default defineComponent({
},
getVariant() {
let selectedVariant = this.product.variants.find((variant: any) => variant.productId === this.variantId)
let selectedFeatures = {} as any;
if(!selectedVariant) {
selectedVariant = this.product.variants[0]
showToast(translate("Selected variant not available. Reseting to first variant."))
this.$route.query.variantId !== selectedVariant.productId && (this.router.replace({path: this.$route.path, query: { variantId: selectedVariant.productId } }));
}
selectedVariant.featureHierarchy.map((featureItem: any) => {
if(featureItem.startsWith('1/')){
const featureItemSplitted = featureItem.split("/")
selectedFeatures[featureItemSplitted[1]] = featureItemSplitted[2]
}
})
selectedFeatures = Object.keys(selectedFeatures).sort().reduce((result:any, key) => {
result[key] = selectedFeatures[key];
return result;
}, {});
this.selectedFeatures = selectedFeatures
this.updateSeletedFeatures(selectedVariant)
this.currentVariant = selectedVariant
},
getFeatures() {
Expand Down Expand Up @@ -595,6 +576,22 @@ export default defineComponent({
this.features = features
},
updateSeletedFeatures(variant: any) {
let selectedFeatures = {} as any;
variant.featureHierarchy.map((featureItem: any) => {
if(featureItem.startsWith('1/')){
const featureItemSplitted = featureItem.split("/")
selectedFeatures[featureItemSplitted[1]] = featureItemSplitted[2]
}
})
selectedFeatures = Object.keys(selectedFeatures).sort().reduce((result:any, key) => {
result[key] = selectedFeatures[key];
return result;
}, {});
this.selectedFeatures = selectedFeatures
},
async updateVariant() {
this.variantId = this.currentVariant.variantId
this.$route.query.variantId !== this.currentVariant.productId && (this.router.replace({path: this.$route.path, query: { variantId: this.currentVariant.productId } }));
Expand Down

0 comments on commit a48fde6

Please sign in to comment.