Client: async resources, make store reusable, move plugins and add getTaskFor getter (#8575)

Add library to manage async resource
Make Store reusable for easier testing
Move plugin to libs
Add getTaskFor getter with tests
This commit is contained in:
Matteo Pagliazzi
2017-03-18 18:33:08 +01:00
committed by GitHub
parent 03d6c459bf
commit d9d7c69432
37 changed files with 694 additions and 384 deletions

View File

@@ -6,9 +6,10 @@ import Vue from 'vue';
import axios from 'axios';
import AppComponent from './app';
import router from './router';
import store from './store';
import generateStore from './store';
import StoreModule from './libs/store';
import './filters/registerGlobals';
import i18n from './plugins/i18n';
import i18n from './libs/i18n';
const IS_PRODUCTION = process.env.NODE_ENV === 'production'; // eslint-disable-line no-process-env
@@ -23,8 +24,9 @@ Vue.config.performance = !IS_PRODUCTION;
Vue.config.productionTip = IS_PRODUCTION;
Vue.use(i18n);
Vue.use(StoreModule);
// TODO just for the beginning
// TODO just until we have proper authentication
let authSettings = localStorage.getItem('habit-mobile-settings');
if (authSettings) {
@@ -33,32 +35,35 @@ if (authSettings) {
axios.defaults.headers.common['x-api-key'] = authSettings.auth.apiToken;
}
const app = new Vue({
export default new Vue({
router,
store: generateStore(),
render: h => h(AppComponent),
beforeCreate () {
// Setup listener for title
this.$store.watch(state => state.title, (title) => {
document.title = title;
});
// Mount the app when user and tasks are loaded
const userDataWatcher = this.$store.watch(state => [state.user.data, state.tasks.data], ([user, tasks]) => {
if (user && user._id && Array.isArray(tasks)) {
userDataWatcher(); // remove the watcher
this.$mount('#app');
}
});
// Load the user and the user tasks
Promise.all([
this.$store.dispatch('user:fetch'),
this.$store.dispatch('tasks:fetchUserTasks'),
]).catch((err) => {
console.error(err); // eslint-disable-line no-console
alert('Impossible to fetch user. Copy into localStorage a valid habit-mobile-settings object.');
});
},
mounted () { // Remove the loading screen when the app is mounted
let loadingScreen = document.getElementById('loading-screen');
if (loadingScreen) document.body.removeChild(loadingScreen);
},
});
// Setup listener for title
store.watch(state => state.title, (title) => {
document.title = title;
});
// Mount the app when user and tasks are loaded
let userDataWatcher = store.watch(state => [state.user, state.tasks], ([user, tasks]) => {
if (user && user._id && Array.isArray(tasks)) {
userDataWatcher(); // remove the watcher
app.$mount('#app');
}
});
// Load the user and the user tasks
Promise.all([
store.dispatch('user:fetch'),
store.dispatch('tasks:fetchUserTasks'),
]).catch(() => {
alert('Impossible to fetch user. Copy into localStorage a valid habit-mobile-settings object.');
});
});