Files
habitica/website/client/main.js
Matteo Pagliazzi 257e932bc3 Vue Store (#8071)
* vue: use our own store in place of vuex

* vue store: add getters, watcher and use internal vue instance

* vue store: better state getter and credits to Vuex

* vue store: $watch -> watch

* vuex store: pass store to getters and fix typos

* add comments to store, start writing tests

* fix unit tests and add missing ones

* cleanup components, add less folder, fetch tassks

* use Vuex helpers

* pin vuex version

* move semantic-ui theme to assets/less, keep website/build empty but in git

* import helpers from vuex
2016-09-29 13:32:36 +02:00

51 lines
1.5 KiB
JavaScript

// TODO verify if it's needed, added because Vuex require Promise in the global scope
// and babel-runtime doesn't affect external libraries
require('babel-polyfill');
import Vue from 'vue';
import VueResource from 'vue-resource';
import AppComponent from './app';
import router from './router';
import store from './store';
// TODO just for the beginning
Vue.use(VueResource);
let authSettings = localStorage.getItem('habit-mobile-settings');
if (authSettings) {
authSettings = JSON.parse(authSettings);
Vue.http.headers.common['x-api-user'] = authSettings.auth.apiId;
Vue.http.headers.common['x-api-key'] = authSettings.auth.apiToken;
}
const app = new Vue({
router,
render: h => h(AppComponent),
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 && tasks && tasks.length) {
userDataWatcher(); // remove the watcher
app.$mount('#app');
}
});
// Load the user and the user tasks
Promise.all([
store.dispatch('fetchUser'),
store.dispatch('fetchUserTasks'),
]).catch(() => {
alert('Impossible to fetch user. Copy into localStorage a valid habit-mobile-settings object.');
});