2022-07-21 00:42:36 +02:00
|
|
|
import type { ActionContext } from 'vuex'
|
2022-09-21 16:23:57 +02:00
|
|
|
import type { StoreDefinition } from 'pinia'
|
2021-01-09 15:24:06 +01:00
|
|
|
import {LOADING, LOADING_MODULE} from './mutation-types'
|
2022-07-21 00:42:36 +02:00
|
|
|
import type { RootStoreState } from './types'
|
2020-05-09 22:08:18 +02:00
|
|
|
|
2021-01-09 15:24:06 +01:00
|
|
|
/**
|
|
|
|
* This helper sets the loading state with a 100ms delay to avoid flickering.
|
|
|
|
*
|
|
|
|
* @param {*} context The vuex module context.
|
|
|
|
* @param {null|String} module The module that is loading. This parameter allows components to listen for specific parts of the application loading.
|
|
|
|
* @param {null|function} loadFunc If not null, this function will be executed instead of the default setting loading.
|
|
|
|
*/
|
2022-07-21 00:42:36 +02:00
|
|
|
export function setLoading<State>(
|
|
|
|
context : ActionContext<State, RootStoreState>,
|
|
|
|
module : string | null = null,
|
|
|
|
loadFunc : (() => void) | null = null,
|
|
|
|
) {
|
2020-05-09 22:08:18 +02:00
|
|
|
const timeout = setTimeout(() => {
|
2020-12-23 21:26:43 +01:00
|
|
|
if (loadFunc === null) {
|
|
|
|
context.commit(LOADING, true, {root: true})
|
2021-01-09 15:24:06 +01:00
|
|
|
context.commit(LOADING_MODULE, module, {root: true})
|
2020-12-23 21:26:43 +01:00
|
|
|
} else {
|
|
|
|
loadFunc(true)
|
|
|
|
}
|
2020-05-09 22:08:18 +02:00
|
|
|
}, 100)
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timeout)
|
2020-12-23 21:26:43 +01:00
|
|
|
if (loadFunc === null) {
|
|
|
|
context.commit(LOADING, false, {root: true})
|
2021-01-09 15:24:06 +01:00
|
|
|
context.commit(LOADING_MODULE, null, {root: true})
|
2020-12-23 21:26:43 +01:00
|
|
|
} else {
|
|
|
|
loadFunc(false)
|
|
|
|
}
|
2020-05-09 22:08:18 +02:00
|
|
|
}
|
2022-09-21 16:23:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const setLoadingPinia = (store: StoreDefinition, loadFunc : ((isLoading: boolean) => void) | null = null) => {
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
if (loadFunc === null) {
|
|
|
|
store.isLoading = true
|
|
|
|
} else {
|
|
|
|
loadFunc(true)
|
|
|
|
}
|
|
|
|
}, 100)
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timeout)
|
|
|
|
if (loadFunc === null) {
|
|
|
|
store.isLoading = false
|
|
|
|
} else {
|
|
|
|
loadFunc(false)
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 22:08:18 +02:00
|
|
|
}
|