Skip to content

Commit d96d320

Browse files
authored
Merge pull request #1290 from w3bdesign/dev
Remove Lodash
2 parents 29a6bc4 + 4d4f912 commit d96d320

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

components/Layout/LayoutCart.vue

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,20 @@
4545
</template>
4646

4747
<script setup>
48-
import _ from "lodash";
49-
import {
50-
ref,
51-
onBeforeMount,
52-
onMounted,
53-
onBeforeUnmount,
54-
computed,
55-
watchEffect,
56-
} from "vue";
48+
import { ref, onBeforeMount, computed, watch } from "vue";
49+
5750
import GET_CART_QUERY from "@/apollo/queries/GET_CART_QUERY.gql";
5851
import { getCookie } from "@/utils/functions";
5952
import { useCart } from "@/store/useCart";
6053
61-
const { data, error, pending, execute } = await useAsyncQuery(GET_CART_QUERY, {
62-
options: { fetchPolicy: "cache-and-network" },
63-
});
64-
6554
const cart = useCart();
6655
const cartChanged = ref(false);
6756
const loading = ref(true);
57+
const remoteError = ref(false);
58+
59+
const { data, error, pending, execute } = useAsyncQuery(GET_CART_QUERY, {
60+
fetchPolicy: "cache-and-network",
61+
});
6862
6963
const cartContents = computed(() => data.value?.cart?.contents?.nodes || []);
7064
const cartLength = computed(() =>
@@ -76,39 +70,37 @@ const subTotal = computed(() =>
7670
0
7771
)
7872
);
79-
const remoteError = ref(false);
8073
81-
const debouncedExecute = _.debounce(() => {
82-
if (process.client && !pending.value && getCookie("woo-session")) {
83-
execute(); // Re-fetch the data
84-
}
85-
}, 2000);
86-
87-
watchEffect(() => {
88-
if (cart.getCartQuantity !== cartLength.value) {
74+
// Watch for changes in the cart quantity and set a flag if it changes
75+
watch(cartLength, (newLength, oldLength) => {
76+
if (newLength !== oldLength) {
8977
cartChanged.value = true;
9078
}
9179
});
9280
81+
// Execute the query initially
9382
onBeforeMount(() => {
9483
execute();
9584
});
9685
97-
onMounted(() => {
98-
loading.value = false;
99-
const intervalId = setInterval(() => {
100-
if (cartChanged.value) {
101-
cartChanged.value = false;
102-
debouncedExecute();
103-
}
104-
}, 5000);
86+
// When the component is mounted, stop the loading state
87+
loading.value = false;
10588
106-
onBeforeUnmount(() => {
107-
clearInterval(intervalId);
108-
});
89+
// Watch for the cartChanged flag and execute the query when it changes
90+
watch(cartChanged, (newValue) => {
91+
if (
92+
newValue &&
93+
process.client &&
94+
!pending.value &&
95+
getCookie("woo-session")
96+
) {
97+
execute();
98+
cartChanged.value = false; // Reset the flag after executing the query
99+
}
109100
});
110101
111-
watchEffect(() => {
112-
remoteError.value = error.value;
102+
// Watch for errors from the Apollo query
103+
watch(error, (newError) => {
104+
remoteError.value = !!newError;
113105
});
114106
</script>

0 commit comments

Comments
 (0)