More Client Fixes (#9036)

* add automatic user syncing when user._v does not match with server

* fix google signup

* fixes to user sync

* check for next cron on activity

* add comment
This commit is contained in:
Matteo Pagliazzi
2017-09-14 18:55:17 +02:00
committed by GitHub
parent 2f913666cd
commit c6881c5e30
4 changed files with 59 additions and 26 deletions

View File

@@ -130,6 +130,7 @@ export default {
this.selectedItemToBuy = item;
});
// @TODO split up this file, it's too big
// Set up Error interceptors
axios.interceptors.response.use((response) => {
if (this.user) {
@@ -149,6 +150,39 @@ export default {
return Promise.reject(error);
});
axios.interceptors.response.use((response) => {
// Verify that the user was not updated from another browser/app/client
// If it was, sync
const url = response.config.url;
const method = response.config.method;
const isApiCall = url.indexOf('api/v3') !== -1;
const userV = response.data && response.data.userV;
if (this.isUserLoaded && isApiCall && userV) {
const oldUserV = this.user._v;
this.user._v = userV;
// Do not sync again if already syncing
const isUserSync = url.indexOf('/api/v3/user') === 0 && method === 'get';
const isTasksSync = url.indexOf('/api/v3/tasks/user') === 0 && method === 'get';
// exclude chat seen requests because with real time chat they would be too many
const isChatSeen = url.indexOf('/chat/seen') !== -1 && method === 'post';
// exclude POST /api/v3/cron because the user is synced automatically after cron runs
const isCron = url.indexOf('/api/v3/cron') === 0 && method === 'post';
// Something has changed on the user object that was not tracked here, sync the user
if (userV - oldUserV > 1 && !isCron && !isChatSeen && !isUserSync && !isTasksSync) {
Promise.all([
this.$store.dispatch('user:fetch', {forceLoad: true}),
this.$store.dispatch('tasks:fetchUserTasks', {forceLoad: true}),
]);
}
}
return response;
});
// Setup listener for title
this.$store.watch(state => state.title, (title) => {
document.title = title;