2021-05-04 18:05:03 +02:00
|
|
|
import { createStore } from 'vuex';
|
|
|
|
|
2021-05-05 12:51:01 +02:00
|
|
|
const counterModule = {
|
2021-05-04 18:05:03 +02:00
|
|
|
state() {
|
|
|
|
return {
|
2021-05-05 12:51:01 +02:00
|
|
|
counter: 0
|
2021-05-04 18:05:03 +02:00
|
|
|
};
|
2021-05-04 21:49:40 +02:00
|
|
|
},
|
2021-05-05 12:51:01 +02:00
|
|
|
actions: {
|
|
|
|
increment(context) {
|
|
|
|
setTimeout(function() {
|
|
|
|
context.commit('increment');
|
|
|
|
}, 2000);
|
|
|
|
},
|
|
|
|
increase(context, payload) {
|
|
|
|
context.commit('increase', payload);
|
|
|
|
}
|
|
|
|
},
|
2021-05-04 21:49:40 +02:00
|
|
|
mutations: {
|
|
|
|
increment(state) {
|
|
|
|
state.counter = state.counter + 1;
|
2021-05-04 22:13:48 +02:00
|
|
|
},
|
|
|
|
increase(state, payload) {
|
|
|
|
state.counter = state.counter + payload.value;
|
2021-05-04 21:49:40 +02:00
|
|
|
}
|
2021-05-04 22:20:06 +02:00
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
finalCounter(state) {
|
|
|
|
return state.counter * 3;
|
|
|
|
},
|
|
|
|
normalizedCounter(_, getters) {
|
|
|
|
const finalCounter = getters.finalCounter;
|
|
|
|
if (finalCounter < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (finalCounter > 100) {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
return finalCounter;
|
2021-05-05 12:51:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const store = createStore({
|
|
|
|
modules: {
|
|
|
|
counter: counterModule
|
|
|
|
},
|
|
|
|
state() {
|
|
|
|
return {
|
|
|
|
isLoggedIn: false
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
setAuth(state, payload) {
|
|
|
|
state.isLoggedIn = payload.isAuth;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getters: {
|
2021-05-04 23:31:53 +02:00
|
|
|
userIsAuthenticated(state) {
|
|
|
|
return state.isLoggedIn;
|
2021-05-04 22:20:06 +02:00
|
|
|
}
|
2021-05-04 22:48:12 +02:00
|
|
|
},
|
|
|
|
actions: {
|
2021-05-04 23:31:53 +02:00
|
|
|
login(context) {
|
|
|
|
context.commit('setAuth', { isAuth: true });
|
|
|
|
},
|
|
|
|
logout(context) {
|
|
|
|
context.commit('setAuth', { isAuth: false });
|
2021-05-04 22:48:12 +02:00
|
|
|
}
|
2021-05-04 18:05:03 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default store;
|