Skip to content
Open
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
16 changes: 13 additions & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
import { ref } from 'vue'
import ProductDisplay from '@/components/ProductDisplay.vue'

const cart = ref(0)
const cart = ref([])
const premium = ref(true)

const updateCart = (id) => {
cart.value.push(id)
}
const removeFromCart = (id) => {
const index = cart.value.indexOf(id)
if (index !== -1) {
cart.value.splice(index, 1)
}
}

</script>

<template>
<div class="nav-bar"></div>
<div class="cart">Cart({{ cart }})</div>
<ProductDisplay :premium="premium"></ProductDisplay>
<div class="cart">Cart({{ cart.length }})</div>
<ProductDisplay :premium="premium" @add-to-cart="updateCart" @remove-from-cart="removeFromCart"></ProductDisplay>
</template>
16 changes: 15 additions & 1 deletion src/components/ProductDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const props = defineProps({
}
})

const emit = defineEmits(['add-to-cart'], ['remove-from-cart'])

const product = ref('Socks')
const brand = ref('Vue Mastery')

Expand Down Expand Up @@ -43,7 +45,13 @@ const shipping = computed(() => {
}
})

const addToCart = () => cart.value += 1
const addToCart = () => {
emit('add-to-cart', variants.value[selectedVariant.value].id)
}

const removeFromCart = () => {
emit('remove-from-cart', variants.value[selectedVariant.value].id)
}

const updateVariant = (index) => {
selectedVariant.value = index
Expand Down Expand Up @@ -80,6 +88,12 @@ const updateVariant = (index) => {
>
Add to cart
</button>
<button
class="button"
v-on:click="removeFromCart"
>
Remove
</button>
</div>
</div>
</div>
Expand Down