From 2d5d6a7487a70db38a92f1ffd9e48e7b0a6265f3 Mon Sep 17 00:00:00 2001 From: Yakovenko Valeriia Date: Thu, 12 Mar 2026 12:07:40 +0100 Subject: [PATCH] fix: api --- bo/src/stores/product.ts | 85 ++++++++++++++ bo/src/views/customer/ProductDetailView.vue | 76 ++++++------- bo/src/views/customer/ProductsView.vue | 118 ++++++++++---------- 3 files changed, 178 insertions(+), 101 deletions(-) create mode 100644 bo/src/stores/product.ts diff --git a/bo/src/stores/product.ts b/bo/src/stores/product.ts new file mode 100644 index 0000000..d9816fb --- /dev/null +++ b/bo/src/stores/product.ts @@ -0,0 +1,85 @@ +import { defineStore } from 'pinia' +import { ref, computed } from 'vue' +import { useFetchJson } from '@/composable/useFetchJson' + +export interface Product { + id: number + image: string + name: string + code: string + inStock: boolean + priceFrom: number + priceTo: number + count: number + description?: string + howToUse?: string + productDetails?: string +} + +export interface ProductResponse { + items: Product[] + items_count: number +} + +export const useProductStore = defineStore('product', () => { + const products = ref([]) + const currentProduct = ref(null) + const loading = ref(false) + const error = ref(null) + + // Fetch all products + async function fetchProducts() { + loading.value = true + error.value = null + + try { + const data = await useFetchJson('/api/v1/restricted/product-description', { + method: 'GET', + }) +console.log(data) + const response = (data as any).items || data + products.value = response.items || response || [] + } catch (e: any) { + error.value = e?.message || 'Failed to load products' + console.error('Failed to fetch products:', e) + } finally { + loading.value = false + } + } + + // Fetch single product by ID + async function fetchProductById(id: number) { + loading.value = true + error.value = null + currentProduct.value = null + + try { + const data = await useFetchJson<{ items: Product }>(`/api/v1/restricted/product-description?id=${id}`, { + method: 'GET', + }) + + const response = (data as any).items || data + currentProduct.value = response.items?.[0] || response + } catch (e: any) { + error.value = e?.message || 'Failed to load product' + console.error('Failed to fetch product:', e) + } finally { + loading.value = false + } + } + + // Clear current product + function clearCurrentProduct() { + currentProduct.value = null + } + + return { + products, + currentProduct, + loading, + error, + fetchProducts, + fetchProductById, + clearCurrentProduct, + } +}) diff --git a/bo/src/views/customer/ProductDetailView.vue b/bo/src/views/customer/ProductDetailView.vue index 652e68e..294b12f 100644 --- a/bo/src/views/customer/ProductDetailView.vue +++ b/bo/src/views/customer/ProductDetailView.vue @@ -1,52 +1,38 @@