62
loading...
This website collects cookies to deliver better user experience
is a state management pattern + library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.
. Being that some of the components will share data it makes more sense to store this data in a centralized store vs fetching when a component loads and or storing the data in the phones local storage. Also one quick side note my last relied on Vuex for everything (which I think is / was wonderful) however talking to a few ex employees I am not sure we were using proper naming conventions / yada yada yada, so I might be a tad off on a few things. That being said if / when you use Vuex, read the docs they are pretty straight forward and there are numerous resources available on the interweb. Easiest way to install Vuex is to npm i vuex
. I like to create a store folder in the app root and then inside the folder create an index.js. Once we have this we now need to initialize / start up Vuex so we can use it throughout the app. This is done in the app.js (main.js in others and .ts in the typescript apps), but in here we need to import it / use it / add it to the rendering of the app:import Vuex from 'vuex';
Vue.use(Vuex)
new Vue({
render: h => h(App),
store
}).$start()
state: {
user: {
first_name: '',
last_name: '',
email: '',
phone_number: '',
mobile_phone_number: '',
charters: [],
},
token: '',
bookings: [],
calendarEvents: [],
loading: true,
selectedCharter: '',
},
mutations: {
setUser(state, payload) {
state.user = payload;
},
setBookings(state, payload) {
state.bookings = payload;
},
setCalendarEvents(state, payload) {
state.calendarEvents = payload;
},
setSelectedCharter(state, payload) {
state.selectedCharter = payload;
},
setLoading(state, payload) {
state.loading = payload;
},
setToken(state, payload) {
state.token = payload;
}
},
set
as the prefix to my methods cause well it makes sense to me.context.commit
, context.state
, context.getters
, and so on so forth. It exposes the entire API to the programmer.actions: {
setToken(context, payload) {
//fetch Token (check creds of LOGIN)
},
setUser(context, payload) {
//grab the user from the API with a valid token
},
setBookings(context, payload) {
//fetch bookings from the API
},
setCalendarEvents(context, payload) {
//fetch calendar events from the API
},
resetUser(context, payload) {
let user = {
first_name: '',
last_name: '',
email: '',
phone_number: '',
mobile_phone_number: '',
charters: [],
};
context.commit('setUser', user);
},
setSelectedCharter(context, payload) {
context.commit('setSelectedCharter', payload);
},
setLoading(context, payload) {
context.commit('setLoading', payload);
}
},
getters: {
getToken(state) {
return state.token;
},
getUser(state) {
return state.user;
},
getBookings(state) {
return state.bookings;
},
getCalendarEvents(state) {
return state.calendarEvents;
},
getLoading(state) {
return state.loading;
},
getSelectedCharter(state) {
return state.selectedCharter;
}
}
mounted() {
if(ApplicationSettings.hasKey('token')) {
this.$store.commit('setToken', ApplicationSettings.getString('token'));
}
this.$store.dispatch('setUser', this.$store.getters.getToken);
this.$store.dispatch('setBookings', this.$store.getters.getToken);
if(this.$store.getters.getSelectedCharter) {
this.$store.dispatch('setCalendarEvents', {token: this.$store.getters.getToken});
}
},
this.$store
. commit = an action while dispatch = mutation. getters is pretty self explanatory, and you can always use this.$store.state
, but again the getters are basically computed properties and will update whenever state is updated (action) so I never use it and don't really see a need too.