Skip to content

Instantly share code, notes, and snippets.

@singerxt
Created February 19, 2015 18:50
Show Gist options
  • Save singerxt/79e476b4ee4d649037a3 to your computer and use it in GitHub Desktop.
Save singerxt/79e476b4ee4d649037a3 to your computer and use it in GitHub Desktop.
profile page
/**
* Gigya Profile Module
*
* 'use strict';
* // variables to be ignored by JSHint'
*
* define([optional dependencies], function(dependencies reference in the same order) {
* // You can return any type of objects, functions or variables
* return {
* foo: function() {...}
* }
* });
*/
'use strict';
/* global define, gigya, confirm */
define(['modules/gigya/common', 'jquery'], function (gigyaCommon, $) {
var gigyaProfile = window.gigyaProfile = {
gigyaUID: false,
init: function() {
var self = this;
gigyaCommon.ready(function(gigya) {
self.gigya = gigya;
self.initAddSocialAccount();
self.initCommon();
self.addEventListeners();
gigyaProfile.gigya.accounts.getAccountInfo({callback: self.callbacks.accounts.getAccountInfo.callback});
});
},
initCommon: function() {
gigyaCommon.isOnProfilePage = true;
},
initAddSocialAccount: function() {
var addConnectionLink = $('.add-social-connection');
addConnectionLink.on('click', function () {
var params = {
enabledProviders: 'facebook, twitter, googleplus'
};
gigya.socialize.showAddConnectionsUI(params);
});
},
addEventListeners: function(){
document.getElementById('gigya-account-delete').addEventListener('click', function(e) {
var xhr;
e.preventDefault();
if(true === confirm('Are you sure you want to delete your user?')) {
xhr = new XMLHttpRequest();
xhr.open('GET', '/profile/delete?UID=' + gigyaProfile.gigyaUID, true);
xhr.onreadystatechange = function () {
var response;
if (xhr.readyState === 4) {
response = JSON.parse(xhr.response);
response = response[0] || response;
gigyaProfile.callbacks.ajax.deleteAccount(response);
}
};
xhr.send(null);
}
});
$('#gigya-account-save').on('click', function(e){
e.preventDefault();
console.log('gigya-account-save');
/* jslint camelcase:false */
gigyaProfile.gigya.accounts.setAccountInfo({
data: {
newsletter_opt_in: $('.gigya-newsletter').prop('checked'),
newsletter_19_opt_in: $('.gigya-newsletter-19-entertainment').prop('checked'),
newsletter_fm_opt_in: $('.gigya-newsletter-fremantle').prop('checked')
},
callback: function(response) {
console.log(response);
if(response.errorCode === 0){
$('.profile-updates')
.text('Your settings have been saved.')
.css('display','block');
setTimeout(function(){
$('.profile-updates').hide();
},5000);
} else {
$('.profile-updates')
.text(response.errorMessage)
.show();
}
}
});
/* jslint camelcase:true */
});
},
callbacks: {
accounts: {
getAccountInfo: {
callback: function(response) {
var user = response.profile;
var data = response.data;
var profile = gigyaCommon.getElement('.gigya-profile');
gigyaProfile.gigyaUID = response.UID;
gigyaCommon.getElement('.gigya-username', profile).forEach(function(username) {
username.textContent = user.nickname || ((user.firstName || '') + ' ' + (user.lastName || ''));
});
gigyaCommon.getElement('.gigya-email', profile).forEach(function(email) {
email.textContent = user.email;
});
gigyaCommon.getElement('.gigya-avatar', profile).forEach(function(avatar) {
var photoURL = user.photoURL;
if (photoURL) {
avatar.setAttribute('src', photoURL);
} else {
avatar.parentNode.removeChild(avatar);
}
});
gigyaCommon.getElement('.gigya-newsletter', profile).forEach(function(newsletter) {
/* jslint camelcase:false */
$(newsletter).prop('checked', data.newsletter_opt_in);
/* jslint camelcase:true */
});
gigyaCommon.getElement('.gigya-newsletter-19-entertainment', profile).forEach(function(newsletter19) {
/* jslint camelcase:false */
$(newsletter19).prop('checked', data.newsletter_19_opt_in);
/* jslint camelcase:true */
});
gigyaCommon.getElement('.gigya-newsletter-fremantle', profile).forEach(function(newsletterFremantle) {
/* jslint camelcase:false */
$(newsletterFremantle).prop('checked', data.newsletter_fm_opt_in);
/* jslint camelcase:true */
});
}
}
},
ajax: {
deleteAccount: function (response) {
var errorCode = parseInt(response.errorCode, 10);
this.gigya = gigya;
if (errorCode === 0) {
gigyaProfile.gigya.socialize.logout({
callback: gigyaCommon.callbacks.socialize.logout.callback
});
} else {
gigyaCommon.setError((response.errorDetails || response.errorMessage), errorCode);
}
}
}
}
};
return gigyaProfile;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment