diff --git a/public/language/en_GB/success.json b/public/language/en_GB/success.json index d38b90c6fa..6b079680b5 100644 --- a/public/language/en_GB/success.json +++ b/public/language/en_GB/success.json @@ -1,5 +1,5 @@ { "topic-post": "You have successfully posted.", - "authentication-successful": "Authentication Successful" - + "authentication-successful": "Authentication Successful", + "settings-saved": "Settings saved!" } \ No newline at end of file diff --git a/public/src/forum/account.js b/public/src/forum/account.js index ba5338448a..6e947a9c4d 100644 --- a/public/src/forum/account.js +++ b/public/src/forum/account.js @@ -1,3 +1,7 @@ +'use strict'; + +/* globals define, ajaxify, app, utils, socket, translator*/ + define(['forum/accountheader'], function(header) { var Account = {}; @@ -8,75 +12,52 @@ define(['forum/accountheader'], function(header) { theirid = ajaxify.variables.get('theirid'), isFollowing = ajaxify.variables.get('isFollowing'); - $(document).ready(function() { - var username = $('.account-username').html(); - app.enterRoom('user/' + theirid); - - utils.addCommasToNumbers($('.account .formatted-number')); - utils.makeNumbersHumanReadable($('.account .human-readable-number')); - $('.user-recent-posts img').addClass('img-responsive'); - - var followBtn = $('#follow-btn'); - var unfollowBtn = $('#unfollow-btn'); - var chatBtn = $('#chat-btn'); - - if (yourid !== theirid && yourid !== "0") { - if (isFollowing) { - followBtn.addClass('hide'); - unfollowBtn.removeClass('hide'); - } else { - followBtn.removeClass('hide'); - unfollowBtn.addClass('hide'); - } - chatBtn.removeClass('hide'); - } else { - followBtn.addClass('hide'); - unfollowBtn.addClass('hide'); - chatBtn.addClass('hide'); - } + var username = $('.account-username').html(); + app.enterRoom('user/' + theirid); + + utils.addCommasToNumbers($('.account .formatted-number')); + utils.makeNumbersHumanReadable($('.account .human-readable-number')); + $('.user-recent-posts img').addClass('img-responsive'); + + + var isSelfOrNotLoggedIn = yourid === theirid || yourid === '0'; + $('#follow-btn').toggleClass('hide', isFollowing || isSelfOrNotLoggedIn); + $('#unfollow-btn').toggleClass('hide', !isFollowing || isSelfOrNotLoggedIn); + $('#chat-btn').toggleClass('hide', isSelfOrNotLoggedIn); + + $('#follow-btn').on('click', function() { + return toggleFollow('follow'); + }); - followBtn.on('click', function() { - socket.emit('user.follow', { - uid: theirid - }, function(err) { - if(err) { - return app.alertError('There was an error following' + username + '!'); - } - - followBtn.addClass('hide'); - unfollowBtn.removeClass('hide'); - app.alertSuccess('[[global:alert.follow, ' + username + ']]'); - }); - return false; - }); - - unfollowBtn.on('click', function() { - socket.emit('user.unfollow', { - uid: theirid - }, function(err) { - if(err) { - return app.alertError('There was an error unfollowing ' + username + '!'); - } - - followBtn.removeClass('hide'); - unfollowBtn.addClass('hide'); - app.alertSuccess('[[global:alert.unfollow, ' + username + ']]'); - }); - return false; - }); - - chatBtn.on('click', function() { - app.openChat(username, theirid); - }); - - socket.on('user.isOnline', Account.handleUserOnline); - - socket.emit('user.isOnline', theirid, Account.handleUserOnline); + $('#unfollow-btn').on('click', function() { + return toggleFollow('unfollow'); + }); + $('#chat-btn').on('click', function() { + app.openChat(username, theirid); }); + + socket.on('user.isOnline', handleUserOnline); + + socket.emit('user.isOnline', theirid, handleUserOnline); }; - Account.handleUserOnline = function(err, data) { + function toggleFollow(type) { + socket.emit('user.' + type, { + uid: ajaxify.variables.get('theirid') + }, function(err) { + if(err) { + return app.alertError(err.message); + } + + $('#follow-btn').toggleClass('hide', type === 'follow'); + $('#unfollow-btn').toggleClass('hide', type === 'unfollow'); + app.alertSuccess('[[global:alert.' + type + ', ' + $('.account-username').html() + ']]'); + }); + return false; + } + + function handleUserOnline(err, data) { var onlineStatus = $('.account-online-status'); if(parseInt(ajaxify.variables.get('theirid'), 10) !== parseInt(data.uid, 10)) { @@ -89,7 +70,7 @@ define(['forum/accountheader'], function(header) { .attr('data-original-title', translated); }); - }; + } return Account; }); diff --git a/public/src/forum/accountsettings.js b/public/src/forum/accountsettings.js index cf9fece08d..fcb4c52272 100644 --- a/public/src/forum/accountsettings.js +++ b/public/src/forum/accountsettings.js @@ -23,14 +23,14 @@ define(['forum/accountheader'], function(header) { socket.emit('user.saveSettings', settings, function(err) { if (err) { - return app.alertError('There was an error saving settings!'); + return app.alertError(err.message); } - app.alertSuccess('Settings saved!'); + app.alertSuccess('[[success:settings-saved]]'); }); return false; }); - + socket.emit('user.getSettings', function(err, settings) { for (var setting in settings) { if (settings.hasOwnProperty(setting)) {