diff --git a/app/_locales/en/messages.json b/app/_locales/en/messages.json index 6e337f46..082af12e 100644 --- a/app/_locales/en/messages.json +++ b/app/_locales/en/messages.json @@ -724,5 +724,8 @@ }, "selectSendingWard": { "message": "Select Sending Ward" + }, + "lgaUpdateFailed": { + "message": "LGA update failed, internet connection needed to update LGA list." } } diff --git a/app/scripts/controllers/app-config.js b/app/scripts/controllers/app-config.js index b999baaf..bfcf1eb0 100644 --- a/app/scripts/controllers/app-config.js +++ b/app/scripts/controllers/app-config.js @@ -192,6 +192,10 @@ angular.module('lmisChromeApp') .controller('EditAppConfigCtrl', function($scope, fixtureLoaderService, locationService, $rootScope, appConfigService, growl, $log, i18n, $state, appConfig, ccuProfilesGroupedByCategory, productProfilesGroupedByCategory, utility, alertFactory, $filter) { $scope.spaceOutUpperCaseWords = utility.spaceOutUpperCaseWords; + var oldLgas = []; + if (utility.has(appConfig.facility, 'selectedLgas')) { + oldLgas = angular.copy(appConfig.facility.selectedLgas); + } $scope.stockCountIntervals = appConfigService.stockCountIntervals; $scope.weekDays = appConfigService.weekDays; $scope.ccuProfilesCategories = Object.keys(ccuProfilesGroupedByCategory); @@ -271,34 +275,68 @@ angular.module('lmisChromeApp') $scope.preSelectLgaCheckBox = utility.castArrayToObject($scope.appConfig.facility.selectedLgas, '_id'); }; - $scope.save = function() { - $scope.isSaving = true; - var nearbyLgas = $scope.appConfig.facility.selectedLgas - .map(function(lga) { - if (lga._id) { - return lga._id; - } + var isSameLgas = function(old, recent) { + if (old.length !== recent.length) { + return false; + } + var hasOddElem = true; + recent.forEach(function(rLga) { + var similar = old.filter(function(oLga) { + return rLga._id === oLga._id; }); - fixtureLoaderService.setupWardsAndFacilitesByLgas(nearbyLgas) + if (similar.length === 0) { + hasOddElem = false; + } + }); + return hasOddElem; + }; + + var saveAppConfig = function() { + appConfigService.setup($scope.appConfig) + .then(function(result) { + if (typeof result !== 'undefined') { + $scope.appConfig = result; + alertFactory.success(i18n('appConfigSuccessMsg')); + $state.go('home.index.home.mainActivity'); + } else { + growl.error(i18n('appConfigFailedMsg')); + } + }) + .catch(function(reason) { + if (utility.has(reason, 'type') && reason.type === 'SAVED_NOT_SYNCED') { + alertFactory.success(i18n('appConfigSuccessMsg')); + $state.go('home.index.home.mainActivity'); + console.info('not synced'); + } else { + growl.error(i18n('appConfigFailedMsg')); + console.error(reason); + } + }) .finally(function() { - appConfigService.setup($scope.appConfig) - .then(function(result) { - if (typeof result !== 'undefined') { - $scope.appConfig = result; - alertFactory.success(i18n('appConfigSuccessMsg')); - $state.go('home.index.home.mainActivity'); - } else { - growl.error(i18n('appConfigFailedMsg')); - } - }) - .catch(function(reason) { - growl.error(i18n('appConfigFailedMsg')); - $log.error(reason); - }) - .finally(function() { - $scope.isSaving = false; - }); + $scope.isSaving = false; }); }; + $scope.save = function() { + $scope.isSaving = true; + if (isSameLgas(oldLgas, $scope.appConfig.facility.selectedLgas)) { + saveAppConfig(); + } else { + var nearbyLgas = $scope.appConfig.facility.selectedLgas + .map(function(lga) { + if (lga._id) { + return lga._id; + } + }); + fixtureLoaderService.setupWardsAndFacilitesByLgas(nearbyLgas) + .then(function(res) { + saveAppConfig(); + }) + .catch(function(err) { + growl.error(i18n('lgaUpdateFailed')); + $scope.isSaving = false; + }); + } + }; + }); diff --git a/app/scripts/services/app-config-service.js b/app/scripts/services/app-config-service.js index a10aa29c..2a1ba0a7 100644 --- a/app/scripts/services/app-config-service.js +++ b/app/scripts/services/app-config-service.js @@ -48,7 +48,12 @@ angular.module('lmisChromeApp').service('appConfigService', function($q, storage this.setup = function(appConfig) { return saveAppConfig(appConfig) .then(function(appCfg) { - return syncService.syncUpRecord(storageService.APP_CONFIG, appCfg); + return syncService.syncUpRecord(storageService.APP_CONFIG, appCfg) + .catch(function(err){ + console.error(err); + //TODO: resolve properly. + return { type: 'SAVED_NOT_SYNCED', error: err, appConfig: appConfig }; + }); }); };