docbrown/app/javascript/packs/homePage.jsx
ludwiczakpawel 73e560f7a2
Updating navigation (especially mobile) (#10424)
* .

* .

* .

* .

* .

* specs

* specs

* .

* .

* merge

* moving cheese around

* layout

* .

* merge

* Fixed invalid selector for search.

* Fixed issue with URL not updating after a search.

* do things better

* kinda merge

* whoops

* whoops

* .

* .

* im a backend developer

* tooltips 1.0.1

* tooltips 1.0.1

* fix

* specs

* .

* reduce the spacing

* schema drop

* reverts

* revert

* reverts

* revert

* drop unused nav

* drop unused nav

* drop unused nav

* drop unused nav

* typos

* paths fixes

* speeding up JS

* js

* search

* Reworked initializeTopBarIcons.js

* Reworked initializeNavigation.js

* Small refactor and updated exported function API docs.

* Fixed a bug I created when refactoring the top navigation icons.

* sidebar nav links

* spec

* Fixed issues with logged out experience for top nav icons.

* Small refactor of top nav icons.

* Small refactor and added some API docs.

* Added @testing-library/dom

* Added tests for top navigation utilities.

* spec

* spec

Co-authored-by: Nick Taylor <nick@dev.to>
2020-12-08 15:59:21 +01:00

110 lines
3.1 KiB
JavaScript

import { h, render } from 'preact';
import { TagsFollowed } from '../leftSidebar/TagsFollowed';
/* global userData */
// This logic is similar to that in initScrolling.js.erb
const frontPageFeedPathNames = new Map([
['/', ''],
['/top/week', 'week'],
['/top/month', 'month'],
['/top/year', 'year'],
['/top/infinity', 'infinity'],
['/latest', 'latest'],
]);
/**
* 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 || document.getElementById('followed-tags-wrapper')) {
return;
}
// Only render if a user is logged on.
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,
tagsFollowedContainer.firstElementChild,
);
}
const feedTimeFrame = frontPageFeedPathNames.get(window.location.pathname);
if (!document.getElementById('featured-story-marker')) {
const waitingForDataLoad = setInterval(function dataLoadedCheck() {
const { user = null, userStatus } = document.body.dataset;
if (userStatus === 'logged-out') {
return;
}
if (userStatus === 'logged-in' && user !== null) {
clearInterval(waitingForDataLoad);
if (document.getElementById('rendered-article-feed')) {
return;
}
import('./homePageFeed').then(({ renderFeed }) => {
// We have user data, render followed tags.
renderFeed(feedTimeFrame);
InstantClick.on('change', () => {
const { userStatus: currentUserStatus } = document.body.dataset;
if (currentUserStatus === 'logged-out') {
return;
}
const url = new URL(window.location);
const changedFeedTimeFrame = frontPageFeedPathNames.get(url.pathname);
if (!frontPageFeedPathNames.has(url.pathname)) {
return;
}
renderFeed(changedFeedTimeFrame);
});
});
const tagsFollowedContainer = document.getElementById(
'sidebar-nav-followed-tags',
);
if (tagsFollowedContainer) renderTagsFollowed(tagsFollowedContainer);
}
}, 2);
}
InstantClick.on('change', () => {
if (document.body.dataset.userStatus !== 'logged-in') {
// Nothing to do, the user is not logged on.
return false;
}
const tagsFollowedContainer = document.getElementById(
'sidebar-nav-followed-tags',
);
if (!tagsFollowedContainer) {
// Not on the homepage, so nothing to do.
return false;
}
renderTagsFollowed(tagsFollowedContainer);
});
InstantClick.init();