From 7cc292296bbc356b2bc24821206153ea37b0794b Mon Sep 17 00:00:00 2001 From: Arina Yakovenko Date: Tue, 24 Jun 2025 14:14:40 +0200 Subject: [PATCH] fix shop --- components/section/CategoryTree.vue | 4 +- components/section/ShopMain.vue | 81 +++++++----- composables/useMyFetch.ts | 12 +- error.vue | 11 ++ stores/productStore.ts | 42 +++--- types/index.ts | 123 ++++++++++-------- .../{PlanPrediction.ts => planPrediction.ts} | 0 types/product.ts | 13 ++ 8 files changed, 171 insertions(+), 115 deletions(-) create mode 100644 error.vue rename types/{PlanPrediction.ts => planPrediction.ts} (100%) create mode 100644 types/product.ts diff --git a/components/section/CategoryTree.vue b/components/section/CategoryTree.vue index d07d8a1..69e8087 100644 --- a/components/section/CategoryTree.vue +++ b/components/section/CategoryTree.vue @@ -5,7 +5,7 @@

{{ mainCategoryName }}

- @@ -40,7 +40,7 @@ const toggle = () => { }; // Computed properties -const hasMainCategory = computed(() => props.data.length > 0 && props.data[0].langs && props.data[0].langs.length > 0); +const hasMainCategory = computed(() => props.data && props.data.length > 0 && props.data[0].langs && props.data[0].langs.length > 0); const mainCategoryName = computed(() => hasMainCategory.value ? props.data[0].langs[0].Name : ''); const subcategories = computed(() => hasMainCategory.value && props.data[0].children ? props.data[0].children : []); \ No newline at end of file diff --git a/components/section/ShopMain.vue b/components/section/ShopMain.vue index a3939eb..038cc73 100644 --- a/components/section/ShopMain.vue +++ b/components/section/ShopMain.vue @@ -17,7 +17,7 @@
-
+
@@ -34,7 +34,8 @@
- {{ item.feature }}
-
+
@@ -167,7 +168,7 @@ diff --git a/stores/productStore.ts b/stores/productStore.ts index b8c5e9a..6b9df6f 100644 --- a/stores/productStore.ts +++ b/stores/productStore.ts @@ -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(); const modules = ref(); async function getList(count: number, categoryId = 1) { try { - const {data} = await useMyFetch>( + const { data } = await useMyFetch>( `/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>(`/api/public/user/cart`, { - headers: { - "Content-Type": "application/json", - }, - onErrorOccured: (_, status) => { throw new Error(`HTTP error: ${status}`) }, - }); + const { data } = await useMyFetch>( + `/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}`); diff --git a/types/index.ts b/types/index.ts index 67e8c37..f834108 100644 --- a/types/index.ts +++ b/types/index.ts @@ -44,7 +44,7 @@ export interface PBFooterItem { }>; data: Array<{ title: string; - items: Array + items: Array; }>; company_info: Array<{ data: string; @@ -71,7 +71,6 @@ export interface PBPageItem { section_name: string; } - export interface SectionLangData { title: string; description: string; @@ -111,13 +110,13 @@ export type Countries = { currency_iso_code: string; iso_code: string; name: string; -} +}; export type PartnersList = { country_iso: string; country_name: string; total: number; -} +}; export type Currencies = { iso_code: string; @@ -128,78 +127,88 @@ export type Currencies = { sign: string; active: boolean; suffix: boolean; -} +}; export type FeatureValue = { - parent: number, - products_with_value: number, - value: string, - value_id: number, -} + parent: number; + products_with_value: number; + value: string; + value_id: number; +}; export type Feature = { - feature: string, - feature_id: number, - feature_values: FeatureValue[], - products_with_feature: number, -} + feature: string; + feature_id: number; + feature_values: FeatureValue[]; + products_with_feature: number; +}; export type ProductType = { - applied_tax_rate: number, - cover_picture_uuid: string, - description: string, - formatted_price: string, - id: number, - in_stock: number, - is_sale_active: boolean, - link_rewrite: string, - name: string, - price: number, - tax_name: string, - cart_item_id?: number - product_id?: number -} - + applied_tax_rate: number; + cover_picture_uuid: string; + description: string; + formatted_price: string; + id: number; + in_stock: number; + is_sale_active: boolean; + link_rewrite: string; + name: string; + price: number; + tax_name: string; + cart_item_id?: number; + product_id?: number; +}; export interface Country { - iso_code: string - currency_iso_code: string - call_prefix: string - need_postcode: boolean - postcode_format: string - is_default: boolean - active: boolean - name: string + iso_code: string; + currency_iso_code: string; + call_prefix: string; + need_postcode: boolean; + postcode_format: string; + is_default: boolean; + active: boolean; + name: string; } export interface Currency { - iso_code: string - name: string - UpdatedAt: string - iso_code_num: number - precision: number - sign: string - active: boolean - suffix: boolean + iso_code: string; + name: string; + UpdatedAt: string; + iso_code_num: number; + precision: number; + sign: string; + active: boolean; + suffix: boolean; } export interface UserCart { - id: number - checkout_in_progress: boolean - total_value: number - currency_iso: string + id: number; + checkout_in_progress: boolean; + total_value: number; + currency_iso: string; } export interface GenericResponse { - data: Data - message?: string - status: number + data: Data; + message?: string; + status: number; } export interface GenericResponseItems { - data: { items: Data , items_count: number } - message?: string - status: number + data: { items: Data; items_count: number }; + message?: string; + status: number; } -export type { InvestmentPiece, PlanPrediction, PeriodToFirstPiece} from './PlanPrediction' \ No newline at end of file +export interface GenericResponseChildren { + data: { children: Data; items_count: number }; + message?: string; + status: number; +} + +export type { + InvestmentPiece, + PlanPrediction, + PeriodToFirstPiece, +} from "./planPrediction"; +export type { Product } from "./product"; diff --git a/types/PlanPrediction.ts b/types/planPrediction.ts similarity index 100% rename from types/PlanPrediction.ts rename to types/planPrediction.ts diff --git a/types/product.ts b/types/product.ts new file mode 100644 index 0000000..2c362be --- /dev/null +++ b/types/product.ts @@ -0,0 +1,13 @@ +export interface Product { + id: number; + link_rewrite: string; + name: string; + cover_picture_uuid: string; + description: string; + price: number; + formatted_price: string; + in_stock: number; + is_sale_active: boolean; + applied_tax_rate: number; + tax_name: string; +}