Add saved button to post list view

This commit is contained in:
Sonny Bakker 2021-02-27 15:24:35 +01:00
parent eb4ad8612b
commit 2630d67b76

View file

@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import Cookies from 'js-cookie';
import { import {
CATEGORY_TYPE, CATEGORY_TYPE,
@ -9,19 +10,22 @@ import {
SUBREDDIT, SUBREDDIT,
TWITTER_TIMELINE, TWITTER_TIMELINE,
} from '../../constants.js'; } from '../../constants.js';
import { selectPost } from '../../actions/posts.js'; import { selectPost, toggleSaved } from '../../actions/posts.js';
import { formatDatetime } from '../../../../utils.js'; import { formatDatetime } from '../../../../utils.js';
class PostItem extends React.Component { class PostItem extends React.Component {
render() { render() {
const rule = { ...this.props.post.rule }; const rule = { ...this.props.post.rule };
const post = { ...this.props.post, rule: rule.id }; const post = { ...this.props.post, rule: rule.id };
const token = Cookies.get('csrftoken');
const publicationDate = formatDatetime(post.publicationDate); const publicationDate = formatDatetime(post.publicationDate);
const titleClassName = post.read const titleClassName = post.read
? 'posts__header posts__header--read' ? 'posts__header posts__header--read'
: 'posts__header'; : 'posts__header';
let ruleUrl = ''; const savedIconClass = post.saved ? 'saved-icon saved-icon--saved' : 'saved-icon';
let ruleUrl = '';
if (rule.type === SUBREDDIT) { if (rule.type === SUBREDDIT) {
ruleUrl = `${this.props.subredditUrl}/${rule.id}/`; ruleUrl = `${this.props.subredditUrl}/${rule.id}/`;
} else if (rule.type === TWITTER_TIMELINE) { } else if (rule.type === TWITTER_TIMELINE) {
@ -59,6 +63,10 @@ class PostItem extends React.Component {
> >
<i className="fas fa-external-link-alt"></i> <i className="fas fa-external-link-alt"></i>
</a> </a>
<span
className={savedIconClass}
onClick={() => this.props.toggleSaved(post, token)}
/>
</div> </div>
</li> </li>
); );
@ -67,6 +75,7 @@ class PostItem extends React.Component {
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({
selectPost: post => dispatch(selectPost(post)), selectPost: post => dispatch(selectPost(post)),
toggleSaved: (post, token) => dispatch(toggleSaved(post, token)),
}); });
export default connect(null, mapDispatchToProps)(PostItem); export default connect(null, mapDispatchToProps)(PostItem);