|
1 | 1 | <template> |
2 | 2 | <div> |
3 | | - <div v-if="data"> |
| 3 | + <div v-if="data.cart?.contents?.nodes?.length"> |
4 | 4 | <h1 class="h-10 p-6 text-3xl font-bold text-center">Cart</h1> |
5 | 5 | <section class="mt-10"> |
6 | 6 | <div |
7 | 7 | v-for="products in data.cart.contents.nodes" |
8 | 8 | :key="products.id" |
9 | 9 | class="container mx-auto mt-4 flex-container" |
10 | 10 | > |
| 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 | + |
11 | 25 | <div class="item"> |
12 | 26 | <span class="block mt-2 font-extrabold">Name: <br /></span> |
13 | 27 | <span class="item-content">{{ products.product.name }}</span> |
|
25 | 39 | </div> |
26 | 40 | </section> |
27 | 41 | </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 | + > |
29 | 46 | Cart is currently empty |
30 | 47 | </h2> |
31 | | - <CartCheckoutButton v-if="showCheckoutButton && data" /> |
| 48 | + <CartCheckoutButton |
| 49 | + v-if="showCheckoutButton && data.cart?.contents?.nodes?.length" |
| 50 | + /> |
32 | 51 | </div> |
33 | 52 | </template> |
34 | 53 |
|
35 | 54 | <script setup> |
36 | 55 | 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) |
37 | 61 |
|
38 | 62 | defineProps({ |
39 | 63 | showCheckoutButton: { type: Boolean, required: false, default: false }, |
40 | 64 | }) |
41 | 65 |
|
42 | 66 | 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 | +} |
43 | 97 | </script> |
44 | 98 |
|
45 | 99 | <style scoped> |
|
0 commit comments