Keep state around

This commit is contained in:
Matthias 2021-08-28 19:59:56 +02:00
parent f30f968a2f
commit a253377124
2 changed files with 20 additions and 12 deletions

View File

@ -15,9 +15,8 @@ const AUTO_REFRESH = 'ft_auto_refresh';
Vue.use(Vuex);
const initCurrentTheme = getCurrentTheme();
export default new Vuex.Store({
const store = new Vuex.Store({
modules: {
ftbot: createBotStore(),
alerts: alertsModule,
layout: layoutModule,
uiSettings: settingsModule,
@ -152,3 +151,7 @@ export default new Vuex.Store({
},
},
});
store.registerModule('ftbot', createBotStore(store));
store.dispatch('ftbot/addBot', 'ftbot.0');
export default store;

View File

@ -5,18 +5,21 @@ interface FTMultiBotState {
availableBots: string[];
}
export default function createBotStore() {
export default function createBotStore(store) {
const state: FTMultiBotState = {
selectedBot: 'ftbot.0',
availableBots: ['ftbot.0'],
availableBots: [],
};
// All getters working on all bots should be prefixed with all.
const getters = {
selectedBot(state: FTMultiBotState) {
hasBots(state: FTMultiBotState): boolean {
return state.availableBots.length > 0;
},
selectedBot(state: FTMultiBotState): string {
return state.selectedBot;
},
allAvailableBots(state: FTMultiBotState) {
allAvailableBots(state: FTMultiBotState): string[] {
return state.availableBots;
},
};
@ -35,12 +38,14 @@ export default function createBotStore() {
const actions = {
// Actions automatically filled below
addBot({ getters, commit }, botName: string) {
if (getters.allAvailableBots.includes(botName)) {
addBot({ getters, commit }, botId: string) {
if (getters.allAvailableBots.includes(botId)) {
// throw 'Bot already present';
// TODO: handle error!
}
commit('addBot', botName);
console.log('add bot', botId);
store.registerModule(`ftbot/${botId}`, createBotSubStore(botId));
commit('addBot', botId);
},
};
// Autocreate Actions
@ -52,9 +57,9 @@ export default function createBotStore() {
return {
namespaced: true,
modules: {
'ftbot.0': createBotSubStore('ftbot.0'),
},
// modules: {
// 'ftbot.0': createBotSubStore('ftbot.0'),
// },
state,
mutations,