* feed cards * frontend * preactify * preactify * . * fix * fixing things and adding dropdown * whoops. * whoops 2. * revert body bg color change * search snippet * search snippet * get rid of featured article * get rid of featured article... * cover to its own component * videos * videos * reading time * small-save-filled asset * author adjustments * adjustments * Author --> Meta * whoops. * loader * logged out state * . * dropping what's not needed for now * better name for cover * get rid of nav overflow * bringin comments placeholder back * bringing comments placeholder back * publish date * fix * date * . * save button icon * ghost button * lets skip the icon for saving for now * more buttons * counting logic * Display top comments on homepage feed * Comment list and item components * Remove 'Top Comments' title * Remove subscribe button, add reading time * Update snapshots and tests * Fix article and follower scrolling * Added reading time * (Try) to do flare tags * Button component from Pawel * More button styles from Pawel * Comment count style from Pawel * Handle empty parens for age on older articles * Dont show 'more comment' button for more than 2 comments * cover ratio * flare tag * cover fix * fix buttons * more button fixes * tags fix * responsiveness fix * reading time + a bit of responsiveness * Reading time in more places * snapshots * default color for flare tag * Update CommentsList test snapshot with new styles * fixing search results * fixing buttons * Fix podcast card tests by updating snapshots * Line clamping for top comments * Reflect save button state after click before mouseout * Now the entire feed card is clickable. (#7638) * Now pointer cursor is only on feed posts. (#7727) * Revert podcast card styling * Update test snapshot * merge * Fix rspec on feed page * Reduce complexity of class check for feed card clicking * PR feedback/fixes * Fix featured article test and snapshot * Last minute test tweaks * clean up crayons * padding issue + empty div * Correct merge error * Fix search results tests * Test fixes and snapshot updates * themes * podcasts colors Co-authored-by: ludwiczakpawel <ludwiczakpawel@gmail.com> Co-authored-by: Nick Taylor <nick@iamdeveloper.com>
165 lines
4.4 KiB
JavaScript
165 lines
4.4 KiB
JavaScript
import { h, Component } from 'preact';
|
|
import PropTypes from 'prop-types';
|
|
|
|
/* global userData sendHapticMessage showModal buttonFormData renderNewSidebarCount */
|
|
|
|
export class Feed extends Component {
|
|
componentDidMount() {
|
|
const { timeFrame } = this.props;
|
|
const { reading_list_ids = [] } = userData(); // eslint-disable-line camelcase
|
|
|
|
this.setState({ bookmarkedFeedItems: new Set(reading_list_ids) });
|
|
|
|
Feed.getFeedItems(timeFrame).then(feedItems => {
|
|
// Ensure first article is one with a main_image
|
|
const featuredStory = feedItems.find(story => story.main_image !== null);
|
|
// Remove that first one from the array.
|
|
const index = feedItems.indexOf(featuredStory);
|
|
feedItems.splice(index, 1);
|
|
const subStories = feedItems;
|
|
const organizedFeedItems = [featuredStory, subStories].flat();
|
|
this.setState({
|
|
feedItems: organizedFeedItems,
|
|
podcastEpisodes: Feed.getPodcastEpisodes(),
|
|
});
|
|
});
|
|
}
|
|
|
|
componentDidUpdate(prevProps) {
|
|
const { timeFrame } = this.props;
|
|
if (prevProps.timeFrame !== timeFrame) {
|
|
// The feed timeframe has changed. Get new feed data.
|
|
Feed.getFeedItems(timeFrame).then(feedItems => {
|
|
this.setState(_prevState => ({ feedItems }));
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves feed data.
|
|
*
|
|
* @param {number} [page=1] Page of feed data to retrieve
|
|
*
|
|
* @returns {Promise} A promise containing the JSON response for the feed data.
|
|
*/
|
|
static getFeedItems(timeFrame = '', page = 1) {
|
|
return fetch(`/stories/feed/${timeFrame}?page=${page}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'X-CSRF-Token': window.csrfToken,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
credentials: 'same-origin',
|
|
}).then(response => response.json());
|
|
}
|
|
|
|
static getPodcastEpisodes() {
|
|
const el = document.getElementById('followed-podcasts');
|
|
const user = userData(); // Global
|
|
const episodes = [];
|
|
if (
|
|
user &&
|
|
user.followed_podcast_ids &&
|
|
user.followed_podcast_ids.length > 0
|
|
) {
|
|
const data = JSON.parse(el.dataset.episodes);
|
|
data.forEach(episode => {
|
|
if (user.followed_podcast_ids.indexOf(episode.podcast.id) > -1) {
|
|
episodes.push(episode);
|
|
}
|
|
});
|
|
}
|
|
return episodes;
|
|
}
|
|
|
|
/**
|
|
* Dispatches a click event to bookmark/unbook,ard an article.
|
|
*
|
|
* @param {Event} event
|
|
*/
|
|
bookmarkClick = event => {
|
|
// The assumption is that the user is logged on at this point.
|
|
const { userStatus } = document.body;
|
|
event.preventDefault();
|
|
sendHapticMessage('medium');
|
|
|
|
if (userStatus === 'logged-out') {
|
|
showModal('add-to-readinglist-from-index');
|
|
return;
|
|
}
|
|
|
|
const { currentTarget: button } = event;
|
|
const data = buttonFormData(button);
|
|
|
|
getCsrfToken()
|
|
.then(sendFetch('reaction-creation', data))
|
|
// eslint-disable-next-line consistent-return
|
|
.then(response => {
|
|
if (response.status === 200) {
|
|
return response.json().then(json => {
|
|
const articleId = Number(button.dataset.reactableId);
|
|
|
|
this.setState(previousState => {
|
|
const { bookmarkedFeedItems } = previousState;
|
|
|
|
const { result } = json;
|
|
const updatedBookmarkedFeedItems = new Set([
|
|
...bookmarkedFeedItems.values(),
|
|
]);
|
|
|
|
if (result === 'create') {
|
|
updatedBookmarkedFeedItems.add(articleId);
|
|
}
|
|
|
|
if (result === 'destroy') {
|
|
updatedBookmarkedFeedItems.delete(articleId);
|
|
}
|
|
|
|
renderNewSidebarCount(button, json);
|
|
|
|
return {
|
|
...previousState,
|
|
bookmarkedFeedItems: updatedBookmarkedFeedItems,
|
|
};
|
|
});
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
render() {
|
|
const { renderFeed } = this.props;
|
|
const {
|
|
feedItems = [],
|
|
podcastEpisodes = [],
|
|
bookmarkedFeedItems = new Set(),
|
|
} = this.state;
|
|
|
|
return (
|
|
<div
|
|
ref={element => {
|
|
this.feedContainer = element;
|
|
}}
|
|
>
|
|
{renderFeed({
|
|
feedItems,
|
|
podcastEpisodes,
|
|
bookmarkedFeedItems,
|
|
bookmarkClick: this.bookmarkClick,
|
|
})}
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
|
|
Feed.defaultProps = {
|
|
timeFrame: '',
|
|
};
|
|
|
|
Feed.propTypes = {
|
|
timeFrame: PropTypes.string,
|
|
renderFeed: PropTypes.func.isRequired,
|
|
};
|
|
|
|
Feed.displayName = 'Feed';
|