parsley/app/javascript/store/index.js

64 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-03-29 02:15:47 -05:00
import Vue from 'vue'
import Vuex from 'vuex'
2018-03-29 01:57:00 -05:00
2018-04-01 12:17:54 -05:00
import api from '../lib/Api';
2018-03-29 02:15:47 -05:00
Vue.use(Vuex);
2018-03-29 01:57:00 -05:00
2018-03-29 02:15:47 -05:00
export default new Vuex.Store({
strict: process.env.NODE_ENV !== 'production',
state: {
2018-03-30 14:31:09 -05:00
loading: false,
2018-03-30 17:08:09 -05:00
error: null,
authChecked: false,
2018-04-01 21:43:23 -05:00
user: null,
// MediaQueryList objects in the root App component maintain this state.
mediaQueries: {
mobile: false,
tablet: false,
tabletOnly: false,
touch: false,
desktop: false,
desktopOnly: false,
widescreen: false,
widescreenOnly: false,
fullhd: false
}
2018-03-29 02:15:47 -05:00
},
getters: {
2018-03-30 17:08:09 -05:00
isLoading(state) {
return state.loading === true;
},
isLoggedIn(state) {
return state.user !== null;
2018-04-01 12:17:54 -05:00
},
isAdmin(state) {
return state.user !== null && state.user.admin === true;
2018-03-30 17:08:09 -05:00
}
2018-03-29 02:15:47 -05:00
},
mutations: {
2018-03-30 14:31:09 -05:00
setLoading(state, value) {
state.loading = value;
},
2018-03-29 02:15:47 -05:00
2018-03-30 14:31:09 -05:00
setError(state, value) {
state.error = value;
2018-03-30 17:08:09 -05:00
},
setUser(state, user) {
state.authChecked = true;
state.user = user;
2018-04-01 21:43:23 -05:00
},
setMediaQuery(state, data) {
state.mediaQueries[data.mediaName] = data.value;
2018-03-30 14:31:09 -05:00
}
2018-03-29 02:15:47 -05:00
},
actions: {
2018-04-01 12:17:54 -05:00
logout({commit}) {
return api.getLogout()
.then(() => commit("setUser", null));
}
2018-03-29 02:15:47 -05:00
}
});