From 4ab7cc4fc0adf3a2621c68d741b8a62b7f7e1e3e Mon Sep 17 00:00:00 2001 From: Dominik Pschenitschni Date: Mon, 31 Oct 2022 14:10:37 +0100 Subject: [PATCH] feat: config store with composition api --- 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 -- 2.40.1