fix: store
This commit is contained in:
106
bo/src/stores/admin/product.ts
Normal file
106
bo/src/stores/admin/product.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { useFetchJson } from '@/composable/useFetchJson'
|
||||
import type { ProductDescription } from '@/types/product'
|
||||
import { useSettingsStore } from './settings'
|
||||
|
||||
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 loading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
const productDescription = ref()
|
||||
|
||||
async function getProductDescription(langId: number | null, productID: number) {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const response = await useFetchJson<ProductDescription>(
|
||||
`/api/v1/restricted/product-translation/get-product-description?productID=${productID}&productLangID=${langId ? langId : settingStore.shopDefaultLanguage}`
|
||||
)
|
||||
productDescription.value = response.items
|
||||
|
||||
} catch (e: unknown) {
|
||||
error.value = e instanceof Error ? e.message : 'Failed to load product description'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const translat = ref()
|
||||
const settingStore = useSettingsStore()
|
||||
async function translateProductDescription(productID: number, toLangId: number, model: string = 'Google') {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
|
||||
try {
|
||||
const response = await useFetchJson<ProductDescription>(`/api/v1/restricted/product-translation/translate-product-description?productID=${productID}&productFromLangID=${settingStore.shopDefaultLanguage}&productToLangID=${toLangId}&model=${model}`)
|
||||
productDescription.value = response.items
|
||||
return response.items
|
||||
} catch (e: any) {
|
||||
error.value = e?.message || 'Failed to translate product description'
|
||||
console.error('Failed to translate product description:', e)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function saveProductDescription(productID?: number, langId?: number | null) {
|
||||
const id = productID || 1
|
||||
const lang = langId || 1
|
||||
|
||||
try {
|
||||
const data = await useFetchJson(
|
||||
`/api/v1/restricted/product-translation/save-product-description?productID=${id}&productLangID=${lang}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: productDescription.value?.name || '',
|
||||
description: productDescription.value?.description || '',
|
||||
description_short: productDescription.value?.description_short || '',
|
||||
meta_title: productDescription.value?.meta_title || '',
|
||||
meta_description: productDescription.value?.meta_description || '',
|
||||
available_now: productDescription.value?.available_now || '',
|
||||
available_later: productDescription.value?.available_later || '',
|
||||
usage: productDescription.value?.usage || '',
|
||||
})
|
||||
}
|
||||
)
|
||||
return data
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
productDescription,
|
||||
loading,
|
||||
error,
|
||||
translat,
|
||||
translateProductDescription,
|
||||
getProductDescription,
|
||||
saveProductDescription
|
||||
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user