diff --git a/control/content/controllers/content.reward.controller.js b/control/content/controllers/content.reward.controller.js index ae75f46..76b7eb4 100644 --- a/control/content/controllers/content.reward.controller.js +++ b/control/content/controllers/content.reward.controller.js @@ -15,6 +15,7 @@ }; ContentReward.isInserted = false; ContentReward.masterData = null; + ContentReward.itemSaved = false; updateMasterItem(ContentReward.item); ContentReward.bodyWYSIWYGOptions = { plugins: 'advlist autolink link image lists charmap print preview', @@ -129,11 +130,12 @@ var data = newObj; data.appId = context.appId; data.loyaltyUnqiueId = context.instanceId; - data.userToken = ContentReward.currentLoggedInUser.userToken; - data.auth = ContentReward.currentLoggedInUser.auth; + data.userToken = ContentReward.currentLoggedInUser && ContentReward.currentLoggedInUser.userToken; + data.auth = ContentReward.currentLoggedInUser && ContentReward.currentLoggedInUser.auth; var success = function (result) { console.info('Saved data result: ', result); updateMasterItem(newObj); + ContentReward.itemSaved = true; ContentReward.item.deepLinkUrl = Buildfire.deeplink.createLink({id: result._id}); ContentReward.item = Object.assign(ContentReward.item, result); ContentReward.isInserted = true; @@ -148,6 +150,7 @@ } , error = function (err) { ContentReward.isInserted = false; + ContentReward.itemSaved = true; console.error('Error while saving data : ', err); }; LoyaltyAPI.addReward(data).then(success, error); @@ -173,9 +176,11 @@ $scope.$digest(); var success = function (result) { console.info('Saved data result: ', result); + ContentReward.itemSaved = true; } , error = function (err) { console.error('Error while saving data : ', err); + ContentReward.itemSaved = true; }; LoyaltyAPI.updateReward(data).then(success, error); }; @@ -229,6 +234,7 @@ if (isUnchanged(newObj)) { return; } + ContentReward.itemSaved = false; if (tmrDelay) { clearTimeout(tmrDelay); } diff --git a/control/content/templates/reward.html b/control/content/templates/reward.html index 4ac5492..2093b08 100644 --- a/control/content/templates/reward.html +++ b/control/content/templates/reward.html @@ -49,7 +49,7 @@
diff --git a/test/widget/widget.home.controller.spec.js b/test/widget/widget.home.controller.spec.js index 0f3ee16..b761309 100644 --- a/test/widget/widget.home.controller.spec.js +++ b/test/widget/widget.home.controller.spec.js @@ -209,6 +209,9 @@ describe('Unit : loyaltyPluginWidget Plugin widget.home.controller.js', function var points = 5, callback = function(e, data){ } WidgetHome.loyaltyRewards = [1,2,3]; + WidgetHome.context = { + instanceId : 'abcde1234' + }; $rootScope.$broadcast('REWARD_ADDED', callback); }); it('should invoke GOTO_HOME when point have some values', function () { diff --git a/widget/controllers/widget.home.controller.js b/widget/controllers/widget.home.controller.js index ea4ae9c..6f60ea0 100644 --- a/widget/controllers/widget.home.controller.js +++ b/widget/controllers/widget.home.controller.js @@ -188,7 +188,17 @@ * This event listener is bound for "REWARD_ADDED" event broadcast */ WidgetHome.listeners['REWARD_ADDED'] = $rootScope.$on('REWARD_ADDED', function (e, item) { - WidgetHome.loyaltyRewards.unshift(item); + var successLoyaltyRewards = function (result) { + WidgetHome.loyaltyRewards = result; + if (!WidgetHome.loyaltyRewards) + WidgetHome.loyaltyRewards = []; + } + , errorLoyaltyRewards = function (err) { + if (err && err.code !== STATUS_CODE.NOT_FOUND) { + console.error('Error while getting data loyaltyRewards--------------------------------------', err); + } + }; + LoyaltyAPI.getRewards(WidgetHome.context.instanceId).then(successLoyaltyRewards, errorLoyaltyRewards); }); /** @@ -368,9 +378,13 @@ }); WidgetHome.listeners['REWARD_UPDATED'] = $rootScope.$on('REWARD_UPDATED', function (e, item, index) { - if (index == 0 || index) { - WidgetHome.loyaltyRewards[index] = item; - if($scope.$$phase) $scope.$digest(); + if (item && WidgetHome.loyaltyRewards && WidgetHome.loyaltyRewards.length) { + WidgetHome.loyaltyRewards.some(function (reward, index) { + if (reward._id == item._id) { + WidgetHome.loyaltyRewards[index] = item; + return true; + } + }) } });