From 13c8b556e3b4f2975e835835047bd2c23026a8ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timoth=C3=A9e=20Poussard?= <tpoussard@neogeo.fr>
Date: Tue, 1 Mar 2022 18:26:41 +0100
Subject: [PATCH] change isOffline duplicated function into dynamic store value
 & remove members menu entry if offline

---
 src/App.vue                          |  5 ++++-
 src/main.js                          |  3 +++
 src/store/index.js                   | 22 +++++++++++++---------
 src/views/Projects.vue               | 12 +++++-------
 src/views/feature/Feature_detail.vue |  9 ++++-----
 src/views/feature/Feature_edit.vue   | 13 +++++--------
 src/views/project/Project_detail.vue | 26 ++++++++++++--------------
 7 files changed, 46 insertions(+), 44 deletions(-)

diff --git a/src/App.vue b/src/App.vue
index af053b2d..21da7327 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -78,7 +78,7 @@
               </router-link>
               <router-link
                 v-if="
-                  project &&
+                  project && isOnline &&
                     (user.is_administrator || user.is_superuser || isAdmin)
                 "
                 :to="{
@@ -291,6 +291,9 @@ export default {
       'projects',
       'project',
     ]),
+    ...mapState([
+      'isOnline',
+    ]),
     APPLICATION_NAME() {
       return this.configuration.VUE_APP_APPLICATION_NAME;
     },
