Vue.component("ProductSetup", {
props: ['productKey', 'product'],
methods: {
handleInputChange(e) {
this.$emit("update:product-type", {
...e,
product: this.productKey,
})
},
handleQuantityChange(e) {
this.$emit("update:product-quantity", {
...e,
product: this.productKey,
})
},
},
template: `
<div>
<label
v-for="(val, key) in product.types"
:key="key"
>
{{ key }}
<input
type="radio"
:value="val"
:name="productKey"
@change="() => handleInputChange({ type: key })"
/>
</label>
<input
type="number"
min="0"
@input="(e) => handleQuantityChange({ quantity: e.target.value })"
/>
</div>
`,
})
Vue.component("PriceEstimate", {
props: ["products"],
template: `
<div>
<div
v-for="(product, productKey) in products"
>
{{ productKey }}<br />
{{ productKey }} {{ product.chosen }}: {{ product.types[product.chosen] }}<br />
Quantity {{ productKey }}: {{ product.quantity * product.pricePerPiece }}<br />
TOTAL: {{ product.types[product.chosen] + product.quantity * product.pricePerPiece }}
</div>
</div>
`,
})
new Vue({
el: "#app",
computed: {
estimates() {
return Object.fromEntries(
Object.entries(
this.products
).filter(([key, val]) => {
return val.chosen && val.quantity
})
)
},
},
data() {
return {
products: {
storage: {
chosen: null,
types: {
type1: 60,
type2: 70,
},
pricePerPiece: 20,
quantity: null,
},
os: {
chosen: null,
types: {
os1: 60,
os2: 70,
os3: 80,
},
pricePerPiece: 35,
quantity: null,
},
},
}
},
methods: {
handleUpdateProduct({
type,
product
}) {
this.products[product]["chosen"] = type
},
handleUpdateQuantity({
quantity,
product
}) {
this.products[product]["quantity"] = quantity
}
},
template: `
<div>
<div
v-for="(obj, product) in products"
>
Chosen {{ product }}: {{ obj.chosen }}<br />
Quantity of {{ product }}: {{ obj.quantity }}
<hr />
<product-setup
:product-key="product"
:product="obj"
@update:product-type="(e) => handleUpdateProduct(e)"
@update:product-quantity="(e) => handleUpdateQuantity(e)"
/>
<hr />
</div>
<price-estimate
:products="estimates"
/>
</div>
`,
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app"></div>