From f0d921610085678fe7323bacfa16bc2156f15d7d Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Wed, 12 Apr 2017 20:44:18 +0200
Subject: [PATCH] [fix] Wallet view: Force refreshing of email subscription
 count [enh] Subscription: ask confirmation before deletion

---
 www/i18n/locale-en-GB.json                    |  1 +
 www/i18n/locale-en.json                       |  1 +
 www/js/controllers/wallet-controllers.js      |  4 +++
 www/plugins/es/i18n/locale-en-GB.json         |  8 +++++-
 www/plugins/es/i18n/locale-en.json            |  8 +++++-
 www/plugins/es/i18n/locale-fr-FR.json         |  6 ++++-
 .../controllers/subscription-controllers.js   | 26 ++++++++++++++-----
 7 files changed, 45 insertions(+), 9 deletions(-)

diff --git a/www/i18n/locale-en-GB.json b/www/i18n/locale-en-GB.json
index 4199a556c..25483f774 100644
--- a/www/i18n/locale-en-GB.json
+++ b/www/i18n/locale-en-GB.json
@@ -453,6 +453,7 @@
     "FIELD_ACCENT": "Commas and accent characters not allowed",
     "FIELD_NOT_NUMBER": "Value is not a number",
     "FIELD_NOT_INT": "Value is not an integer",
+    "FIELD_NOT_EMAIL": "Email adress not valid",
     "PASSWORD_NOT_CONFIRMED": "Must match previous password.",
     "SALT_NOT_CONFIRMED": "Must match previous identifier.",
     "SEND_IDENTITY_FAILED": "Error while trying to register.",
diff --git a/www/i18n/locale-en.json b/www/i18n/locale-en.json
index 398d6e101..3b8c961a0 100644
--- a/www/i18n/locale-en.json
+++ b/www/i18n/locale-en.json
@@ -453,6 +453,7 @@
     "FIELD_ACCENT": "Commas and accent characters not allowed",
     "FIELD_NOT_NUMBER": "Value is not a number",
     "FIELD_NOT_INT": "Value is not an integer",
+    "FIELD_NOT_EMAIL": "Email adress not valid",
     "PASSWORD_NOT_CONFIRMED": "Must match previous password.",
     "SALT_NOT_CONFIRMED": "Must match previous identifier.",
     "SEND_IDENTITY_FAILED": "Error while trying to register.",
diff --git a/www/js/controllers/wallet-controllers.js b/www/js/controllers/wallet-controllers.js
index d9cbe8d45..2ea90ed83 100644
--- a/www/js/controllers/wallet-controllers.js
+++ b/www/js/controllers/wallet-controllers.js
@@ -72,6 +72,10 @@ function WalletController($scope, $rootScope, $q, $ionicPopup, $timeout, $state,
           }
         });
     }
+    else {
+      // update view (to refresh profile and subscriptions)
+      $scope.updateView();
+    }
   });
 
   $scope.updateView = function() {
diff --git a/www/plugins/es/i18n/locale-en-GB.json b/www/plugins/es/i18n/locale-en-GB.json
index 26ef99db3..ccfa81a25 100644
--- a/www/plugins/es/i18n/locale-en-GB.json
+++ b/www/plugins/es/i18n/locale-en-GB.json
@@ -373,8 +373,14 @@
         "EMAIL": "Receive email notifications"
       }
     },
+    "CONFIRM": {
+      "DELETE_SUBSCRIPTION": "Are you sur you want to <b>delete this subscription</b>?"
+    },
     "ERROR": {
-      "LOAD_SUBSCRIPTIONS_FAILED": "Error loading online services"
+      "LOAD_SUBSCRIPTIONS_FAILED": "Error while loading online services",
+      "ADD_SUBSCRIPTION_FAILED": "Error while adding subscription",
+      "UPDATE_SUBSCRIPTION_FAILED": "Error during subscription update",
+      "DELETE_SUBSCRIPTION_FAILED": "Error while deleting subscription"
     },
     "MODAL_EMAIL": {
       "TITLE" : "Notification by email",
diff --git a/www/plugins/es/i18n/locale-en.json b/www/plugins/es/i18n/locale-en.json
index 26ef99db3..ccfa81a25 100644
--- a/www/plugins/es/i18n/locale-en.json
+++ b/www/plugins/es/i18n/locale-en.json
@@ -373,8 +373,14 @@
         "EMAIL": "Receive email notifications"
       }
     },
