feat: base store with composition api (#2601)
Co-authored-by: Dominik Pschenitschni <mail@celement.de> Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/2601 Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de> Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
parent
15ef86d597
commit
b4f4fd45a4
@ -1,3 +1,4 @@
|
|||||||
|
import {readonly, ref} from 'vue'
|
||||||
import {defineStore, acceptHMRUpdate} from 'pinia'
|
import {defineStore, acceptHMRUpdate} from 'pinia'
|
||||||
|
|
||||||
import {getBlobFromBlurHash} from '@/helpers/getBlobFromBlurHash'
|
import {getBlobFromBlurHash} from '@/helpers/getBlobFromBlurHash'
|
||||||
@ -9,140 +10,155 @@ import {checkAndSetApiUrl} from '@/helpers/checkAndSetApiUrl'
|
|||||||
import {useAuthStore} from '@/stores/auth'
|
import {useAuthStore} from '@/stores/auth'
|
||||||
import type {IList} from '@/modelTypes/IList'
|
import type {IList} from '@/modelTypes/IList'
|
||||||
|
|
||||||
export interface RootStoreState {
|
export const useBaseStore = defineStore('base', () => {
|
||||||
loading: boolean,
|
const loading = ref(false)
|
||||||
ready: boolean,
|
const ready = ref(false)
|
||||||
|
|
||||||
currentList: IList | null,
|
// This is used to highlight the current list in menu for all list related views
|
||||||
background: string,
|
const currentList = ref<IList | null>(new ListModel({
|
||||||
blurHash: string,
|
id: 0,
|
||||||
|
isArchived: false,
|
||||||
|
}))
|
||||||
|
const background = ref('')
|
||||||
|
const blurHash = ref('')
|
||||||
|
|
||||||
hasTasks: boolean,
|
const hasTasks = ref(false)
|
||||||
menuActive: boolean,
|
const menuActive = ref(true)
|
||||||
keyboardShortcutsActive: boolean,
|
const keyboardShortcutsActive = ref(false)
|
||||||
quickActionsActive: boolean,
|
const quickActionsActive = ref(false)
|
||||||
logoVisible: boolean,
|
const logoVisible = ref(true)
|
||||||
}
|
|
||||||
|
|
||||||
export const useBaseStore = defineStore('base', {
|
function setLoading(newLoading: boolean) {
|
||||||
state: () : RootStoreState => ({
|
loading.value = newLoading
|
||||||
loading: false,
|
}
|
||||||
ready: false,
|
|
||||||
|
|
||||||
// This is used to highlight the current list in menu for all list related views
|
function setCurrentList(newCurrentList: IList | null) {
|
||||||
currentList: new ListModel({
|
// Server updates don't return the right. Therefore, the right is reset after updating the list which is
|
||||||
id: 0,
|
// confusing because all the buttons will disappear in that case. To prevent this, we're keeping the right
|
||||||
isArchived: false,
|
// when updating the list in global state.
|
||||||
}),
|
if (
|
||||||
background: '',
|
typeof currentList.value?.maxRight !== 'undefined' &&
|
||||||
blurHash: '',
|
newCurrentList !== null &&
|
||||||
|
(
|
||||||
|
typeof newCurrentList.maxRight === 'undefined' ||
|
||||||
|
newCurrentList.maxRight === null
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
newCurrentList.maxRight = currentList.value.maxRight
|
||||||
|
}
|
||||||
|
currentList.value = newCurrentList
|
||||||
|
}
|
||||||
|
|
||||||
hasTasks: false,
|
function setHasTasks(newHasTasks: boolean) {
|
||||||
menuActive: true,
|
hasTasks.value = newHasTasks
|
||||||
keyboardShortcutsActive: false,
|
}
|
||||||
quickActionsActive: false,
|
|
||||||
logoVisible: true,
|
|
||||||
}),
|
|
||||||
|
|
||||||
actions: {
|
function setMenuActive(newMenuActive: boolean) {
|
||||||
setLoading(loading: boolean) {
|
menuActive.value = newMenuActive
|
||||||
this.loading = loading
|
}
|
||||||
},
|
|
||||||
|
|
||||||
setCurrentList(currentList: IList | null) {
|
function toggleMenu() {
|
||||||
// Server updates don't return the right. Therefore, the right is reset after updating the list which is
|
menuActive.value = !menuActive.value
|
||||||
// confusing because all the buttons will disappear in that case. To prevent this, we're keeping the right
|
}
|
||||||
// when updating the list in global state.
|
|
||||||
if (
|
|
||||||
typeof this.currentList?.maxRight !== 'undefined' &&
|
|
||||||
currentList !== null &&
|
|
||||||
(
|
|
||||||
typeof currentList.maxRight === 'undefined' ||
|
|
||||||
currentList.maxRight === null
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
currentList.maxRight = this.currentList.maxRight
|
|
||||||
}
|
|
||||||
this.currentList = currentList
|
|
||||||
},
|
|
||||||
|
|
||||||
setHasTasks(hasTasks: boolean) {
|
function setKeyboardShortcutsActive(value: boolean) {
|
||||||
this.hasTasks = hasTasks
|
keyboardShortcutsActive.value = value
|
||||||
},
|
}
|
||||||
|
|
||||||
setMenuActive(menuActive: boolean) {
|
function setQuickActionsActive(value: boolean) {
|
||||||
this.menuActive = menuActive
|
quickActionsActive.value = value
|
||||||
},
|
}
|
||||||
|
|
||||||
toggleMenu() {
|
function setBackground(newBackground: string) {
|
||||||
this.menuActive = !this.menuActive
|
background.value = newBackground
|
||||||
},
|
}
|
||||||
|
|
||||||
setKeyboardShortcutsActive(active: boolean) {
|
function setBlurHash(newBlurHash: string) {
|
||||||
this.keyboardShortcutsActive = active
|
blurHash.value = newBlurHash
|
||||||
},
|
}
|
||||||
|
|
||||||
setQuickActionsActive(active: boolean) {
|
function setLogoVisible(visible: boolean) {
|
||||||
this.quickActionsActive = active
|
logoVisible.value = visible
|
||||||
},
|
}
|
||||||
|
|
||||||
|
function setReady(value: boolean) {
|
||||||
|
ready.value = value
|
||||||
|
}
|
||||||
|
|
||||||
setBackground(background: string) {
|
async function handleSetCurrentList(
|
||||||
this.background = background
|
{list, forceUpdate = false}: {list: IList | null, forceUpdate: boolean},
|
||||||
},
|
) {
|
||||||
|
if (list === null) {
|
||||||
|
setCurrentList({})
|
||||||
|
setBackground('')
|
||||||
|
setBlurHash('')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
setBlurHash(blurHash: string) {
|
// The forceUpdate parameter is used only when updating a list background directly because in that case
|
||||||
this.blurHash = blurHash
|
// the current list stays the same, but we want to show the new background right away.
|
||||||
},
|
if (list.id !== currentList.value.id || forceUpdate) {
|
||||||
|
if (list.backgroundInformation) {
|
||||||
setLogoVisible(visible: boolean) {
|
try {
|
||||||
this.logoVisible = visible
|
const blurHash = await getBlobFromBlurHash(list.backgroundBlurHash)
|
||||||
},
|
if (blurHash) {
|
||||||
|
setBlurHash(window.URL.createObjectURL(blurHash))
|
||||||
setReady(ready: boolean) {
|
|
||||||
this.ready = ready
|
|
||||||
},
|
|
||||||
|
|
||||||
async handleSetCurrentList({list, forceUpdate = false} : {list: IList | null, forceUpdate: boolean}) {
|
|
||||||
if (list === null) {
|
|
||||||
this.setCurrentList({})
|
|
||||||
this.setBackground('')
|
|
||||||
this.setBlurHash('')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// The forceUpdate parameter is used only when updating a list background directly because in that case
|
|
||||||
// the current list stays the same, but we want to show the new background right away.
|
|
||||||
if (list.id !== this.currentList.id || forceUpdate) {
|
|
||||||
if (list.backgroundInformation) {
|
|
||||||
try {
|
|
||||||
const blurHash = await getBlobFromBlurHash(list.backgroundBlurHash)
|
|
||||||
if (blurHash) {
|
|
||||||
this.setBlurHash(window.URL.createObjectURL(blurHash))
|
|
||||||
}
|
|
||||||
|
|
||||||
const listService = new ListService()
|
|
||||||
const background = await listService.background(list)
|
|
||||||
this.setBackground(background)
|
|
||||||
} catch (e) {
|
|
||||||
console.error('Error getting background image for list', list.id, e)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const listService = new ListService()
|
||||||
|
const background = await listService.background(list)
|
||||||
|
setBackground(background)
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error getting background image for list', list.id, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof list.backgroundInformation === 'undefined' || list.backgroundInformation === null) {
|
if (
|
||||||
this.setBackground('')
|
typeof list.backgroundInformation === 'undefined' ||
|
||||||
this.setBlurHash('')
|
list.backgroundInformation === null
|
||||||
}
|
) {
|
||||||
|
setBackground('')
|
||||||
|
setBlurHash('')
|
||||||
|
}
|
||||||
|
|
||||||
this.setCurrentList(list)
|
setCurrentList(list)
|
||||||
},
|
}
|
||||||
|
|
||||||
async loadApp() {
|
const authStore = useAuthStore()
|
||||||
await checkAndSetApiUrl(window.API_URL)
|
async function loadApp() {
|
||||||
await useAuthStore().checkAuth()
|
await checkAndSetApiUrl(window.API_URL)
|
||||||
this.ready = true
|
await authStore.checkAuth()
|
||||||
},
|
ready.value = true
|
||||||
},
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
loading: readonly(loading),
|
||||||
|
ready: readonly(ready),
|
||||||
|
currentList: readonly(currentList),
|
||||||
|
background: readonly(background),
|
||||||
|
blurHash: readonly(blurHash),
|
||||||
|
hasTasks: readonly(hasTasks),
|
||||||
|
menuActive: readonly(menuActive),
|
||||||
|
keyboardShortcutsActive: readonly(keyboardShortcutsActive),
|
||||||
|
quickActionsActive: readonly(quickActionsActive),
|
||||||
|
logoVisible: readonly(logoVisible),
|
||||||
|
|
||||||
|
setLoading,
|
||||||
|
setCurrentList,
|
||||||
|
setHasTasks,
|
||||||
|
setMenuActive,
|
||||||
|
toggleMenu,
|
||||||
|
setKeyboardShortcutsActive,
|
||||||
|
setQuickActionsActive,
|
||||||
|
setBackground,
|
||||||
|
setBlurHash,
|
||||||
|
setLogoVisible,
|
||||||
|
setReady,
|
||||||
|
|
||||||
|
handleSetCurrentList,
|
||||||
|
loadApp,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// support hot reloading
|
// support hot reloading
|
||||||
|
Loading…
x
Reference in New Issue
Block a user