Skip to content

Commit

Permalink
reformatted store to comply with Nuxt 3's modular system
Browse files Browse the repository at this point in the history
  • Loading branch information
rastenis committed Jul 23, 2019
1 parent 565a9ba commit 3e049bf
Showing 1 changed file with 138 additions and 140 deletions.
278 changes: 138 additions & 140 deletions store/index.js
Original file line number Diff line number Diff line change
@@ -1,147 +1,145 @@
import Vuex from "vuex";
import axios from "axios";

const util = require("util");
// using the new Module design. Old (classic) Vuex configuration can be viewed in this project's pre-release versions (<1.0.0)
// modules can be exported into separate files. Also, multiple stores can be described this way.
// read more: https://nuxtjs.org/guide/vuex-store/

const store = () =>
new Vuex.Store({
state: {
user: null,
flash: null
},
mutations: {
SET_USER: function SET_USER(state, user) {
state.user = user;
},
SET_FLASH: function SET_FLASH(state, flash) {
state.flash = flash;
},
CLEAR_MESSAGE: function CLEAR_MESSAGE(state) {
state.flash = state.flash.slice(1);
}
},
actions: {
nuxtServerInit({ commit }, { req }) {
if (req) {
// req doesn't exist when running tests
if (
typeof req.session !== "undefined" &&
typeof req.user !== "undefined"
) {
commit("SET_USER", req.user);
}
if (
typeof req.session !== "undefined" &&
typeof req.session.flash !== "undefined"
) {
commit("SET_FLASH", req.session.flash);
req.session.flash = [];
}
}
},
login({ commit }, { email, password }) {
return axios({
method: "post",
url: "/login",
credentials: "same-origin",
data: {
email,
password
}
})
.then(res => {
if (res.data.meta.error === true) {
throw res.data;
}
export const state = () => ({
user: null,
flash: null
});

export const mutations = {
SET_USER(state, user) {
state.user = user;
},
SET_FLASH(state, flash) {
state.flash = flash;
},
CLEAR_MESSAGE(state) {
state.flash = state.flash.slice(1);
}
};

// if no errors, continue
return res.data.user;
})
.then(authUser => {
commit("SET_USER", authUser);
});
},
register({ commit }, { email, password }) {
return axios({
method: "post",
url: "/register",
credentials: "same-origin",
data: {
email,
password
}
})
.then(res => {
if (res.data.meta.error === true) {
throw res.data;
}
return res.data.user;
})
.then(authUser => {
commit("SET_USER", authUser);
});
},
changePassword({ commit }, { password, newPassword, newPasswordRep }) {
return axios({
method: "patch",
url: "/changePassword",
credentials: "same-origin",
data: {
password,
newPassword,
newPasswordRep
}
})
.then(res => {
if (res.data.meta.error === true) {
throw res.data;
}
// server returns user with adjusted data,
// so we just SET_USER it
return res.data.user;
})
.then(modifiedUser => {
commit("SET_USER", modifiedUser);
});
},
unlink({ commit }, { toUnlink }) {
return axios({
method: "post",
url: "/unlink",
credentials: "same-origin",
data: {
toUnlink
}
})
.then(res => {
if (res.data.meta.error === true) {
throw res.data;
}
return res.data.user;
})
.then(modifiedUser => {
commit("SET_USER", modifiedUser);
});
},
deleteAccount({ commit }) {
return axios({
method: "post",
url: "/deleteAccount",
credentials: "same-origin"
}).then(() => {
commit("SET_USER", null);
});
},
logout({ commit }) {
return axios({
method: "post",
url: "/logout",
credentials: "same-origin"
}).then(() => {
commit("SET_USER", null);
});
export const actions = {
nuxtServerInit({ commit }, { req }) {
if (req) {
// req doesn't exist when running tests
if (
typeof req.session !== "undefined" &&
typeof req.user !== "undefined"
) {
commit("SET_USER", req.user);
}
if (
typeof req.session !== "undefined" &&
typeof req.session.flash !== "undefined"
) {
commit("SET_FLASH", req.session.flash);
req.session.flash = [];
}
}
});
},
login({ commit }, { email, password }) {
return axios({
method: "post",
url: "/login",
credentials: "same-origin",
data: {
email,
password
}
})
.then(res => {
if (res.data.meta.error === true) {
throw res.data;
}

export default store;
// if no errors, continue
return res.data.user;
})
.then(authUser => {
commit("SET_USER", authUser);
});
},
register({ commit }, { email, password }) {
return axios({
method: "post",
url: "/register",
credentials: "same-origin",
data: {
email,
password
}
})
.then(res => {
if (res.data.meta.error === true) {
throw res.data;
}
return res.data.user;
})
.then(authUser => {
commit("SET_USER", authUser);
});
},
changePassword({ commit }, { password, newPassword, newPasswordRep }) {
return axios({
method: "patch",
url: "/changePassword",
credentials: "same-origin",
data: {
password,
newPassword,
newPasswordRep
}
})
.then(res => {
if (res.data.meta.error === true) {
throw res.data;
}
// server returns user with adjusted data,
// so we just SET_USER it
return res.data.user;
})
.then(modifiedUser => {
commit("SET_USER", modifiedUser);
});
},
unlink({ commit }, { toUnlink }) {
return axios({
method: "post",
url: "/unlink",
credentials: "same-origin",
data: {
toUnlink
}
})
.then(res => {
if (res.data.meta.error === true) {
throw res.data;
}
return res.data.user;
})
.then(modifiedUser => {
commit("SET_USER", modifiedUser);
});
},
deleteAccount({ commit }) {
return axios({
method: "post",
url: "/deleteAccount",
credentials: "same-origin"
}).then(() => {
commit("SET_USER", null);
});
},
logout({ commit }) {
return axios({
method: "post",
url: "/logout",
credentials: "same-origin"
}).then(() => {
commit("SET_USER", null);
});
}
};

0 comments on commit 3e049bf

Please sign in to comment.