228 lines
7.5 KiB
TypeScript
228 lines
7.5 KiB
TypeScript
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, 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}`
|
|
)
|
|
productDescription.value = response.items
|
|
|
|
} catch (e: unknown) {
|
|
error.value = e instanceof Error ? e.message : 'Failed to load product description'
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
|
|
function stripHtml(html: string) {
|
|
const div = document.createElement('div')
|
|
div.innerHTML = html
|
|
return div.textContent || div.innerText || ''
|
|
}
|
|
|
|
async function saveProductDescription(productID?: number, langId?: number) {
|
|
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: stripHtml(productDescription.value?.name || ''),
|
|
description: stripHtml(productDescription.value?.description || ''),
|
|
description_short: stripHtml(productDescription.value?.description_short || ''),
|
|
meta_title: stripHtml(productDescription.value?.meta_title || ''),
|
|
meta_description: stripHtml(productDescription.value?.meta_description || ''),
|
|
available_now: stripHtml(productDescription.value?.available_now || ''),
|
|
available_later: stripHtml(productDescription.value?.available_later || ''),
|
|
usage: stripHtml(productDescription.value?.usage || ''),
|
|
// delivery_in_stock: stripHtml(productDescription.value?.delivery_in_stock || '')
|
|
})
|
|
}
|
|
)
|
|
return data
|
|
} catch (e) {
|
|
console.error(e)
|
|
}
|
|
}
|
|
|
|
return {
|
|
productDescription,
|
|
loading,
|
|
error,
|
|
translateProductDescription,
|
|
getProductDescription,
|
|
saveProductDescription
|
|
|
|
}
|
|
})
|
|
|
|
|
|
|
|
// 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 productDescription = ref()
|
|
// const currentProduct = ref<Product | null>(null)
|
|
// const loading = ref(false)
|
|
// const error = ref<string | null>(null)
|
|
|
|
// async function getProductDescription(langId = 1, 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}`
|
|
// )
|
|
// productDescription.value = response.items
|
|
// console.log(productDescription, 'dfsfsdf');
|
|
|
|
// } catch (e: unknown) {
|
|
// error.value = e instanceof Error ? e.message : 'Failed to load product description'
|
|
// } finally {
|
|
// loading.value = false
|
|
// }
|
|
// }
|
|
// function stripHtml(html: string) {
|
|
// const div = document.createElement('div')
|
|
// div.innerHTML = html
|
|
// return div.textContent || div.innerText || ''
|
|
// }
|
|
// async function saveProductDescription(productID?: number, langId?: number) {
|
|
// 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: stripHtml(productDescription.value?.name || ''),
|
|
// description: stripHtml(productDescription.value?.description || ''),
|
|
// description_short: stripHtml(productDescription.value?.description_short || ''),
|
|
// meta_title: stripHtml(productDescription.value?.meta_title || ''),
|
|
// meta_description: stripHtml(productDescription.value?.meta_description || ''),
|
|
// available_now: stripHtml(productDescription.value?.available_now || ''),
|
|
// available_later: stripHtml(productDescription.value?.available_later || ''),
|
|
// usage: stripHtml(productDescription.value?.usage || '')
|
|
// })
|
|
// }
|
|
// )
|
|
// return data
|
|
// } catch (e) {
|
|
// console.error(e)
|
|
// }
|
|
// }
|
|
|
|
// const defaultLangId = ref(1)
|
|
// async function translateProductDescription(productID: number, fromLangId: number, defaultLangId: number, model: string = 'OpenAI') {
|
|
// loading.value = true
|
|
// error.value = null
|
|
|
|
// try {
|
|
// const response = await useFetchJson<ProductDescription>(`/api/v1/restricted/product-translation/translate-product-description?productID=${productID}&productFromLangID=${fromLangId}&productToLangID=${defaultLangId}&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
|
|
// }
|
|
// }
|
|
|
|
// function clearCurrentProduct() {
|
|
// currentProduct.value = null
|
|
// }
|
|
|
|
// return {
|
|
// productDescription,
|
|
// currentProduct,
|
|
// loading,
|
|
// error,
|
|
// getProductDescription,
|
|
// clearCurrentProduct,
|
|
// saveProductDescription,
|
|
// translateProductDescription,
|
|
// }
|
|
// })
|