🏗️ (vuex) init vuex store

This commit is contained in:
2020-07-11 23:03:55 +02:00
parent 4f235a8264
commit d9d8337297

View File

@@ -1,11 +1,55 @@
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex, { ActionContext, ActionTree } from 'vuex'
import VuexPersistence from 'vuex-persist'
Vue.use(Vuex) Vue.use(Vuex)
export default new Vuex.Store({ export interface State {
state: {}, interval: number
mutations: {}, withMusic: boolean
actions: {}, }
modules: {}
export interface RootActions extends ActionTree<State, State> {
setInterval: (ctx: ActionContext<State, State>, payload: number) => void
setWithMusic: (ctx: ActionContext<State, State>, payload: boolean) => void
}
const vuexLocal = new VuexPersistence<State>({
key: 'binome',
storage: window.localStorage
}) })
const SET_INTERVAL = 'SET_INTERVAL'
const WITH_MUSIC = 'WITH_MUSIC'
const store = new Vuex.Store<State>({
state: {
interval: 5,
withMusic: true
},
getters: {
interval: ({ interval }) => interval,
withMusic: ({ withMusic }) => withMusic
},
mutations: {
[SET_INTERVAL](state, interval: number) {
state.interval = interval
},
[WITH_MUSIC](state, withMusic: boolean) {
state.withMusic = withMusic
}
},
actions: {
setInterval({ commit }, interval: number) {
if (interval > 0) {
commit(SET_INTERVAL, interval)
}
},
setWithMusic({ commit }, withMusic: number) {
commit(WITH_MUSIC, withMusic)
}
},
plugins: [vuexLocal.plugin]
})
export default store