mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-10-26 10:42:52 +01:00
client: reorganize actions
This commit is contained in:
@@ -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.');
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
15
website/client/store/actions/tasks.js
Normal file
15
website/client/store/actions/tasks.js
Normal 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;
|
||||
15
website/client/store/actions/user.js
Normal file
15
website/client/store/actions/user.js
Normal 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;
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user