Skip to content

Commit

Permalink
Merge pull request #530 from jofomah/bug/cant-save-app-config-while-o…
Browse files Browse the repository at this point in the history
…ffline

Bug/cant save app config while offline
  • Loading branch information
jofomah committed Sep 1, 2014
2 parents 86716b1 + a6153fa commit a2cc020
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 26 deletions.
3 changes: 3 additions & 0 deletions app/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -724,5 +724,8 @@
},
"selectSendingWard": {
"message": "Select Sending Ward"
},
"lgaUpdateFailed": {
"message": "LGA update failed, internet connection needed to update LGA list."
}
}
88 changes: 63 additions & 25 deletions app/scripts/controllers/app-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
});
}
};

});
7 changes: 6 additions & 1 deletion app/scripts/services/app-config-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
});
});
};

Expand Down

0 comments on commit a2cc020

Please sign in to comment.