This commit is contained in:
2025-06-24 14:14:40 +02:00
parent a000f966eb
commit 7cc292296b
8 changed files with 171 additions and 115 deletions

View File

@ -1,30 +1,31 @@
import { NuxtErrorBoundary } from "#components";
import { useMyFetch } from "#imports";
import type { GenericResponse, UserCart } from "~/types";
import type { GenericResponse, GenericResponseItems, UserCart } from "~/types";
import type { Product } from "~/types/product";
export const useProductStore = defineStore("productStore", () => {
const productList = ref();
const productList = ref<Product>();
const modules = ref();
async function getList(count: number, categoryId = 1) {
try {
const {data} = await useMyFetch<GenericResponse<object>>(
const { data } = await useMyFetch<GenericResponseItems<Product>>(
`/api/public/products/category/${categoryId}?p=1&elems=${count}`,
{
headers: {
"Content-Type": "application/json",
},
onErrorOccured: (_, status) => { throw new Error(`HTTP error: ${status}`) },
onErrorOccured: async (_, status) => {
// await navigateTo("/error", { replace: true });
throw createError({
statusCode: status,
statusMessage: `HTTP error: ${status}`,
});
},
}
);
console.log(data);
// if (!res.ok) {
// throw new Error(`HTTP error: ${res.status}`);
// }
// const data = await res.json();
// productList.value = data.data.items;
productList.value = data.items;
} catch (error) {
console.error("getList error:", error);
}
@ -55,7 +56,7 @@ console.log(data);
}
}
async function addToCart(product) {
async function addToCart(product: Product) {
try {
const res = await fetch(
`http://127.0.0.1:4000/api/public/user/cart/item/add/${product.id}/1`,
@ -81,12 +82,17 @@ console.log(data);
const cart = ref({} as UserCart);
async function getCart() {
try {
const {data} = await useMyFetch<GenericResponse<UserCart>>(`/api/public/user/cart`, {
headers: {
"Content-Type": "application/json",
},
onErrorOccured: (_, status) => { throw new Error(`HTTP error: ${status}`) },
});
const { data } = await useMyFetch<GenericResponse<UserCart>>(
`/api/public/user/cart`,
{
headers: {
"Content-Type": "application/json",
},
onErrorOccured: (_, status) => {
throw new Error(`HTTP error: ${status}`);
},
}
);
// if (!res.ok) {
// throw new Error(`HTTP error: ${res.status}`);