Struggling updating user preference from portal.

guill3
Giga Contributor

Hi, community

 

I'm having a bad time trying to update a user preference from the Service portal, basically what I need to do is when a user clicks on a link labeled "Dismiss" it  will update a user preference to false, and the element will not appear again until the user changes the value of this preference.

 

On Server side I call the gs object and then use the save preference method to save the new value, however, this is not working, this is triggered after the hideTour function is executed on the client-side, is there another way to update data from client to server-side please let me know.

 

The user preference is called "hr-tour-hide" when set to false will make the Banner/header disappear. Before asking why we are not using the OOB guided tour auto-trigger... this is was disabled because for new users some other panels/modals/pop-ups etc will go first and after that then the guided tour will be displayed, by enabling the OOB will appear prior the mentioned pop-ups.

Here is the widget code:

 

HMTL :

<nav ng-if="data.tour" ng-hide="data.hideNav"  class="navbar navbar-default">
  <div class="navbar-nav">
    ${New to this portal?} ${Take the}
    <button id="tour" ng-if="data.tour" class="btn btn-default" ng-click="data.tour.clicked()">
      ${Guided Tour}!
    </button>
  </div>
  <div id="close-button">
    <!--<i ng-click="hideTour()" class="fa fa-times" aria-hidden="true"></i>-->
    <span ng-click="hideTour()" >${Dismiss}</span>
  </div>
</nav>

Server script:

 

(function() {
	/* populate the 'data' object */
	/* e.g., data.table = $sp.getValue('table'); */
	data.tour = "";
	data.hideNav = (gs.getUser().getPreference("hr-tour-hide") == "false");
 var currentUser = gs.getUser(); 
	
	if(input && input.action == "set-pref"){
		currentUser.savePreference('hr-tour-hide', 'true');
	}

})();

 

Client controller:

 

function($scope,spGtd, spUtil) {
	/* widget controller */
	var c = this;
	spGtd.getToursForPage();
	
	$scope.hideTour = function(){
		c.data.hideNav = true;
		c.server.get({action: "set-pref"}).then(function(){
			c.data.action = undefined;
			
		})
	}

	$scope.$on('sp-menu-update-tours', function(event, tours) {
		if (tours.length > 0) {
			//Code Rolled back for IE 11 support.  find function not supported.
			/*var tour = tours.find(function(tour){
				return tour.name == "New User Tour";
			})*/
			var tour = "";
			for(var x = 0; x < tours.length; x++){
				if(tours[x].name == "HR Portal - Guided Tour"){
					tour = tours[x];
				}
			}
			if(tour){
				c.data.tour = {
					title: tour.name,
					id: tour.id,
					clicked: function() {
						spGtd.launch(tour.id);
					}
				}
			}
		}
	});
}
1 ACCEPTED SOLUTION

guill3
Giga Contributor

Actually I did a mistake, the preference should be set to "false" because if it true will display the element  😄 . my bad.

View solution in original post

2 REPLIES 2

Mike Patel
Tera Sage

try changing server side script to below just remove quotes

currentUser.savePreference('hr-tour-hide', true);

guill3
Giga Contributor

Actually I did a mistake, the preference should be set to "false" because if it true will display the element  😄 . my bad.