Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display gold status in userbar( Closes #1915 ) #3485

Merged
merged 2 commits into from
Oct 24, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Display gold status in userbar( Closes #1915 )
- new module option: showGold (Display gilded icon if current user has
  gold status)
- new module function updateUserSpan (If the user has gold and would
  like to, display the gilded-icon in the user span.)
- basically borrow a bunch of JS from various functions to make
  everything work smoothly.
- new import needed for nice date on hover
- also comments for things :)
  • Loading branch information
dhaynespls committed Sep 23, 2016
commit 7758f9b7dc9b1a304238487049ee5828f58d3ff0
36 changes: 35 additions & 1 deletion lib/modules/showKarma.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $ } from '../vendor';
import { formatNumber, getUserInfo, loggedInUser } from '../utils';
import { formatDateDiff, formatNumber, getUserInfo, loggedInUser } from '../utils';

export const module = {};

Expand All @@ -24,6 +24,11 @@ module.options = {
value: true,
description: 'Use commas for large karma numbers',
},
showGold: {
type: 'boolean',
value: true,
description: 'Display gilded icon if current user has gold status',
},
};

module.go = async () => {
Expand All @@ -33,6 +38,9 @@ module.go = async () => {
} else {
formatPostKarma();
}
if (module.options.showGold.value) {
updateUserSpan();
}
}
};

Expand Down Expand Up @@ -62,6 +70,32 @@ async function updateKarmaDiv() {
}
}

// If the user has gold and would like to, display the gilded-icon in the user span.
async function updateUserSpan() {
// Get the user <span> located in the userbar
const userSpan = document.querySelector('#header-bottom-right .user');

if (userSpan) {
// Let's get the user's data.
const { data } = await getUserInfo();

// If the user has gold, display the icon and set the title text to the
// time remaining till it expires.
if (data.is_gold) {
const $gilded = $('<span>', { class: 'gilded-icon' });

if (data.gold_expiration) {
const today = new Date();
const expDate = new Date(data.gold_expiration * 1000); // s -> ms
$gilded.attr('title', `Until ${expDate.toDateString()} (${formatDateDiff(today, expDate)})`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: import and use formatDate(expDate) instead of expDate.toDateString() here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, I pulled that line from accountSwitcher (L231):

https://github.com/honestbleeps/Reddit-Enhancement-Suite/blob/master/lib/modules/accountSwitcher.js#L231

That may need to be modified as well to use formatDate(expDate) instead of toDateString().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, looks like that needs to be changed. You could open a new PR for that if you want, or I can take care of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take care of it in a separate PR, one sec.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--> #3486

}

// Prepend the icon to user <span> s/t it appears before the username.
$(userSpan).prepend($gilded);
}
}
}

function formatPostKarma(value) {
const container = document.querySelector('#header-bottom-right .user .userkarma');
value = (typeof value !== 'undefined') ? value : container.textContent;
Expand Down