62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
//import dari api
|
|
import * as api from "./api.js";
|
|
|
|
export const store = new Vuex.Store({
|
|
state: {
|
|
isLogin: false,
|
|
token: "",
|
|
isLoading: false,
|
|
errorMessage: "",
|
|
nextPage: ""
|
|
},
|
|
mutations: {
|
|
updateLoading(state, flag) {
|
|
state.isLoading = flag;
|
|
},
|
|
updateIsLogin(state, flag) {
|
|
state.isLogin = flag;
|
|
},
|
|
updateToken(state, resp) {
|
|
state.token = resp.data.token;
|
|
state.nextPage = resp.nextPage;
|
|
},
|
|
updateErrorMessage(state, msg) {
|
|
state.errorMessage = msg;
|
|
}
|
|
},
|
|
actions: {
|
|
async login(context, data) {
|
|
context.commit("updateLoading", true);
|
|
let resp = await api.login(data.userName, data.userPassword);
|
|
if (resp.status == "OK") {
|
|
context.commit("updateIsLogin", true);
|
|
api.saveOneToken(resp.data.token);
|
|
context.commit("updateToken", resp);
|
|
context.commit("updateErrorMessage", "");
|
|
} else {
|
|
context.commit("updateIsLogin", false);
|
|
context.commit("updateErrorMessage", resp.message);
|
|
api.removeOneToken();
|
|
setTimeout(() => {
|
|
context.commit("updateErrorMessage", "");
|
|
}, 3000);
|
|
}
|
|
context.commit("updateLoading", false);
|
|
},
|
|
async loadToken(context) {
|
|
context.commit("updateLoading", true);
|
|
let token = api.loadOneToken();
|
|
if (token == "") {
|
|
context.commit("updateIsLogin", false);
|
|
} else {
|
|
let resp = await api.isLogin(token);
|
|
if (resp.status == "OK") {
|
|
context.commit("updateToken", resp);
|
|
context.commit("updateIsLogin", true);
|
|
}
|
|
}
|
|
context.commit("updateLoading", false);
|
|
}
|
|
}
|
|
});
|