Files
lelong-web-pembida/src/store/modules/cart.js
T
ismailmasseran 8b83144617 Security/register account (#1)
Co-authored-by: ISMAIL MASSERAN <topaz@ISMAILs-Macbook.local>
Co-authored-by: ISMAIL MASSERAN <topaz@Mac.dlinkrouter.local>
Reviewed-on: #1
Co-authored-by: ISMAIL MASSERAN <ismail@koppkb.com>
Co-committed-by: ISMAIL MASSERAN <ismail@koppkb.com>
2026-10-19 13:31:05 +08:00

42 lines
991 B
JavaScript

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;
},
},
});