From e1b3c14cbf39a7dfd56138fdfe4b2df8cfc7395a Mon Sep 17 00:00:00 2001
From: Thomas Citharel <tcit@tcit.fr>
Date: Wed, 9 Oct 2019 17:18:27 +0200
Subject: [PATCH] Prevent route changing when editing with changes

Close #197

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
---
 js/src/views/Event/Edit.vue | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/js/src/views/Event/Edit.vue b/js/src/views/Event/Edit.vue
index 42c52c77f..cc3dd9c3d 100644
--- a/js/src/views/Event/Edit.vue
+++ b/js/src/views/Event/Edit.vue
@@ -266,6 +266,9 @@ import { ICurrentUser } from '@/types/current-user.model';
       query: TAGS,
     },
   },
+  beforeRouteLeave(to, from, next) {
+    this.confirmGoElsewhere(() => next());
+  },
 })
 export default class EditEvent extends Vue {
   @Prop({ type: Boolean, default: false }) isUpdate!: boolean;
@@ -499,9 +502,9 @@ export default class EditEvent extends Vue {
   /**
    * Confirm cancel
    */
-  confirmGoBack() {
+  confirmGoElsewhere(callback) {
     if (!this.isEventModified) {
-      return this.$router.go(-1);
+      return callback();
     }
     const title: string = this.isUpdate ?
             this.$t('Cancel edition') as string :
@@ -519,10 +522,17 @@ export default class EditEvent extends Vue {
       cancelText: this.$t('Continue editing') as string,
       type: 'is-warning',
       hasIcon: true,
-      onConfirm: () => this.$router.go(-1),
+      onConfirm: callback,
     });
   }
 
+  /**
+   * Confirm cancel
+   */
+  confirmGoBack() {
+    this.confirmGoElsewhere(() => this.$router.go(-1));
+  }
+
   get isEventModified(): boolean {
     return JSON.stringify(this.event.toEditJSON()) !== JSON.stringify(this.unmodifiedEvent);
   }