import { defineStore } from "pinia"; import axios from "axios"; import { useAuthenticationUserStore } from "@/store/modules/authentication"; export const useCartStore = defineStore("cart", { state: () => ({ count: 0, }), getters: { hasItems: (state) => state.count > 0, badgeLabel: (state) => (state.count > 99 ? "99+" : String(state.count)), }, actions: { async fetchCount() { const auth = useAuthenticationUserStore(); if (!auth.token || !auth.id) { this.count = 0; return; } try { const response = await axios.get("/itemInCart", { headers: { Authorization: "Bearer " + auth.token, }, params: { user_id: auth.id, }, }); this.count = response.data.success === true ? Number(response.data.cart) || 0 : 0; } catch (error) { console.log(error); } }, clear() { this.count = 0; }, }, });