diff --git a/src/main.js b/src/main.js
index 2aaa4ad7..c8dd8f15 100644
--- a/src/main.js
+++ b/src/main.js
@@ -38,6 +38,9 @@ if(navigator.serviceWorker){
 
 let onConfigLoaded = function(config){
   store.commit('SET_CONFIG', config);
+  setInterval(() => { //* check if navigator is online
+    store.commit('SET_IS_ONLINE', navigator.onLine);
+  }, 1000);
 
   // set title and favico
   document.title= config.VUE_APP_APPLICATION_NAME+' '+config.VUE_APP_APPLICATION_ABSTRACT;
diff --git a/src/store/index.js b/src/store/index.js
index 3aa9c61d..68a6d4fb 100644
--- a/src/store/index.js
+++ b/src/store/index.js
@@ -23,25 +23,29 @@ const noPermissions = {
 
 export default new Vuex.Store({
   modules,
-  
+
   state: {
-    logged: false,
-    user: false,
+    cancellableSearchRequest: [],
     configuration: null,
-    staticPages: null,
-    USER_LEVEL_PROJECTS: null,
-    user_permissions: null,
+    isOnline: true,
     levelsPermissions: [],
-    messages: [],
     loader: {
       isLoading: false,
       message: 'En cours de chargement'
     },
-    cancellableSearchRequest: [],
-    reloadIntervalId: null
+    logged: false,
+    messages: [],
+    reloadIntervalId: null,
+    staticPages: null,
+    user: false,
+    USER_LEVEL_PROJECTS: null,
+    user_permissions: null,
   },
 
   mutations: {
+    SET_IS_ONLINE(state, payload) {
+      state.isOnline = payload;
+    },
     SET_USER(state, payload) {
       state.user = payload;
     },
diff --git a/src/views/Projects.vue b/src/views/Projects.vue
index 42640515..559f4b7e 100644
--- a/src/views/Projects.vue
+++ b/src/views/Projects.vue
@@ -6,14 +6,14 @@
 
     <div class="flex">
       <router-link
-        v-if="user && user.can_create_project && isOffline() != true"
+        v-if="user && user.can_create_project && isOnline"
         :to="{ name: 'project_create', params: { action: 'create' } }"
         class="ui green basic button"
       >
         <i class="plus icon" /> Créer un nouveau projet
       </router-link>
       <router-link
-        v-if="user && user.can_create_project && isOffline() != true"
+        v-if="user && user.can_create_project && isOnline"
         :to="{
           name: 'project_type_list',
         }"
@@ -162,12 +162,13 @@ export default {
     ...mapState([
       'configuration',
       'user',
-      'USER_LEVEL_PROJECTS'
+      'USER_LEVEL_PROJECTS',
+      'isOnline',
     ]),
     ...mapState('projects', [
       'projects',
       'count',
-      'filters'
+      'filters',
     ]),
     APPLICATION_NAME() {
       return this.$store.state.configuration.VUE_APP_APPLICATION_NAME;
@@ -226,9 +227,6 @@ export default {
       'GET_PROJECTS'
     ]),
 
-    isOffline() {
-      return navigator.onLine == false;
-    },
     refreshId() {
       //* change path of thumbnail to update image
       return '?ver=' + Math.random();
diff --git a/src/views/feature/Feature_detail.vue b/src/views/feature/Feature_detail.vue
index 66892238..b070952b 100644
--- a/src/views/feature/Feature_detail.vue
+++ b/src/views/feature/Feature_detail.vue
@@ -268,7 +268,7 @@
           </div>
 
           <div
-            v-if="permissions && permissions.can_create_feature && isOffline() !== true"
+            v-if="permissions && permissions.can_create_feature && isOnline"
             class="ui segment"
           >
             <form
@@ -433,7 +433,8 @@ export default {
   computed: {
     ...mapState([
       'user',
-      'USER_LEVEL_PROJECTS'
+      'USER_LEVEL_PROJECTS',
+      'isOnline',
     ]),
     ...mapState('projects', [
       'project'
@@ -541,9 +542,7 @@ export default {
     ...mapActions('feature', [
       'GET_PROJECT_FEATURES'
     ]),
-    isOffline() {
-      return navigator.onLine == false;
-    },
+
     pushNgo(link) {
       this.$router.push({
         name: 'details-signalement',
diff --git a/src/views/feature/Feature_edit.vue b/src/views/feature/Feature_edit.vue
index c54f912e..c3ec4d6a 100644
--- a/src/views/feature/Feature_edit.vue
+++ b/src/views/feature/Feature_edit.vue
@@ -74,7 +74,7 @@
             v-if="feature_type && feature_type.geom_type === 'point'"
             v-frag
           >
-            <p v-if="isOffline() !== true">
+            <p v-if="isOnline">
               <button
                 id="add-geo-image"
                 type="button"
@@ -229,12 +229,12 @@
         </div>
 
         <!-- Pièces jointes -->
-        <div v-if="isOffline() !== true">
+        <div v-if="isOnline">
           <div class="ui horizontal divider">
             PIÈCES JOINTES
           </div>
           <div
-            v-if="isOffline() !== true"
+            v-if="isOnline"
             id="formsets-attachment"
           >
             <FeatureAttachmentForm
@@ -256,7 +256,7 @@
         </div>
 
         <!-- Signalements liés -->
-        <div v-if="isOffline() !== true">
+        <div v-if="isOnline">
           <div class="ui horizontal divider">
             SIGNALEMENTS LIÉS
           </div>
@@ -373,7 +373,7 @@ export default {
   computed: {
     ...mapGetters(['permissions']),
     ...mapGetters('feature_type', ['feature_type']),
-    ...mapState(['user', 'USER_LEVEL_PROJECTS']),
+    ...mapState(['user', 'USER_LEVEL_PROJECTS', 'isOnline']),
     ...mapState('projects', ['project']),
     ...mapState('map', ['basemaps']),
     ...mapState('feature', [
@@ -486,9 +486,6 @@ export default {
   },
 
   methods: {
-    isOffline() {
-      return navigator.onLine == false;
-    },
     initForm() {
       if (this.currentRouteName === 'editer-signalement') {
         for (let key in this.feature) {
diff --git a/src/views/project/Project_detail.vue b/src/views/project/Project_detail.vue
index c2abc54a..bb0e7e21 100644
--- a/src/views/project/Project_detail.vue
+++ b/src/views/project/Project_detail.vue
@@ -87,7 +87,7 @@
                       user &&
                         permissions &&
                         permissions.can_view_project &&
-                        isOffline() !== true
+                        isOnline
                     "
                     id="subscribe-button"
                     class="ui button button-hover-green"
@@ -102,7 +102,7 @@
                     v-if="
                       permissions &&
                         permissions.can_update_project &&
-                        isOffline() !== true
+                        isOnline
                     "
                     :to="{ name: 'project_edit', params: { slug } }"
                     class="ui button button-hover-orange"
@@ -113,7 +113,7 @@
                     <i class="inverted grey pencil alternate icon" />
                   </router-link>
                   <a
-                    v-if="isProjectAdmin && isOffline() !== true"
+                    v-if="isProjectAdmin && isOnline"
                     id="delete-button"
                     class="ui button button-hover-red"
                     data-tooltip="Supprimer le projet"
@@ -152,7 +152,7 @@
         <div v-if="arraysOffline.length > 0">
           {{ arraysOffline.length }} modification<span v-if="arraysOffline.length>1">s</span> en attente
           <button
-            :disabled="isOffline()"
+            :disabled="!isOnline"
             class="ui fluid labeled teal icon button"
             @click="sendOfflineFeatures()"
           >
@@ -244,7 +244,7 @@
                       project &&
                         permissions &&
                         permissions.can_create_feature_type &&
-                        isOffline() !== true
+                        isOnline
                     "
                     :to="{
                       name: 'dupliquer-type-signalement',
@@ -277,7 +277,7 @@
                     v-frag
                   >
                     <a
-                      v-if="isProjectAdmin && isOffline() !== true"
+                      v-if="isProjectAdmin && isOnline"
                       class="
                         ui
                         compact
@@ -299,7 +299,7 @@
                         project &&
                           permissions &&
                           permissions.can_create_feature_type &&
-                          isOffline() !== true
+                          isOnline
                       "
                       :to="{
                         name: 'editer-symbologie-signalement',
@@ -326,7 +326,7 @@
                           type.is_editable &&
                           permissions &&
                           permissions.can_create_feature_type &&
-                          isOffline() !== true
+                          isOnline
                       "
                       :to="{
                         name: 'editer-type-signalement',
@@ -361,7 +361,7 @@
               v-if="
                 permissions &&
                   permissions.can_update_project &&
-                  isOffline() !== true
+                  isOnline
               "
               :to="{
                 name: 'ajouter-type-signalement',
@@ -377,7 +377,7 @@
               v-if="
                 permissions &&
                   permissions.can_update_project &&
-                  isOffline() !== true
+                  isOnline
               "
               class="
                 ui
@@ -414,7 +414,7 @@
                 IDGO &&
                   permissions &&
                   permissions.can_update_project &&
-                  isOffline() !== true
+                  isOnline
               "
               :to="{
                 name: 'catalog-import',
@@ -813,6 +813,7 @@ export default {
     ]),
     ...mapState([
       'configuration',
+      'isOnline',
     ]),
     ...mapState('feature_type', [
       'feature_types',
@@ -953,9 +954,6 @@ export default {
       }
       return url.replace(this.$store.state.configuration.BASE_URL, ''); //* remove duplicate /geocontrib
     },
-    isOffline() {
-      return navigator.onLine === false;
-    },
     isImporting(type) {
       if (this.importFeatureTypeData) {
         const singleImportData = this.importFeatureTypeData.find(
-- 
GitLab