fix: store
This commit is contained in:
40
bo/src/stores/admin/category.ts
Normal file
40
bo/src/stores/admin/category.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
|
||||
|
||||
const products = [
|
||||
{name: "product name", price: 342432},
|
||||
{name: "product name", price: 342432},
|
||||
{name: "product name", price: 342432},
|
||||
{name: "product name", price: 342432},
|
||||
{name: "product name", price: 342432},
|
||||
]
|
||||
|
||||
// type CategoryProducts = {}
|
||||
|
||||
export const useCategoryStore = defineStore('category', () => {
|
||||
const idCategory = ref(0)
|
||||
const categoryProducts = ref(products)
|
||||
|
||||
|
||||
function setCategoryID(id: number) {
|
||||
idCategory.value = id
|
||||
}
|
||||
|
||||
async function getCategoryProducts() {
|
||||
return new Promise<typeof products>((resolve) => {
|
||||
setTimeout(() => {
|
||||
// console.log('Fetching products from category id: ', idCategory.value);
|
||||
resolve(categoryProducts.value)
|
||||
}, 2000 * Math.random())
|
||||
})
|
||||
}
|
||||
return {
|
||||
idCategory,
|
||||
getCategoryProducts,
|
||||
setCategoryID
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
46
bo/src/stores/admin/customer.ts
Normal file
46
bo/src/stores/admin/customer.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import type { Address } from './user/address'
|
||||
|
||||
export interface CustomerData {
|
||||
companyName: string
|
||||
companyEmail: string
|
||||
companyAddress: string
|
||||
regon: string
|
||||
nip: string
|
||||
vat: string
|
||||
billingAddressId: number | null
|
||||
companyAddressId: number | null
|
||||
}
|
||||
|
||||
export const useCustomerStore = defineStore('customer', () => {
|
||||
const customer = ref<CustomerData | null>(null)
|
||||
const loading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
const hasAccount = computed(() => customer.value !== null)
|
||||
|
||||
function setCustomer(data: CustomerData) {
|
||||
customer.value = data
|
||||
}
|
||||
|
||||
function clearCustomer() {
|
||||
customer.value = null
|
||||
}
|
||||
|
||||
function updateCustomer(data: Partial<CustomerData>) {
|
||||
if (customer.value) {
|
||||
customer.value = { ...customer.value, ...data }
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
customer,
|
||||
loading,
|
||||
error,
|
||||
hasAccount,
|
||||
setCustomer,
|
||||
clearCustomer,
|
||||
updateCustomer
|
||||
}
|
||||
})
|
||||
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
|
||||
|
||||
}
|
||||
})
|
||||
21
bo/src/stores/admin/settings.ts
Normal file
21
bo/src/stores/admin/settings.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { useFetchJson } from '@/composable/useFetchJson'
|
||||
import type { Resp } from '@/types'
|
||||
import type { Settings } from '@/types/settings'
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
export const useSettingsStore = defineStore('settings', () => {
|
||||
const settings = ref<Settings | null>(null)
|
||||
const loaded = ref(false)
|
||||
const shopDefaultLanguage = computed(() => settings.value?.app?.shop_default_language ?? 1)
|
||||
|
||||
async function getSettings(): Promise<Settings | null> {
|
||||
if (loaded.value && settings.value) return settings.value
|
||||
const resp = await useFetchJson<Settings>('/api/v1/settings')
|
||||
settings.value = resp.items
|
||||
loaded.value = true
|
||||
return resp.items
|
||||
}
|
||||
|
||||
return { settings, loaded, shopDefaultLanguage, getSettings }
|
||||
})
|
||||
47
bo/src/stores/admin/theme.ts
Normal file
47
bo/src/stores/admin/theme.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
export const useThemeStore = defineStore('theme', () => {
|
||||
const vueuseColorScheme = ref(localStorage.getItem('vueuse-color-scheme'))
|
||||
const themeIcon = computed(() => {
|
||||
switch (true) {
|
||||
case vueuseColorScheme.value == 'light':
|
||||
return 'i-heroicons-sun'
|
||||
case vueuseColorScheme.value == 'dark':
|
||||
return 'i-heroicons-moon'
|
||||
case vueuseColorScheme.value == 'auto':
|
||||
return 'i-heroicons-computer-desktop'
|
||||
}
|
||||
})
|
||||
|
||||
function setTheme() {
|
||||
switch (true) {
|
||||
case localStorage.getItem('vueuse-color-scheme') == 'dark':
|
||||
vueuseColorScheme.value = 'light'
|
||||
localStorage.setItem('vueuse-color-scheme', 'light')
|
||||
document.documentElement.classList.toggle('dark', false)
|
||||
break
|
||||
case localStorage.getItem('vueuse-color-scheme') == 'light':
|
||||
vueuseColorScheme.value = 'dark'
|
||||
localStorage.setItem('vueuse-color-scheme', 'dark')
|
||||
document.documentElement.classList.toggle('dark', true)
|
||||
break
|
||||
case localStorage.getItem('vueuse-color-scheme') == 'auto':
|
||||
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||
vueuseColorScheme.value = 'light'
|
||||
localStorage.setItem('vueuse-color-scheme', 'light')
|
||||
document.documentElement.classList.toggle('dark', false)
|
||||
} else {
|
||||
vueuseColorScheme.value = 'light'
|
||||
localStorage.setItem('vueuse-color-scheme', 'light')
|
||||
document.documentElement.classList.toggle('dark', false)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return {
|
||||
vueuseColorScheme,
|
||||
setTheme,
|
||||
themeIcon,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user