fix: save product descriptions
This commit is contained in:
@@ -1,18 +1,25 @@
|
|||||||
<template>
|
<template>
|
||||||
<component :is="Default || 'div'">
|
<component :is="Default || 'div'">
|
||||||
|
|
||||||
<div class="container my-10 mx-auto ">
|
<div class="container my-10 mx-auto ">
|
||||||
<div
|
<div
|
||||||
class="flex items-end justify-between gap-4 mb-6 bg-(--second-light) dark:bg-(--main-dark) border border-(--border-light) dark:border-(--border-dark) p-4 rounded-md">
|
class="flex items-end justify-between gap-4 mb-6 bg-(--second-light) dark:bg-(--main-dark) border border-(--border-light) dark:border-(--border-dark) p-4 rounded-md">
|
||||||
<div class="flex items-end gap-3">
|
<div class="flex items-center gap-3" v-if="!isTranslations">
|
||||||
<USelect v-model="selectedLanguage" :items="availableLangs" variant="outline" class="w-40!" valueKey="id">
|
<p class="text-red-500 text-md">Translate from Polish to</p>
|
||||||
|
<USelect v-model="toLangId" :items="availableLangs" variant="outline" class="w-40!" valueKey="id">
|
||||||
<template #default="{ modelValue }">
|
<template #default="{ modelValue }">
|
||||||
<div class="flex items-center gap-2">
|
<div class="flex items-center gap-2">
|
||||||
<span class="text-md">{{availableLangs.find(x => x.id == modelValue)?.flag}}</span>
|
<span v-if="!modelValue" class="text-gray-400">
|
||||||
<span class="font-medium dark:text-white text-black">{{availableLangs.find(x => x.id ==
|
Select language
|
||||||
modelValue)?.name}}</span>
|
</span>
|
||||||
|
<template v-else>
|
||||||
|
<span class="text-md">{{availableLangs.find(x => x.id === modelValue)?.flag}}</span>
|
||||||
|
<span class="font-medium dark:text-white text-black">
|
||||||
|
{{availableLangs.find(x => x.id === modelValue)?.name}}
|
||||||
|
</span>
|
||||||
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<template #item-leading="{ item }">
|
<template #item-leading="{ item }">
|
||||||
<div class="flex items-center rounded-md cursor-pointer transition-colors">
|
<div class="flex items-center rounded-md cursor-pointer transition-colors">
|
||||||
<span class="text-md">{{ item.flag }}</span>
|
<span class="text-md">{{ item.flag }}</span>
|
||||||
@@ -20,11 +27,30 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</USelect>
|
</USelect>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<UButton @click="fetchForLanguage(toLangId)" color="primary"
|
||||||
|
class="text-white bg-(--accent-blue-light) dark:bg-(--accent-blue-dark) px-12!">
|
||||||
|
Show product
|
||||||
|
</UButton>
|
||||||
<UButton @click="translateToSelectedLanguage" color="primary" :loading="translating"
|
<UButton @click="translateToSelectedLanguage" color="primary" :loading="translating"
|
||||||
|
v-if="isTranslations === false"
|
||||||
class="text-white bg-(--accent-blue-light) dark:bg-(--accent-blue-dark) px-12!">
|
class="text-white bg-(--accent-blue-light) dark:bg-(--accent-blue-dark) px-12!">
|
||||||
Translate
|
Translate
|
||||||
</UButton>
|
</UButton>
|
||||||
|
<div v-else class="flex gap-3 items-end">
|
||||||
|
<UButton @click="() => {
|
||||||
|
toLangId = settingStore.shopDefaultLanguage
|
||||||
|
isTranslations = false
|
||||||
|
}" color="primary" class="text-white bg-(--accent-blue-light) dark:bg-(--accent-blue-dark) px-12!">
|
||||||
|
Cancel
|
||||||
|
</UButton>
|
||||||
|
<UButton color="primary" @click="saveDescription"
|
||||||
|
class="text-white bg-(--accent-blue-light) dark:bg-(--accent-blue-dark) px-12!">
|
||||||
|
Save
|
||||||
|
</UButton>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-if="translating" class="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
|
<div v-if="translating" class="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
|
||||||
@@ -137,7 +163,6 @@ import Default from '@/layouts/default.vue';
|
|||||||
import { langs } from '@/router/langs';
|
import { langs } from '@/router/langs';
|
||||||
import { useProductStore } from '@/stores/product';
|
import { useProductStore } from '@/stores/product';
|
||||||
import { useSettingsStore } from '@/stores/settings';
|
import { useSettingsStore } from '@/stores/settings';
|
||||||
import type { Language } from '@/types';
|
|
||||||
import { watch } from 'vue';
|
import { watch } from 'vue';
|
||||||
import { onMounted, ref } from 'vue';
|
import { onMounted, ref } from 'vue';
|
||||||
import { computed } from 'vue';
|
import { computed } from 'vue';
|
||||||
@@ -146,15 +171,13 @@ import { useRoute } from 'vue-router';
|
|||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const settingStore = useSettingsStore()
|
const settingStore = useSettingsStore()
|
||||||
const productStore = useProductStore()
|
const productStore = useProductStore()
|
||||||
|
const isTranslations = ref(false)
|
||||||
|
|
||||||
|
|
||||||
const selectedLanguage = ref(settingStore.shopDefaultLanguage)
|
|
||||||
const availableLangs = computed(() => langs)
|
|
||||||
const productID = ref<number>(0)
|
|
||||||
const toLangId = ref(settingStore.shopDefaultLanguage)
|
const toLangId = ref(settingStore.shopDefaultLanguage)
|
||||||
const defaultLangId = ref(settingStore.shopDefaultLanguage)
|
|
||||||
const translating = ref(false)
|
|
||||||
|
|
||||||
|
const availableLangs = computed(() => langs.filter(item => item.id !== settingStore.shopDefaultLanguage))
|
||||||
|
const productID = ref<number>(0)
|
||||||
|
const translating = ref(false)
|
||||||
|
|
||||||
const activeTab = ref('description')
|
const activeTab = ref('description')
|
||||||
const usageRef = ref<HTMLElement | null>(null)
|
const usageRef = ref<HTMLElement | null>(null)
|
||||||
@@ -163,50 +186,39 @@ const isEditing = ref(false)
|
|||||||
const usageEdit = useEditable(usageRef)
|
const usageEdit = useEditable(usageRef)
|
||||||
const descriptionRef = ref<HTMLElement | null>(null)
|
const descriptionRef = ref<HTMLElement | null>(null)
|
||||||
const descriptionEdit = useEditable(descriptionRef)
|
const descriptionEdit = useEditable(descriptionRef)
|
||||||
|
|
||||||
|
|
||||||
const originalDescription = ref('')
|
const originalDescription = ref('')
|
||||||
|
|
||||||
// Коли користувач обирає мову, ця функція бере текст продукту з основної мови і перекладає його на вибрану мову. Поки переклад йде – показує спінер. Коли переклад готовий, активна мова оновлюється, а спінер зникає.
|
|
||||||
|
|
||||||
const translateToSelectedLanguage = async () => {
|
const translateToSelectedLanguage = async () => {
|
||||||
const targetLang = langs.find((l: Language) => l.id === selectedLanguage.value)
|
if (toLangId.value && productID.value) {
|
||||||
if (targetLang && toLangId.value && productID.value) {
|
|
||||||
translating.value = true
|
translating.value = true
|
||||||
try {
|
try {
|
||||||
await productStore.translateProductDescription(productID.value, toLangId.value, defaultLangId.value)
|
await productStore.translateProductDescription(productID.value, toLangId.value)
|
||||||
toLangId.value = targetLang.id
|
|
||||||
} finally {
|
} finally {
|
||||||
translating.value = false
|
translating.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if (productStore.productDescription) {
|
||||||
|
isTranslations.value = true
|
||||||
|
|
||||||
// знайти мову → підвантажити опис → запам’ятати, що це “базова” мова для перекладу.
|
|
||||||
const fetchForLanguage = async (langCode: number) => {
|
|
||||||
const lang = langs.find((l: Language) => l.id === langCode)
|
|
||||||
if (lang && productID.value) {
|
|
||||||
await productStore.getProductDescription(lang.id, productID.value)
|
|
||||||
toLangId.value = lang.id
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(selectedLanguage, async (newLang: number) => {
|
const fetchForLanguage = async (langId: number) => {
|
||||||
if (productID.value) {
|
if (langId && productID.value) {
|
||||||
await fetchForLanguage(newLang)
|
await productStore.getProductDescription(langId, productID.value)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
const id = route.params.product_id
|
const id = route.params.product_id
|
||||||
if (id) {
|
if (id) {
|
||||||
productID.value = Number(id)
|
productID.value = Number(id)
|
||||||
await fetchForLanguage(selectedLanguage.value)
|
await fetchForLanguage(toLangId.value)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
// text edit
|
||||||
const enableEdit = () => {
|
const enableEdit = () => {
|
||||||
if (usageRef.value) {
|
if (usageRef.value) {
|
||||||
originalUsage.value = usageRef.value.innerHTML
|
originalUsage.value = usageRef.value.innerHTML
|
||||||
@@ -249,6 +261,8 @@ const saveDescription = async () => {
|
|||||||
descriptionEdit.disableEdit()
|
descriptionEdit.disableEdit()
|
||||||
|
|
||||||
await productStore.saveProductDescription(productID.value, toLangId.value)
|
await productStore.saveProductDescription(productID.value, toLangId.value)
|
||||||
|
toLangId.value = settingStore.shopDefaultLanguage
|
||||||
|
isTranslations.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
const cancelDescriptionEdit = () => {
|
const cancelDescriptionEdit = () => {
|
||||||
@@ -257,6 +271,7 @@ const cancelDescriptionEdit = () => {
|
|||||||
}
|
}
|
||||||
descriptionEdit.disableEdit()
|
descriptionEdit.disableEdit()
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
@@ -328,10 +343,6 @@ const cancelDescriptionEdit = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div v-if="productStore.productDescription" class="mt-16">
|
<div v-if="productStore.productDescription" class="mt-16">
|
||||||
<div class="flex gap-4 my-6">
|
<div class="flex gap-4 my-6">
|
||||||
<UButton @click="activeTab = 'description'"
|
<UButton @click="activeTab = 'description'"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { createRouter, createWebHistory } from 'vue-router'
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
import { currentLang, langs } from './langs'
|
import { currentLang, langs, switchLocalization } from './langs'
|
||||||
import { getSettings } from './settings'
|
import { getSettings } from './settings'
|
||||||
import { useAuthStore } from '@/stores/auth'
|
import { useAuthStore } from '@/stores/auth'
|
||||||
import { getRoutes } from './menu'
|
import { getRoutes } from './menu'
|
||||||
@@ -8,6 +8,7 @@ function isAuthenticated(): boolean {
|
|||||||
if (typeof document === 'undefined') return false
|
if (typeof document === 'undefined') return false
|
||||||
return document.cookie.split('; ').some((c) => c === 'is_authenticated=1')
|
return document.cookie.split('; ').some((c) => c === 'is_authenticated=1')
|
||||||
}
|
}
|
||||||
|
await switchLocalization()
|
||||||
await getSettings()
|
await getSettings()
|
||||||
|
|
||||||
const routes = await getRoutes()
|
const routes = await getRoutes()
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ export const useProductStore = defineStore('product', () => {
|
|||||||
`/api/v1/restricted/product-translation/get-product-description?productID=${productID}&productLangID=${langId}`
|
`/api/v1/restricted/product-translation/get-product-description?productID=${productID}&productLangID=${langId}`
|
||||||
)
|
)
|
||||||
productDescription.value = response.items
|
productDescription.value = response.items
|
||||||
console.log(productDescription, 'dfsfsdf');
|
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
error.value = e instanceof Error ? e.message : 'Failed to load product description'
|
error.value = e instanceof Error ? e.message : 'Failed to load product description'
|
||||||
} finally {
|
} finally {
|
||||||
@@ -45,12 +45,13 @@ export const useProductStore = defineStore('product', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function translateProductDescription(productID: number, toLangId: number, defaultLangId: number, model: string = 'OpenAI') {
|
const settingStore = useSettingsStore()
|
||||||
|
async function translateProductDescription(productID: number, toLangId: number, model: string = 'Google') {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await useFetchJson<ProductDescription>(`/api/v1/restricted/product-translation/translate-product-description?productID=${productID}&productFromLangID=${defaultLangId}&productToLangID=${toLangId}&model=${model}`)
|
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
|
productDescription.value = response.items
|
||||||
return response.items
|
return response.items
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
@@ -87,7 +88,8 @@ export const useProductStore = defineStore('product', () => {
|
|||||||
meta_description: stripHtml(productDescription.value?.meta_description || ''),
|
meta_description: stripHtml(productDescription.value?.meta_description || ''),
|
||||||
available_now: stripHtml(productDescription.value?.available_now || ''),
|
available_now: stripHtml(productDescription.value?.available_now || ''),
|
||||||
available_later: stripHtml(productDescription.value?.available_later || ''),
|
available_later: stripHtml(productDescription.value?.available_later || ''),
|
||||||
usage: stripHtml(productDescription.value?.usage || '')
|
usage: stripHtml(productDescription.value?.usage || ''),
|
||||||
|
// delivery_in_stock: stripHtml(productDescription.value?.delivery_in_stock || '')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user