Skip to content

Commit c4a11a4

Browse files
committed
Remove cart item
1 parent 453176f commit c4a11a4

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

nuxt3/components/Cart/CartContents.vue

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
<template>
22
<div>
3-
<div v-if="data">
3+
<div v-if="data.cart?.contents?.nodes?.length">
44
<h1 class="h-10 p-6 text-3xl font-bold text-center">Cart</h1>
55
<section class="mt-10">
66
<div
77
v-for="products in data.cart.contents.nodes"
88
:key="products.id"
99
class="container mx-auto mt-4 flex-container"
1010
>
11+
<div class="item">
12+
<span class="block mt-2 font-extrabold">Remove: <br /></span>
13+
<span class="item-content">
14+
<img
15+
class="mt-2 ml-4 cursor-pointer"
16+
:class="{ removing: isRemoving }"
17+
alt="Remove icon"
18+
aria-label="Remove"
19+
src="@/assets/svg/Remove.svg"
20+
@click="handleRemoveProduct(products)"
21+
/>
22+
</span>
23+
</div>
24+
1125
<div class="item">
1226
<span class="block mt-2 font-extrabold">Name: <br /></span>
1327
<span class="item-content">{{ products.product.name }}</span>
@@ -25,21 +39,61 @@
2539
</div>
2640
</section>
2741
</div>
28-
<h2 v-if="!data" class="m-4 text-3xl text-center">
42+
<h2
43+
v-if="!data.cart?.contents?.nodes?.length"
44+
class="m-4 text-3xl text-center"
45+
>
2946
Cart is currently empty
3047
</h2>
31-
<CartCheckoutButton v-if="showCheckoutButton && data" />
48+
<CartCheckoutButton
49+
v-if="showCheckoutButton && data.cart?.contents?.nodes?.length"
50+
/>
3251
</div>
3352
</template>
3453

3554
<script setup>
3655
import GET_CART_QUERY from '@/apollo/queries/GET_CART_QUERY.gql'
56+
import UPDATE_CART_MUTATION from '@/apollo/mutations/UPDATE_CART_MUTATION.gql'
57+
58+
const router = useRouter()
59+
60+
const isRemoving = useState('isRemoving', () => false)
3761
3862
defineProps({
3963
showCheckoutButton: { type: Boolean, required: false, default: false },
4064
})
4165
4266
const { data } = await useAsyncQuery(GET_CART_QUERY)
67+
68+
const handleRemoveProduct = ({ key }) => {
69+
let updatedItems = []
70+
71+
updatedItems.push({
72+
key: key,
73+
quantity: 0,
74+
})
75+
76+
isRemoving.value = true
77+
78+
const variables = {
79+
input: {
80+
items: updatedItems,
81+
},
82+
}
83+
84+
const { mutate, onDone, onError } = useMutation(UPDATE_CART_MUTATION, {
85+
variables,
86+
})
87+
88+
mutate(variables)
89+
90+
onDone(() => {
91+
isRemoving.value = false
92+
document.location = '/cart'
93+
})
94+
95+
onError(() => (isRemoving.value = false))
96+
}
4397
</script>
4498

4599
<style scoped>

0 commit comments

Comments
 (0)