cart pop-up
This commit is contained in:
@ -1,15 +1,20 @@
|
||||
import { NuxtErrorBoundary } from "#components";
|
||||
import { useMyFetch } from "#imports";
|
||||
import type { GenericResponse, GenericResponseItems, UserCart } from "~/types";
|
||||
import type {
|
||||
GenericResponse,
|
||||
GenericResponseChildren,
|
||||
GenericResponseItems,
|
||||
UserCart,
|
||||
} from "~/types";
|
||||
import type { Product } from "~/types/product";
|
||||
|
||||
export const useProductStore = defineStore("productStore", () => {
|
||||
const productList = ref<Product>();
|
||||
const productList = ref<Product[]>();
|
||||
const modules = ref();
|
||||
|
||||
async function getList(count: number, categoryId = 1) {
|
||||
try {
|
||||
const { data } = await useMyFetch<GenericResponseItems<Product>>(
|
||||
const { data } = await useMyFetch<GenericResponseItems<[]>>(
|
||||
`/api/public/products/category/${categoryId}?p=1&elems=${count}`,
|
||||
{
|
||||
headers: {
|
||||
@ -33,20 +38,18 @@ export const useProductStore = defineStore("productStore", () => {
|
||||
|
||||
async function getModules() {
|
||||
try {
|
||||
const res = await fetch(
|
||||
`http://127.0.0.1:4000/api/public/module/e_shop`,
|
||||
const { data } = await useMyFetch<GenericResponseChildren<[]>>(
|
||||
`/api/public/module/e_shop`,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
onErrorOccured: (_, status) => {
|
||||
throw new Error(`HTTP error: ${status}`);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP error: ${res.status}`);
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
modules.value = data.children.find(
|
||||
(item: { id: number; name: string }) =>
|
||||
item.name === "currency_rates_bar"
|
||||
@ -58,22 +61,18 @@ export const useProductStore = defineStore("productStore", () => {
|
||||
|
||||
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`,
|
||||
const { data } = await useMyFetch<GenericResponse<UserCart>>(
|
||||
`/api/public/user/cart/item/add/${product.id}/1`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
onErrorOccured: (_, status) => {
|
||||
throw new Error(`HTTP error: ${status}`);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP error: ${res.status}`);
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
console.log(data);
|
||||
} catch (error) {
|
||||
console.error("getList error:", error);
|
||||
}
|
||||
|
Reference in New Issue
Block a user