diff --git a/src/store/index.ts b/src/store/index.ts index 0107c20..421ae72 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,11 +1,55 @@ import Vue from 'vue' -import Vuex from 'vuex' +import Vuex, { ActionContext, ActionTree } from 'vuex' +import VuexPersistence from 'vuex-persist' Vue.use(Vuex) -export default new Vuex.Store({ - state: {}, - mutations: {}, - actions: {}, - modules: {} +export interface State { + interval: number + withMusic: boolean +} + +export interface RootActions extends ActionTree { + setInterval: (ctx: ActionContext, payload: number) => void + setWithMusic: (ctx: ActionContext, payload: boolean) => void +} + +const vuexLocal = new VuexPersistence({ + key: 'binome', + storage: window.localStorage }) + +const SET_INTERVAL = 'SET_INTERVAL' +const WITH_MUSIC = 'WITH_MUSIC' + +const store = new Vuex.Store({ + 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