Skip to content

Commit 901d17f

Browse files
authored
Merge pull request #2766 from metacpan/haarg/show-user-avatar
Show user avatar in account button
2 parents 8b7697b + dd63bd4 commit 901d17f

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

root/base.tx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5">
55
<title>[% $title || 'Search the CPAN' %] - metacpan.org</title>
6+
<link rel="preload" as="fetch" href="/account/login_status" crossorigin="anonymous" />
67
%% if $rss {
78
<link rel="alternate" type="application/rss+xml" title="[% $rss_title || 'RSS'; %]" href="[% $rss %]" />
89
%% }
@@ -125,9 +126,7 @@
125126
<form action="/account/logout" method="POST" id="metacpan-logout"></form>
126127
<li class="dropdown logged_in" style="display: none;">
127128
<button type="button" class="dropdown-toggle" data-toggle="dropdown">
128-
<!--Add the below back in once we have a way to store a logged in user's gravatar-->
129-
<!--<img class="logged-in-gravatar" src="[% gravatar_image() %]">-->
130-
<i class="fa fa-user button-fa-icon" aria-hidden="true"></i>
129+
<i class="fa fa-user button-fa-icon logged-in-icon" aria-hidden="true"></i>
131130
<i class="fas fa-chevron-down"></i>
132131
</button>
133132
<ul class="dropdown-menu">

root/static/js/cpan.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -484,27 +484,32 @@ function set_page_size(selector, storage_name) {
484484
});
485485
}
486486

487-
function processUserData() {
488-
489-
// Could do some fancy localStorage thing here
490-
// but as the user favs data is cached at fastly
491-
// not worth the effort yet
492-
493-
// TODO: get this working to save hits and 403's
494-
// if(document.cookie.match('metacpan_secure')) {
495-
// getFavDataFromServer();
496-
// } else {
497-
// // Can't be logged in
498-
// $('.logged_out').css('display', 'inline');
499-
// }
500-
501-
getFavDataFromServer();
487+
// poor man's RFC-6570 formatter
488+
function format_string(input_string, replacements) {
489+
const output_string = input_string.replace(
490+
/\{(\/?)(\w+)\}/g,
491+
(x, slash, placeholder) =>
492+
replacements.hasOwnProperty(placeholder) ?
493+
slash + replacements[placeholder] : ''
494+
);
495+
return output_string;
502496
}
503497

504498
function showUserData(user_data) {
505499
// User is logged in, so show it
506500
$('.logged_in').css('display', 'grid');
507501
$('.logged_placeholder').css('display', 'none');
502+
if (user_data.avatar) {
503+
const avatar = document.createElement('img');
504+
avatar.classList.add('logged-in-avatar');
505+
506+
// could use srcset
507+
avatar.src = format_string(user_data.avatar, {
508+
size: Math.floor(35 * window.devicePixelRatio)
509+
});
510+
avatar.crossorigin = 'anonymous';
511+
document.querySelector('.logged_in .logged-in-icon').replaceWith(avatar);
512+
}
508513

509514
// process users current favs
510515
$.each(user_data.faves, function(index, value) {
@@ -524,24 +529,20 @@ function showUserData(user_data) {
524529

525530
}
526531

527-
function getFavDataFromServer() {
528-
$.ajax({
529-
type: 'GET',
530-
url: '/account/login_status',
531-
success: function(databack) {
532-
if (databack.logged_in) {
533-
showUserData(databack);
532+
function processUserData() {
533+
fetch('/account/login_status')
534+
.then(res => res.json())
535+
.then(data => {
536+
if (data.logged_in) {
537+
showUserData(data);
534538
} else {
535539
$('.logged_out').css('display', 'inline');
536540
}
537541
$('.logged_placeholder').css('display', 'none');
538-
},
539-
error: function() {
540-
// Can't be logged in, should be getting 403
542+
})
543+
.catch(() => {
541544
$('.logged_out').css('display', 'inline');
542-
$('.logged_placeholder').css('display', 'none');
543-
}
544-
});
545+
});
545546
return true;
546547
}
547548

root/static/less/nav.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ nav {
108108
border: 0;
109109

110110
.button-fa-icon,
111-
.logged-in-gravatar {
111+
.logged-in-avatar {
112112
border: 1px solid @list-group-border;
113113
border-radius: 25px;
114114
cursor: pointer;

0 commit comments

Comments
 (0)