From 15ef86d597ceb8731febf789f1b812a339273e40 Mon Sep 17 00:00:00 2001 From: Dominik Pschenitschni Date: Mon, 31 Oct 2022 20:29:56 +0000 Subject: [PATCH] feat: config store with composition api (#2604) Co-authored-by: Dominik Pschenitschni Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/2604 Co-authored-by: Dominik Pschenitschni Co-committed-by: Dominik Pschenitschni --- src/stores/config.ts | 51 ++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/stores/config.ts b/src/stores/config.ts index 58e23aa77..b9d5de19e 100644 --- a/src/stores/config.ts +++ b/src/stores/config.ts @@ -1,3 +1,4 @@ +import {computed, reactive, toRefs} from 'vue' import {defineStore, acceptHMRUpdate} from 'pinia' import {parseURL} from 'ufo' @@ -36,8 +37,8 @@ export interface ConfigState { }, } -export const useConfigStore = defineStore('config', { - state: (): ConfigState => ({ +export const useConfigStore = defineStore('config', () => { + const state = reactive({ // These are the api defaults. version: '', frontendUrl: '', @@ -66,25 +67,33 @@ export const useConfigStore = defineStore('config', { providers: [], }, }, - }), - getters: { - migratorsEnabled: (state) => state.availableMigrators?.length > 0, - apiBase() { - const {host, protocol} = parseURL(window.API_URL) - return protocol + '//' + host - }, - }, - actions: { - setConfig(config: ConfigState) { - Object.assign(this, config) - }, - async update() { - const HTTP = HTTPFactory() - const {data: config} = await HTTP.get('info') - this.setConfig(objectToCamelCase(config)) - return config - }, - }, + }) + + const migratorsEnabled = computed(() => state.availableMigrators?.length > 0) + const apiBase = computed(() => { + const {host, protocol} = parseURL(window.API_URL) + return protocol + '//' + host + }) + + function setConfig(config: ConfigState) { + Object.assign(state, config) + } + async function update() { + const HTTP = HTTPFactory() + const {data: config} = await HTTP.get('info') + setConfig(objectToCamelCase(config)) + return config + } + + return { + ...toRefs(state), + + migratorsEnabled, + apiBase, + setConfig, + update, + } + }) // support hot reloading