cart
This commit is contained in:
@ -9,6 +9,7 @@ import type {
|
||||
import type { Product } from "~/types/product";
|
||||
|
||||
export const useProductStore = defineStore("productStore", () => {
|
||||
const { $toast } = useNuxtApp();
|
||||
const productList = ref<Product[]>();
|
||||
const modules = ref();
|
||||
|
||||
@ -61,7 +62,7 @@ export const useProductStore = defineStore("productStore", () => {
|
||||
|
||||
async function incrementCartItem(id: number) {
|
||||
try {
|
||||
await useMyFetch(
|
||||
const res = await useMyFetch<GenericResponse<object>>(
|
||||
`/api/public/user/cart/item/add/${id}/1`,
|
||||
{
|
||||
method: "PUT",
|
||||
@ -74,15 +75,30 @@ export const useProductStore = defineStore("productStore", () => {
|
||||
}
|
||||
);
|
||||
|
||||
getCart();
|
||||
if (res.status === 200) {
|
||||
$toast.success("Item successfully added to your cart.", {
|
||||
autoClose: 5000,
|
||||
dangerouslyHTMLString: true,
|
||||
});
|
||||
getCart();
|
||||
} else {
|
||||
$toast.error("Failed to add item to cart. Please try again.", {
|
||||
autoClose: 5000,
|
||||
dangerouslyHTMLString: true,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("getList error:", error);
|
||||
$toast.error("An unexpected error occurred while updating your cart.", {
|
||||
autoClose: 5000,
|
||||
dangerouslyHTMLString: true,
|
||||
});
|
||||
console.error("incrementCartItem error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
async function decrementCartItem(id: number) {
|
||||
try {
|
||||
await useMyFetch(
|
||||
const res = await useMyFetch<GenericResponse<object>>(
|
||||
`/api/public/user/cart/item/subtract/${id}/1`,
|
||||
{
|
||||
method: "PUT",
|
||||
@ -95,15 +111,30 @@ export const useProductStore = defineStore("productStore", () => {
|
||||
}
|
||||
);
|
||||
|
||||
getCart();
|
||||
if (res.status === 200) {
|
||||
$toast.success("Item successfully removed from your cart.", {
|
||||
autoClose: 5000,
|
||||
dangerouslyHTMLString: true,
|
||||
});
|
||||
getCart();
|
||||
} else {
|
||||
$toast.error("Failed to removed item from cart. Please try again.", {
|
||||
autoClose: 5000,
|
||||
dangerouslyHTMLString: true,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("removeFromCart error:", error);
|
||||
$toast.error("An unexpected error occurred while updating your cart.", {
|
||||
autoClose: 5000,
|
||||
dangerouslyHTMLString: true,
|
||||
});
|
||||
console.error("decrementCartItem error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteCartItem(id: number) {
|
||||
try {
|
||||
await useMyFetch(
|
||||
const res = await useMyFetch<GenericResponse<object>>(
|
||||
`/api/public/user/cart/item/${id}`,
|
||||
{
|
||||
method: "DELETE",
|
||||
@ -116,9 +147,24 @@ export const useProductStore = defineStore("productStore", () => {
|
||||
}
|
||||
);
|
||||
|
||||
getCart();
|
||||
if (res.status === 200) {
|
||||
$toast.success("Item successfully removed from your cart.", {
|
||||
autoClose: 5000,
|
||||
dangerouslyHTMLString: true,
|
||||
});
|
||||
getCart();
|
||||
} else {
|
||||
$toast.error("Failed to removed item from cart. Please try again.", {
|
||||
autoClose: 5000,
|
||||
dangerouslyHTMLString: true,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("removeFromCart error:", error);
|
||||
$toast.error("An unexpected error occurred while updating your cart.", {
|
||||
autoClose: 5000,
|
||||
dangerouslyHTMLString: true,
|
||||
});
|
||||
console.error("deleteCartItem error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user