From cd42ef313229de29f9a7f4a0cac6f594d1b34ea0 Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Sun, 15 Oct 2017 17:58:54 -0500 Subject: [PATCH] Don't import files that didn't decrypt properly --- Gruntfile.js | 2 +- .../services/directives/views/accountMenu.js | 40 ++++++++++++++----- .../app/services/filters/sortBy.js | 1 - .../directives/account-menu.html.haml | 2 +- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index cd3c452c8..7aecbdebc 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -5,7 +5,7 @@ module.exports = function(grunt) { watch: { haml: { files: ['app/assets/templates/**/*.haml'], - tasks: ['newer:haml', 'ngtemplates', 'concat'], + tasks: ['newer:haml', 'ngtemplates', 'concat:app', 'babel', 'browserify', 'concat:dist'], options: { spawn: false, }, diff --git a/app/assets/javascripts/app/services/directives/views/accountMenu.js b/app/assets/javascripts/app/services/directives/views/accountMenu.js index 96a7a70ff..6a5bfb039 100644 --- a/app/assets/javascripts/app/services/directives/views/accountMenu.js +++ b/app/assets/javascripts/app/services/directives/views/accountMenu.js @@ -185,15 +185,24 @@ class AccountMenu { $scope.importData.loading = true; // allow loading indicator to come up with timeout $timeout(function(){ - $scope.importJSONData(data, password, function(response){ + $scope.importJSONData(data, password, function(response, errorCount){ $timeout(function(){ $scope.importData.loading = false; $scope.importData = null; - if(!response) { - alert("There was an error importing your data. Please try again."); - } else { - alert("Your data was successfully imported.") - } + + // Update UI before showing alert + setTimeout(function () { + if(!response) { + alert("There was an error importing your data. Please try again."); + } else { + if(errorCount > 0) { + var message = `Import complete. ${errorCount} items were not imported because there was an error decrypting them. Make sure the password is correct and try again.`; + alert(message); + } else { + alert("Your data was successfully imported.") + } + } + }, 10); }) }) }) @@ -225,7 +234,7 @@ class AccountMenu { } $scope.importJSONData = function(data, password, callback) { - var onDataReady = function() { + var onDataReady = function(errorCount) { var items = modelManager.mapResponseItemsToLocalModels(data.items); items.forEach(function(item){ item.setDirty(true); @@ -233,7 +242,9 @@ class AccountMenu { item.markAllReferencesDirty(); }) - syncManager.sync(callback, {additionalFields: ["created_at", "updated_at"]}); + syncManager.sync((response) => { + callback(response, errorCount); + }, {additionalFields: ["created_at", "updated_at"]}); }.bind(this) if(data.auth_params) { @@ -244,8 +255,19 @@ class AccountMenu { data.items.forEach(function(item){ item.enc_item_key = null; item.auth_hash = null; + }); + + var errorCount = 0; + // Don't import items that didn't decrypt properly + data.items = data.items.filter(function(item){ + if(item.errorDecrypting) { + errorCount++; + return false; + } + return true; }) - onDataReady(); + + onDataReady(errorCount); } catch (e) { console.log("Error decrypting", e); diff --git a/app/assets/javascripts/app/services/filters/sortBy.js b/app/assets/javascripts/app/services/filters/sortBy.js index beced415e..c3902604b 100644 --- a/app/assets/javascripts/app/services/filters/sortBy.js +++ b/app/assets/javascripts/app/services/filters/sortBy.js @@ -35,7 +35,6 @@ angular.module('app.frontend') } items = items || []; - console.log("Sorting", items.length, "items"); return items.sort(function(a, b){ return sortValueFn(a, b); }) diff --git a/app/assets/templates/frontend/directives/account-menu.html.haml b/app/assets/templates/frontend/directives/account-menu.html.haml index b07e95635..e1d8f0691 100644 --- a/app/assets/templates/frontend/directives/account-menu.html.haml +++ b/app/assets/templates/frontend/directives/account-menu.html.haml @@ -149,7 +149,7 @@ %input{"type" => "radio", "ng-model" => "archiveFormData.encrypted", "ng-value" => "false", "ng-change" => "archiveFormData.encrypted = false"} Decrypted - %a.block.mt-5{"ng-click" => "downloadDataArchive()", "ng-class" => "{'mt-5' : !user}"} Download Data Archive + %a.block.mt-5{"ng-click" => "downloadDataArchive()", "ng-class" => "{'mt-5' : !user}"} Export Data Archive %label.block.mt-5 %input{"type" => "file", "style" => "display: none;", "file-change" => "->", "handler" => "importFileSelected(files)"}