dynamic banners of khanegi_theme
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { reactive, ref } from 'vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import { loadAppBannersConst, saveAppBannersConst } from '@/api/appBanners'
|
||||
import { emptyAppBanners, type AppBannersValue } from '@/model/appBanners'
|
||||
|
||||
const state = reactive<AppBannersValue>(emptyAppBanners())
|
||||
const constId = ref<number | null>(null)
|
||||
const loaded = ref(false)
|
||||
const loading = ref(false)
|
||||
const saving = ref(false)
|
||||
let loadPromise: Promise<void> | null = null
|
||||
|
||||
async function load(force = false) {
|
||||
if (!force && loaded.value) return
|
||||
if (!force && loadPromise) return loadPromise
|
||||
|
||||
loadPromise = (async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await loadAppBannersConst()
|
||||
constId.value = res.id
|
||||
Object.assign(state, emptyAppBanners(), res.data)
|
||||
loaded.value = true
|
||||
} catch (error) {
|
||||
console.error('خطا در دریافت بنرهای اپ:', error)
|
||||
Object.assign(state, emptyAppBanners())
|
||||
} finally {
|
||||
loading.value = false
|
||||
loadPromise = null
|
||||
}
|
||||
})()
|
||||
|
||||
return loadPromise
|
||||
}
|
||||
|
||||
async function save(successText = 'بنرهای اپ ذخیره شد') {
|
||||
saving.value = true
|
||||
try {
|
||||
const nextId = await saveAppBannersConst(constId.value, {
|
||||
slider: [...state.slider],
|
||||
homeAsset: { ...state.homeAsset },
|
||||
piggyCreate: { ...state.piggyCreate },
|
||||
productsHero1: { ...state.productsHero1 },
|
||||
productsHero2: { ...state.productsHero2 },
|
||||
productsHero3: { ...state.productsHero3 },
|
||||
referral: { ...state.referral },
|
||||
share: { ...state.share },
|
||||
invite: { ...state.invite },
|
||||
})
|
||||
constId.value = nextId
|
||||
loaded.value = true
|
||||
message.success(successText)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
message.error('خطا در ذخیره بنرهای اپ')
|
||||
return false
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
export function useAppBannersAdmin() {
|
||||
void load()
|
||||
|
||||
return {
|
||||
state,
|
||||
constId,
|
||||
loaded,
|
||||
loading,
|
||||
saving,
|
||||
load,
|
||||
save,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user