client: reorganize actions

This commit is contained in:
Matteo Pagliazzi
2016-12-06 17:11:40 -08:00
parent f8d4a2bd6b
commit 0b0466b960
5 changed files with 42 additions and 27 deletions

View File

@@ -43,8 +43,8 @@ let userDataWatcher = store.watch(state => [state.user, state.tasks], ([user, ta
// Load the user and the user tasks
Promise.all([
store.dispatch('fetchUser'),
store.dispatch('fetchUserTasks'),
store.dispatch('user.fetch'),
store.dispatch('tasks.fetchUserTasks'),
]).catch(() => {
alert('Impossible to fetch user. Copy into localStorage a valid habit-mobile-settings object.');
});

View File

@@ -1,25 +1,9 @@
import Vue from 'vue';
import tasks from './tasks';
import user from './user';
export function setTitle (store, title) {
store.state.title = title;
}
const actions = {
tasks,
user,
};
export function fetchUser (store) {
let promise = Vue.http.get('/api/v3/user');
promise.then((response) => {
store.state.user = response.body.data;
});
return promise;
}
export function fetchUserTasks (store) {
let promise = Vue.http.get('/api/v3/tasks/user');
promise.then((response) => {
store.state.tasks = response.body.data;
});
return promise;
}
export default actions;

View File

@@ -0,0 +1,15 @@
import Vue from 'vue';
const actions = {};
actions.fetchUserTasks = function fetchUserTasks (store) {
let promise = Vue.http.get('/api/v3/tasks/user');
promise.then((response) => {
store.state.tasks = response.body.data;
});
return promise;
};
export default actions;

View File

@@ -0,0 +1,15 @@
import Vue from 'vue';
const actions = {};
actions.fetch = function fetchUser (store) {
let promise = Vue.http.get('/api/v3/user');
promise.then((response) => {
store.state.user = response.body.data;
});
return promise;
};
export default actions;

View File

@@ -1,7 +1,8 @@
import Vue from 'vue';
import state from './state';
import * as actions from './actions';
import actions from './actions';
import * as getters from './getters';
import { get } from 'lodash';
// Central application store for Habitica
// Heavily inspired to Vuex (https://github.com/vuejs/vuex) with a very
@@ -21,7 +22,7 @@ const store = {
// Actions should be called using store.dispatch(ACTION_NAME, ...ARGS)
// They get passed the store instance and any additional argument passed to dispatch()
dispatch (type, ...args) {
let action = actions[type];
let action = get(actions, type);
if (!action) throw new Error(`Action "${type}" not found.`);
return action(store, ...args);