diff --git a/website/client/main.js b/website/client/main.js index 7ec2e55dc4..1a43a4dd4a 100644 --- a/website/client/main.js +++ b/website/client/main.js @@ -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.'); }); diff --git a/website/client/store/actions/index.js b/website/client/store/actions/index.js index 0128908104..383d8e5c1d 100644 --- a/website/client/store/actions/index.js +++ b/website/client/store/actions/index.js @@ -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; -} \ No newline at end of file +export default actions; \ No newline at end of file diff --git a/website/client/store/actions/tasks.js b/website/client/store/actions/tasks.js new file mode 100644 index 0000000000..84263f99fa --- /dev/null +++ b/website/client/store/actions/tasks.js @@ -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; diff --git a/website/client/store/actions/user.js b/website/client/store/actions/user.js new file mode 100644 index 0000000000..aeef1ae848 --- /dev/null +++ b/website/client/store/actions/user.js @@ -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; \ No newline at end of file diff --git a/website/client/store/index.js b/website/client/store/index.js index fb4f754ed5..eefc988dda 100644 --- a/website/client/store/index.js +++ b/website/client/store/index.js @@ -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);