* SASS formatting in action. * Unnested .sidebar-nav-element in SASS * Created the <TagsFollowed /> component. * Mini refactor. * Left side bar uses <TagsFollowed /> component now. * Accomodating InstantClick * Small refactor and added some comments. * The homepage webpacker pack only needs to be loaded once. * Added tests for the <TagsFollowed /> component. * Modified the receive InstantClick callback as per their docs. * Reverted the data-no-instant on he homepage bundle. It seems to cause issues in certain scenarios. * Just some tweaks to <TagsFollowed /> component. * Removed CSS for general img tag in context of sidebar as it does not appear to be used. * Fixed copy paste error from JSX to HTML. * Fixed <img /> styles for arbitrary sponsorship widget. * Fixed a regression that a Capybara test caught. * Fixed user followed tags on home page tests. * Fixed logic for loading user data on homepage.
98 lines
2.7 KiB
JavaScript
98 lines
2.7 KiB
JavaScript
import { h, render } from 'preact';
|
|
|
|
/* global userData */
|
|
|
|
function toggleListingsMinimization() {
|
|
if (document.body.classList.contains('config_minimize_newest_listings')) {
|
|
// Un-minimize
|
|
localStorage.setItem('config_minimize_newest_listings', 'no');
|
|
document.body.classList.remove('config_minimize_newest_listings');
|
|
} else {
|
|
// Minimize
|
|
localStorage.setItem('config_minimize_newest_listings', 'yes');
|
|
document.body.classList.add('config_minimize_newest_listings');
|
|
}
|
|
}
|
|
|
|
const sidebarListingsMinimizeButton = document.getElementById(
|
|
'sidebar-listings-widget-minimize-button',
|
|
);
|
|
if (sidebarListingsMinimizeButton) {
|
|
sidebarListingsMinimizeButton.addEventListener(
|
|
'click',
|
|
toggleListingsMinimization,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Renders tags followed in the left side bar of the homepage.
|
|
*
|
|
* @param {HTMLElement} tagsFollowedContainer DOM element to render tags followed.
|
|
* @param {object} user The currently logged on user, null if not logged on.
|
|
*/
|
|
function renderTagsFollowed(tagsFollowedContainer, user = userData()) {
|
|
if (user === null) {
|
|
return;
|
|
}
|
|
|
|
// Only render if a user is logged on.
|
|
import('../leftSidebar/TagsFollowed').then(({ TagsFollowed }) => {
|
|
const { followed_tags } = user; // eslint-disable-line camelcase
|
|
const followedTags = JSON.parse(followed_tags);
|
|
|
|
// This should be done server-side potentially
|
|
// sort tags by descending weight, descending popularity and name
|
|
followedTags.sort((tagA, tagB) => {
|
|
return (
|
|
tagB.points - tagA.points ||
|
|
tagB.hotness_score - tagA.hotness_score ||
|
|
tagA.name.localeCompare(tagB.name)
|
|
);
|
|
});
|
|
|
|
render(<TagsFollowed tags={followedTags} />, tagsFollowedContainer);
|
|
});
|
|
}
|
|
|
|
let waitingForDataLoad = setTimeout(function dataLoadedCheck() {
|
|
const { user = null, userStatus } = document.body.dataset;
|
|
|
|
if (userStatus === 'logged-out') {
|
|
// Nothing to do, the user is not logged on.
|
|
return;
|
|
}
|
|
|
|
if (userStatus === 'logged-in' && user !== null) {
|
|
// We have user data, render followed tags.
|
|
clearTimeout(waitingForDataLoad);
|
|
renderTagsFollowed(document.getElementById('sidebar-nav-followed-tags'));
|
|
return;
|
|
}
|
|
|
|
// No user data yet for the logged on user, poll once more.
|
|
waitingForDataLoad = setTimeout(dataLoadedCheck, 40);
|
|
}, 40);
|
|
|
|
InstantClick.on('receive', (_url, body, title) => {
|
|
if (document.body.dataset.userStatus !== 'logged-in') {
|
|
// Nothing to do, the user is not logged on.
|
|
return false;
|
|
}
|
|
|
|
const tagsFollowedContainer = body.querySelector(
|
|
'#sidebar-nav-followed-tags',
|
|
);
|
|
|
|
if (!tagsFollowedContainer) {
|
|
// Not on the homepage, so nothing to do.
|
|
return false;
|
|
}
|
|
|
|
renderTagsFollowed(tagsFollowedContainer);
|
|
|
|
return {
|
|
body,
|
|
title,
|
|
};
|
|
});
|
|
InstantClick.init();
|