mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +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
|
// Load the user and the user tasks
|
||||||
Promise.all([
|
Promise.all([
|
||||||
store.dispatch('fetchUser'),
|
store.dispatch('user.fetch'),
|
||||||
store.dispatch('fetchUserTasks'),
|
store.dispatch('tasks.fetchUserTasks'),
|
||||||
]).catch(() => {
|
]).catch(() => {
|
||||||
alert('Impossible to fetch user. Copy into localStorage a valid habit-mobile-settings object.');
|
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) {
|
const actions = {
|
||||||
store.state.title = title;
|
tasks,
|
||||||
}
|
user,
|
||||||
|
};
|
||||||
|
|
||||||
export function fetchUser (store) {
|
export default actions;
|
||||||
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;
|
|
||||||
}
|
|
||||||
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 Vue from 'vue';
|
||||||
import state from './state';
|
import state from './state';
|
||||||
import * as actions from './actions';
|
import actions from './actions';
|
||||||
import * as getters from './getters';
|
import * as getters from './getters';
|
||||||
|
import { get } from 'lodash';
|
||||||
|
|
||||||
// Central application store for Habitica
|
// Central application store for Habitica
|
||||||
// Heavily inspired to Vuex (https://github.com/vuejs/vuex) with a very
|
// 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)
|
// Actions should be called using store.dispatch(ACTION_NAME, ...ARGS)
|
||||||
// They get passed the store instance and any additional argument passed to dispatch()
|
// They get passed the store instance and any additional argument passed to dispatch()
|
||||||
dispatch (type, ...args) {
|
dispatch (type, ...args) {
|
||||||
let action = actions[type];
|
let action = get(actions, type);
|
||||||
|
|
||||||
if (!action) throw new Error(`Action "${type}" not found.`);
|
if (!action) throw new Error(`Action "${type}" not found.`);
|
||||||
return action(store, ...args);
|
return action(store, ...args);
|
||||||
|
|||||||
Reference in New Issue
Block a user