+    "CONFIRM": {
+      "DELETE_SUBSCRIPTION": "Are you sur you want to <b>delete this subscription</b>?"
+    },
     "ERROR": {
-      "LOAD_SUBSCRIPTIONS_FAILED": "Error loading online services"
+      "LOAD_SUBSCRIPTIONS_FAILED": "Error while loading online services",
+      "ADD_SUBSCRIPTION_FAILED": "Error while adding subscription",
+      "UPDATE_SUBSCRIPTION_FAILED": "Error during subscription update",
+      "DELETE_SUBSCRIPTION_FAILED": "Error while deleting subscription"
     },
     "MODAL_EMAIL": {
       "TITLE" : "Notification by email",
diff --git a/www/plugins/es/i18n/locale-fr-FR.json b/www/plugins/es/i18n/locale-fr-FR.json
index 1ea7537de..932951262 100644
--- a/www/plugins/es/i18n/locale-fr-FR.json
+++ b/www/plugins/es/i18n/locale-fr-FR.json
@@ -414,10 +414,14 @@
         "EMAIL": "Recevoir les notifications par email"
       }
     },
+    "CONFIRM": {
+      "DELETE_SUBSCRIPTION": "Etes-vous sûr de vouloir <b>supprimer cet abonnement</b> ?"
+    },
     "ERROR": {
       "LOAD_SUBSCRIPTIONS_FAILED": "Erreur lors du chargement des services en ligne",
       "ADD_SUBSCRIPTION_FAILED": "Erreur de l'envoi de l'abonnement",
-      "UPDATE_SUBSCRIPTION_FAILED": "Erreur de la mise à jour de l'abonnement"
+      "UPDATE_SUBSCRIPTION_FAILED": "Erreur de la mise à jour de l'abonnement",
+      "DELETE_SUBSCRIPTION_FAILED": "Erreur lors de la suppression de l'abonnement"
     },
     "MODAL_EMAIL": {
       "TITLE" : "Notification par email",
diff --git a/www/plugins/es/js/controllers/subscription-controllers.js b/www/plugins/es/js/controllers/subscription-controllers.js
index 72a93fe7e..f4bb17fac 100644
--- a/www/plugins/es/js/controllers/subscription-controllers.js
+++ b/www/plugins/es/js/controllers/subscription-controllers.js
@@ -77,7 +77,7 @@ function ViewSubscriptionsController($scope, $rootScope, $q, csWot, UIUtils, Mod
       .catch(function(err){
         UIUtils.loading.hide(10);
         if (err && err.ucode == 404) {
-          $scope.updateView({});
+          $scope.updateView([]);
           $scope.existing = false;
         }
         else {
@@ -122,7 +122,7 @@ function ViewSubscriptionsController($scope, $rootScope, $q, csWot, UIUtils, Mod
             UIUtils.loading.hide();
             $scope.updateView();
           })
-          .catch(UIUtils.onError('SUBSCRIPTION.ERROR.FAILED_ADD_SUBSCRIPTION'));
+          .catch(UIUtils.onError('SUBSCRIPTION.ERROR.ADD_SUBSCRIPTION_FAILED'));
       });
   };
 
@@ -153,15 +153,29 @@ function ViewSubscriptionsController($scope, $rootScope, $q, csWot, UIUtils, Mod
             UIUtils.loading.hide();
             $scope.updateView();
           })
-          .catch(UIUtils.onError('SUBSCRIPTION.ERROR.FAILED_ADD_SUBSCRIPTION'));
+          .catch(UIUtils.onError('SUBSCRIPTION.ERROR.UPDATE_SUBSCRIPTION_FAILED'));
       });
   };
 
-  $scope.deleteSubscription = function(record) {
+  $scope.deleteSubscription = function(record, confirm) {
     if (!record || !record.id) return;
 
-    esSubscription.record.remove(record.id);
-    $scope.removeFromUI(record);
+    if (!confirm) {
+      return UIUtils.alert.confirm('SUBSCRIPTION.CONFIRM.DELETE_SUBSCRIPTION')
+        .then(function(confirm) {
+          if (confirm) return $scope.deleteSubscription(record, confirm);
+        });
+    }
+
+    UIUtils.loading.show();
+    esSubscription.record.remove(record.id)
+      .then(function() {
+        $rootScope.walletData.subscriptions = $rootScope.walletData.subscriptions || {count: 1};
+        $rootScope.walletData.subscriptions.count--;
+        $scope.removeFromUI(record);
+        UIUtils.loading.hide();
+      })
+      .catch(UIUtils.onError('SUBSCRIPTION.ERROR.DELETE_SUBSCRIPTION_FAILED'));
   };
 
   $scope.removeFromUI = function(record) {
-- 
GitLab