Skip to content

Commit

Permalink
Basic popup code
Browse files Browse the repository at this point in the history
  • Loading branch information
eritbh committed Mar 23, 2022
1 parent 9574026 commit 92a0399
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion extension/data/modules/modnotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,64 @@ function modnotes () {
}
}

/**
* Creates a mod note popup for the given information.
* @param {object} data Data associated with the popup
* @param {string} data.user Name of the relevant user
* @param {string} data.subreddit Name of the relevant subreddit
*/
function createModNotesPopup ({
user,
subreddit,
notes,
}) {
const $popup = TBui.popup({
title: `Mod notes for /u/${user} in /r/${subreddit}`,
tabs: [{
title: 'All Activity',
content: `
<p class="error">loading...</p>
`,
footer: TBui.actionButton('dab', 'tb-modnote-dab'),
}],
cssClass: 'tb-modnote-popup',
});
$popup.attr('data-user', user);
$popup.attr('data-subreddit', subreddit);

updateModNotesPopup($popup, {
notes,
});

return $popup;
}

function updateModNotesPopup ($popup, {
notes,
}) {
const $content = $popup.find('.tb-window-content');
$content.empty();
if (!notes) {
$content.append(`
<p class="error">
Error fetching mod notes
</p>
`);
return;
}

if (!notes.length) {
$content.append(`
<p>
No notes
</p>
`);
return;
}

$content.append($('<pre>').text(JSON.stringify(notes, null, 2)));
}

self.init = () => {
TBListener.on('author', async e => {
const subreddit = e.detail.data.subreddit.name;
Expand Down Expand Up @@ -126,9 +184,32 @@ function modnotes () {
user: e.detail.data.author,
subreddit: e.detail.data.subreddit.name,
});
$badge.on('click', () => {
$badge.on('click', async clickEvent => {
// TODO: open popup with more information for this user
self.info(`clicked badge for /u/${author} in /r/${subreddit}`);

// Fetch all usernotes for this user
let notes;
try {
// TODO: store these somewhere persistent so they can be
// added to later if the user wants to load more
notes = await TBApi.getModNotes(subreddit, author);
} catch (error) {
self.error(`Error fetching mod notes for /u/${author} in /r/${subreddit}`, error);
}

// Create, position, and display popup
const positions = TBui.drawPosition(clickEvent);
createModNotesPopup({
user: author,
subreddit,
notes,
})
.css({
top: positions.topPosition,
left: positions.leftPosition,
})
.appendTo($('body'));
});
$badge.appendTo($target);
}
Expand Down

0 comments on commit 92a0399

Please sign in to comment.