108 lines
2.8 KiB
TypeScript
108 lines
2.8 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref } 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 productDescription = ref()
|
|
const currentProduct = ref<Product | null>(null)
|
|
const loading = ref(false)
|
|
const error = ref<string | null>(null)
|
|
|
|
// Fetch all products
|
|
async function getProductDescription() {
|
|
loading.value = true
|
|
error.value = null
|
|
|
|
try {
|
|
const response = await useFetchJson('/api/v1/restricted/product-description/get-product-description?productID=51&productShopID=1&productLangID=1')
|
|
productDescription.value = response
|
|
} catch (e: any) {
|
|
error.value = e?.message || 'Failed to load product description'
|
|
console.error('Failed to fetch product description:', 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}`)
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
|
|
async function saveProductDescription(description: string) {
|
|
try {
|
|
const data = await useFetchJson(
|
|
`/api/v1/restricted/product-description/save-product-description?productID=1&productShopID=1&productLangID=1`,
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
description: description,
|
|
description_short: "<p>Test short</p>",
|
|
meta_description: "This is a test",
|
|
meta_title: "...",
|
|
name: "...",
|
|
available_now: "Test",
|
|
available_later: "...",
|
|
usage: "<p>test</p>"
|
|
})
|
|
}
|
|
)
|
|
return data
|
|
} catch (e) {
|
|
console.error(e)
|
|
}
|
|
}
|
|
|
|
// Clear current product
|
|
function clearCurrentProduct() {
|
|
currentProduct.value = null
|
|
}
|
|
|
|
return {
|
|
productDescription,
|
|
currentProduct,
|
|
loading,
|
|
error,
|
|
getProductDescription,
|
|
fetchProductById,
|
|
clearCurrentProduct,
|
|
saveProductDescription
|
|
}
|
|
})
|