Files
b2b/bo/src/stores/product.ts
2026-03-23 10:12:55 +01:00

98 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)
async function getProductDescription(langId: number = 1) {
loading.value = true
error.value = null
try {
const response = await useFetchJson(`/api/v1/restricted/product-description/get-product-description?productID=51&productShopID=1&productLangID=${langId}`)
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
}
}
async function saveProductDescription() {
try {
const data = await useFetchJson(
`/api/v1/restricted/product-description/save-product-description?productID=1&productShopID=1&productLangID=1`,
{
method: 'POST',
body: JSON.stringify(
{
description: productDescription.value.description,
description_short: productDescription.value.description_short,
meta_description: productDescription.value.meta_description,
available_now: productDescription.value.available_now,
usage: productDescription.value.usage
})
}
)
return data
} catch (e) {
console.error(e)
}
}
async function translateProductDescription(fromLangId: number, toLangId: number) {
loading.value = true
error.value = null
try {
const response = await useFetchJson(`/api/v1/restricted/product-description/translate-product-description?productID=51&productShopID=1&productFromLangID=${fromLangId}&productToLangID=${toLangId}&model=OpenAI`)
productDescription.value = response
return response
} catch (e: any) {
error.value = e?.message || 'Failed to translate product description'
console.error('Failed to translate product description:', e)
} finally {
loading.value = false
}
}
function clearCurrentProduct() {
currentProduct.value = null
}
return {
productDescription,
currentProduct,
loading,
error,
getProductDescription,
clearCurrentProduct,
saveProductDescription,
translateProductDescription,
}
})