77 lines
1.7 KiB
TypeScript
77 lines
1.7 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
|
|
export const useLoadingStore = defineStore('loadingStore', () => {
|
|
// 글로벌 로딩 표기
|
|
const fullLoading = ref(true)
|
|
const hasApiCallStarted = ref(false)
|
|
const isPAssApiLoading = ref(false)
|
|
// 컴포넌트별 로딩 표기 - Map 대신 일반 객체 사용
|
|
const localLoadings = ref<Record<string, { active: boolean }>>({})
|
|
|
|
/**
|
|
* 로딩 상태 초기화
|
|
*/
|
|
const initializeStore = () => {
|
|
localLoadings.value = {}
|
|
hasApiCallStarted.value = false
|
|
isPAssApiLoading.value = false
|
|
}
|
|
|
|
/**
|
|
* Full 로딩
|
|
*/
|
|
const startFullLoading = () => {
|
|
initializeStore()
|
|
fullLoading.value = true
|
|
}
|
|
const startApiLoading = () => {
|
|
hasApiCallStarted.value = true
|
|
isPAssApiLoading.value = false
|
|
}
|
|
|
|
const stopFullLoading = () => {
|
|
if (!hasApiCallStarted.value || isPAssApiLoading.value) {
|
|
fullLoading.value = false
|
|
}
|
|
}
|
|
const finishApiLoading = () => {
|
|
setTimeout(() => {
|
|
hasApiCallStarted.value = false
|
|
isPAssApiLoading.value = true
|
|
}, 300)
|
|
}
|
|
|
|
/**
|
|
* Local 로딩
|
|
*/
|
|
const startLocalLoading = (localId: string) => {
|
|
localLoadings.value[localId] = { active: true }
|
|
}
|
|
|
|
const stopLocalLoading = (localId: string) => {
|
|
if (localLoadings.value[localId]) {
|
|
localLoadings.value[localId].active = false
|
|
}
|
|
}
|
|
|
|
const isLocalLoading = (localId: string) => {
|
|
return !!localLoadings.value[localId]?.active
|
|
}
|
|
|
|
return {
|
|
fullLoading,
|
|
localLoadings,
|
|
hasApiCallStarted,
|
|
isPAssApiLoading,
|
|
|
|
startApiLoading,
|
|
finishApiLoading,
|
|
initializeStore,
|
|
startFullLoading,
|
|
stopFullLoading,
|
|
startLocalLoading,
|
|
stopLocalLoading,
|
|
isLocalLoading,
|
|
}
|
|
})
|