docbrown/app/javascript/packs/homePage.jsx
Nick Taylor f6e99dec8b
Nickytonline/homepage main feed component (#6164) [deploy]
* wip

* Removed reference to cached_tag_list as the feed enpoint only returns tag_list now.

* Removed reference to cached_properties.

* Updated <CommentsCount /> and <TagList /> to support additinal className

* wip

* wip

* Undid init scrolling for Algolia articles

* Converted homePageFeed.jsx to an ERB template to access Sprockets.

* Now the homePageFeed.jsx files is an ERB so it can load Sprockets assets.

* wip

* Removed accidental commit of debugger statement in ERB.

* Now different feed time frames are loaded based on the homepage feed tab clicked.

* Prevent default Algolia paging when on the homepage.

* Added a tiny comment about feed time frame.

* Now feed loads properly for each time frame on home page.

* prop name refactor.

* Reneabled default Algolia scrolling.

* On the homepage, we no longer overwrite article HTML with Algolia data for initial articles.

* Assumption is the proper URL for main_image of featured article will be sent in the feed.

* Fixed Google Analytics for featured article.

* No longer need the new-feed ID for checking if we're on the homepage.

* Trying to make codeclimate happy.

* wip

* Removed reference to cached_tag_list as the feed enpoint only returns tag_list now.

* Removed reference to cached_properties.

* Updated <CommentsCount /> and <TagList /> to support additinal className

* wip

* wip

* Undid init scrolling for Algolia articles

* Converted homePageFeed.jsx to an ERB template to access Sprockets.

* Now the homePageFeed.jsx files is an ERB so it can load Sprockets assets.

* wip

* Removed accidental commit of debugger statement in ERB.

* Now different feed time frames are loaded based on the homepage feed tab clicked.

* Prevent default Algolia paging when on the homepage.

* Added a tiny comment about feed time frame.

* Now feed loads properly for each time frame on home page.

* prop name refactor.

* Reneabled default Algolia scrolling.

* On the homepage, we no longer overwrite article HTML with Algolia data for initial articles.

* Assumption is the proper URL for main_image of featured article will be sent in the feed.

* Fixed Google Analytics for featured article.

* No longer need the new-feed ID for checking if we're on the homepage.

* Added postcst episodes to <Feed /> state.

* Breaking up Article.jsx to make codeclimate happy.

* wip

* Created the <TodaysPodcastEpisodes /> and <PodcastEpisode /> components.

* Fixed issues with some components that were updated for the <FeaturedArticle />

* Added tests for <FeaturedArticle />

* Barreled up <FeaturedArticle />

* Added Storybook stories for <FeaturedArticle />

* Featured articles don't display org, so removed that test.

* Added proper today's podcasts components to feed.

* Converted some leftover ERB + removed pro checkmark as it doesn't exist yet.

* Now logic is same as master for feed timeframe.

* Updated <FeaturedArticle /> snapshots.

* On frontpage, bookmark button state is managed by Preact components now.

* bookmarkedFeedItems is now a prop on <Feed />.

* Fixed logic check for initializing reading list icons.

* Now <Article /> and <FeaturedArticle /> receive a bookmarkClick prop.

* Removed beginnings of scroll handler in <Feed /> as we're using what's there already for now.

* Removed logic to not show main image on other time frame feeds as it was not correct.

* Fixed issue with 0 rendering when no podcast episodes in feed.

* Converted <SaveButton /> to a stateful component.

* wip - <Feed /> render prop now passes bookmarked feed item ID set and a click event.

* Moved FEED_ICONS to an export so it's easier to use JSX/JS in <Feed />

* Moved <Feed /> into article components.

* Now reading list save buttons work properly in Preact components.

* Fixed issue with Save buttons in Algolia paging/<Feed />

* Updated snapshots.

* Took <Feed /> out of articles barrel file as jest was trying to import FEED_ICONs which is an ERB template.

* Now the Preact feed is dynamically imported.

* Now the Preact <Feed /> only loads for a logged on user.

* Fixed <FeaturedArticle /> so that the save button behaves properly now.

* Updated article stories for Storybook.

* Putting this back to the order it was because I've removed my code changes for this block.

* Fixed when renderFeed is imported for a logged on user.

* Add podcasts to feed and render home from server

* Add padding to feature laoding div

* Update tests

* Add more loading articles

* Fix conflicts

* Add readinglist javascript to initscrolling

* Ensure first article is one with image

* Organize feed items outside of feed instead of in it

Co-authored-by: Ben Halpern <bendhalpern@gmail.com>
Co-authored-by: Josh Puetz <josh@grorichpuetz.com>
2020-02-28 14:14:47 -05:00

133 lines
3.7 KiB
JavaScript

import { h, render } from 'preact';
import { renderFeed } from './homePageFeed';
/* global userData */
// This logic is similar to that in initScrolling.js.erb
// that prevents the classic Algolia scrolling for the front page.
const frontPageFeedPathNames = new Map([
['/', ''],
['/top/week', 'week'],
['/top/month', 'month'],
['/top/year', 'year'],
['/top/infinity', 'infinity'],
['/latest', 'latest'],
]);
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);
});
}
const feedTimeFrame = frontPageFeedPathNames.get(window.location.pathname);
let waitingForDataLoad = setTimeout(function dataLoadedCheck() {
const { user = null, userStatus } = document.body.dataset;
if (userStatus === 'logged-out') {
return;
}
if (userStatus === 'logged-in' && user !== null) {
clearTimeout(waitingForDataLoad);
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);
});
});
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', (address, 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();