parsley/app/javascript/store/index.js

190 lines
4.5 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-04-17 18:38:48 -05:00
updateAvailable: false,
2018-04-18 17:04:25 -05:00
loadingCount: 0,
2018-03-30 17:08:09 -05:00
error: null,
authChecked: false,
2018-04-01 21:43:23 -05:00
user: null,
2018-09-06 18:16:13 -05:00
loginMessage: null,
taskLists: [],
currentTaskList: null,
2018-04-01 21:43:23 -05:00
// 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) {
2018-08-28 16:52:56 -05:00
return state.loadingCount > 0;
2018-03-30 17:08:09 -05:00
},
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-04-17 18:38:48 -05:00
setUpdateAvailable(state, value) {
state.updateAvailable = value;
},
2018-03-30 14:31:09 -05:00
setLoading(state, value) {
2018-04-18 17:04:25 -05:00
if (value) {
state.loadingCount = state.loadingCount + 1;
} else {
state.loadingCount = state.loadingCount - 1;
}
state.loading = state.loadingCount !== 0;
2018-03-30 14:31:09 -05:00
},
2018-03-29 02:15:47 -05:00
2018-03-30 14:31:09 -05:00
setError(state, value) {
2018-04-07 10:54:56 -05:00
console.log(value);
2018-03-30 14:31:09 -05:00
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
},
2018-09-06 18:16:13 -05:00
setLoginMessage(state, msg) {
state.loginMessage = msg;
},
setTaskLists(state, lists) {
state.taskLists = lists || [];
},
setCurrentTaskList(state, list) {
state.currentTaskList = list || null;
},
appendTaskItem(state, item) {
const listId = item.task_list_id;
const list = state.taskLists.find(l => l.id === listId);
if (list) {
list.task_items.push(item);
}
},
replaceTaskItem(state, item) {
const listId = item.task_list_id;
const list = state.taskLists.find(l => l.id === listId);
if (list) {
const taskIdx = list.task_items.findIndex(i => i.id === item.id);
if (taskIdx >= 0) {
list.task_items.splice(taskIdx, 1, item);
}
}
},
removeTaskItem(state, item) {
const listId = item.task_list_id;
const list = state.taskLists.find(l => l.id === listId);
if (list) {
const taskIdx = list.task_items.findIndex(i => i.id === item.id);
if (taskIdx >= 0) {
list.task_items.splice(taskIdx, 1);
}
}
2018-03-30 14:31:09 -05:00
}
2018-03-29 02:15:47 -05:00
},
actions: {
2018-09-06 18:16:13 -05:00
updateCurrentUser({commit}) {
return api.getCurrentUser()
.then(user => {
commit("setUser", user);
return user;
});
},
login({commit}, authData) {
return api.postLogin(authData.username, authData.password)
.then(data => {
if (data.success) {
commit("setUser", data.user);
commit("setLoginMessage", null);
} else {
commit("setUser", null);
commit("setLoginMessage", data.message);
}
return data;
});
},
2018-04-01 12:17:54 -05:00
logout({commit}) {
return api.getLogout()
2018-09-06 18:16:13 -05:00
.then(() => {
commit("setUser", null);
});
},
refreshTaskLists({commit, state}) {
const cb = function(data) {
commit("setTaskLists", data);
let ctl = null;
if (state.currentTaskList) {
ctl = data.find(l => l.id === state.currentTaskList.id);
}
ctl = ctl || data[0] || null;
commit("setCurrentTaskList", ctl);
};
return api.getTaskLists(cb)
},
createTaskList({commit, dispatch}, newList) {
return api.postTaskList(newList)
.then(data => commit("setCurrentTaskList", data))
.then(() => dispatch("refreshTaskLists"))
},
deleteTaskList({dispatch}, taskList) {
return api.deleteTaskList(taskList)
.then(() => dispatch("refreshTaskLists"));
},
createTaskItem({commit, dispatch}, taskItem) {
return api.postTaskItem(taskItem.task_list_id, taskItem)
.then(data => {
commit("appendTaskItem", data);
return data;
});
},
updateTaskItem({commit}, taskItem) {
return api.patchTaskItem(taskItem.task_list_id, taskItem)
.then(data => {
commit("replaceTaskItem", data);
return data;
});
},
deleteTaskItem({commit}, taskItem) {
return api.deleteTaskItem(taskItem.task_list_id, taskItem)
.then(() => commit("removeTaskItem", taskItem));
2018-04-01 12:17:54 -05:00
}
2018-03-29 02:15:47 -05:00
}
});