Skip to content

Commit 1178b8f

Browse files
authored
Merge pull request #1002 from w3bdesign/1001-refactor-code
Refactor and reuse CommonButton
2 parents f276a2e + eac485b commit 1178b8f

File tree

4 files changed

+43
-78
lines changed

4 files changed

+43
-78
lines changed

components/Cart/CartContents.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<CommonButton
4949
link-to="/checkout"
5050
v-if="showCheckoutButton && data.cart?.contents?.nodes?.length"
51+
center-button
5152
>CHECKOUT</CommonButton
5253
>
5354
</div>

components/Products/ProductsAddToCartButton.vue

Lines changed: 0 additions & 74 deletions
This file was deleted.

components/Products/ProductsSingleProduct.vue

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@
6565
</select>
6666
</p>
6767
<div class="pt-1 mt-2">
68-
<ProductsAddToCartButton :product="data.product" />
68+
<CommonButton
69+
@common-button-click="addProductToCart(data.product)"
70+
:is-loading="isLoading"
71+
>
72+
ADD TO CART</CommonButton
73+
>
6974
</div>
7075
</div>
7176
</div>
@@ -76,9 +81,12 @@
7681

7782
<script setup>
7883
import GET_SINGLE_PRODUCT_QUERY from "@/apollo/queries/GET_SINGLE_PRODUCT_QUERY.gql";
84+
import ADD_TO_CART_MUTATION from "@/apollo/mutations/ADD_TO_CART_MUTATION.gql";
7985
8086
import { stripHTML, filteredVariantPrice } from "@/utils/functions";
8187
88+
const isLoading = useState("isLoading", () => false);
89+
8290
const config = useRuntimeConfig();
8391
8492
const props = defineProps({
@@ -88,4 +96,29 @@ const props = defineProps({
8896
8997
const variables = { id: props.id, slug: props.slug };
9098
const { data } = await useAsyncQuery(GET_SINGLE_PRODUCT_QUERY, variables);
99+
100+
const addProductToCart = async (product) => {
101+
const productId = product.databaseId ? product.databaseId : product;
102+
const productQueryInput = {
103+
productId,
104+
};
105+
106+
isLoading.value = true;
107+
108+
const addToCartvariables = { input: productQueryInput };
109+
110+
const { mutate, onDone, onError } = useMutation(ADD_TO_CART_MUTATION, {
111+
addToCartvariables,
112+
});
113+
114+
mutate(addToCartvariables);
115+
116+
onDone(() => {
117+
isLoading.value = false;
118+
});
119+
120+
onError(() => {
121+
isLoading.value = false;
122+
});
123+
};
91124
</script>

components/common/CommonButton.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div v-if="linkTo" class="flex justify-center">
2+
<div v-if="linkTo" :class="{ center: centerButton }">
33
<NuxtLink :to="linkTo" class="button-link">
44
<button
55
:class="{ disabled: isLoading }"
@@ -26,7 +26,7 @@
2626
</button>
2727
</NuxtLink>
2828
</div>
29-
<div v-else class="flex justify-center">
29+
<div v-else :class="{ center: centerButton }">
3030
<button
3131
:class="{ disabled: isLoading }"
3232
@click="$emit('CommonButtonClick')"
@@ -56,7 +56,7 @@
5656
<script setup>
5757
/*
5858
* Usage:
59-
* <CommonButton @common-button-click="functionName" is-loading="true" link-to="/link">Common button</CommonButton>
59+
* <CommonButton @common-button-click="functionName" is-loading="true" center-button="false" link-to="/link">Common button</CommonButton>
6060
*/
6161
6262
defineEmits(["CommonButtonClick"]);
@@ -72,6 +72,7 @@ defineProps({
7272
required: false,
7373
default: "",
7474
},
75+
centerButton: { type: Boolean, required: false, default: false },
7576
});
7677
</script>
7778

@@ -87,4 +88,8 @@ button {
8788
.button-link {
8889
border-bottom: none;
8990
}
91+
92+
.center {
93+
@apply flex justify-center;
94+
}
9095
</style>

0 commit comments

Comments
 (0)