store.js 1.51 KB
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
    strict: true,
    plugins: [
        createPersistedState()
    ],
    state: {
        token: null,
        data: null,
        isUserLoggedIn: false,
        id: null,
        role: null,
        schoolId: null
    },
    // serve as the one and only way to change the state of the data in the state object
    mutations: {
        setToken(state, token) {
            state.token = token
                //state.isUserLoggedIn = !!(token)
            if (token != null) {
                state.isUserLoggedIn = true
            } else {
                state.isUserLoggedIn = false
            }
        },
        setUser(state, data) {
            state.data = data
        },
        Id(state, id) {
            state.id = id
        },
        Role(state, role) {
            state.role = role
        },
        SchoolId(state, schoolId) {
            state.schoolId = schoolId
        }
    },
    //Action methods are referred to as being "dispatched"
    actions: {
        setToken({ commit }, token) {
            commit('setToken', token)
        },
        setUser({ commit }, data) {
            commit('setUser', data)
        },
        Id({ commit }, id) {
            commit('Id', id)
        },
        Role({ commit }, role) {
            commit('Role', role)
        },
        SchoolId({ commit }, schoolId) {
            commit('SchoolId', schoolId)
        }

    }

})