This commit is contained in:
2026-03-12 12:07:40 +01:00
parent 3943614abb
commit 2d5d6a7487
3 changed files with 178 additions and 101 deletions

85
bo/src/stores/product.ts Normal file
View File

@@ -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<Product[]>([])
const currentProduct = ref<Product | null>(null)
const loading = ref(false)
const error = ref<string | null>(null)
// Fetch all products
async function fetchProducts() {
loading.value = true
error.value = null
try {
const data = await useFetchJson<ProductResponse>('/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,
}
})