Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update all dependencies #106

Merged
merged 9 commits into from
Jun 10, 2018
2 changes: 0 additions & 2 deletions components/CurrentReplies.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class DeletedItems extends React.Component {
}

export default function CurrentReplies({
authId,
replyConnections,
disabled = false,
onDelete = () => {},
Expand Down Expand Up @@ -122,7 +121,6 @@ export default function CurrentReplies({
<ul className="items">
{validConnections.map(conn => (
<ReplyConnection
authId={authId}
key={`${conn.get('articleId')}__${conn.get('replyId')}`}
replyConnection={conn}
onAction={onDelete}
Expand Down
7 changes: 1 addition & 6 deletions components/ReplyConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export default class ReplyConnection extends React.PureComponent {

renderFooter = () => {
const {
authId,
replyConnection,
disabled,
actionText,
Expand Down Expand Up @@ -87,11 +86,7 @@ export default class ReplyConnection extends React.PureComponent {
</button>,
]
: ''}
<ReplyFeedback
authId={authId}
replyConnection={replyConnection}
onVote={onVote}
/>
<ReplyFeedback replyConnection={replyConnection} onVote={onVote} />
</footer>
);
};
Expand Down
42 changes: 23 additions & 19 deletions components/ReplyFeedback.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import React, { PureComponent } from 'react';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { feedbackStyle } from './ReplyFeedback.styles';

export default class ReplyFeedback extends PureComponent {
class ReplyFeedback extends Component {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why instead PureComponent by normal Component?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we only export connect()(ReplyFeedback), which already does pure rendering by default

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I get it!

static propTypes = {
authId: PropTypes.string,
replyConnection: PropTypes.object.isRequired,
onVote: PropTypes.func.isRequired,
};

/**
* @returns {boolean} if the reply connection is created by the current user
*/
isOwnReply = () => {
const { replyConnection, authId } = this.props;
return authId === replyConnection.getIn(['user', 'id']);
};

handleUpVote = () => {
const { replyConnection, onVote } = this.props;
return onVote(replyConnection, 'UPVOTE');
Expand All @@ -28,11 +20,11 @@ export default class ReplyFeedback extends PureComponent {
};

getFeedbackScore = () => {
return this.props.replyConnection.get('feedbacks').reduce(
const { replyConnection, currentUserId } = this.props;

return replyConnection.get('feedbacks').reduce(
(agg, feedback) => {
const isOwnFeedback =
!this.isOwnReply() &&
feedback.getIn(['user', 'id']) === this.props.authId;
const isOwnFeedback = feedback.getIn(['user', 'id']) === currentUserId;

switch (feedback.get('score')) {
case 1:
Expand All @@ -51,16 +43,20 @@ export default class ReplyFeedback extends PureComponent {
};

render() {
const { currentUserId, replyConnection } = this.props;
const { positiveCount, negativeCount, ownVote } = this.getFeedbackScore();
const isOwnReply = this.isOwnReply();

const isOwnArticleReply =
currentUserId === replyConnection.getIn(['user', 'id']);

return (
<div className="reply-feedback">
{!isOwnReply && <label>是否有幫助?</label>}
{!isOwnArticleReply && <label>是否有幫助?</label>}
<span className="vote-num">{positiveCount}</span>
<button
className="btn-vote"
onClick={this.handleUpVote}
disabled={isOwnReply}
disabled={isOwnArticleReply}
>
<svg
className={`icon icon-circle ${ownVote === 1 && 'active'}`}
Expand All @@ -74,7 +70,7 @@ export default class ReplyFeedback extends PureComponent {
<button
className="btn-vote"
onClick={this.handleDownVote}
disabled={isOwnReply}
disabled={isOwnArticleReply}
>
<svg
className={`icon icon-cross ${ownVote === -1 && 'active'}`}
Expand All @@ -89,3 +85,11 @@ export default class ReplyFeedback extends PureComponent {
);
}
}

function mapStateToProps({ auth }) {
return {
currentUserId: auth.getIn(['user', 'id']),
};
}

export default connect(mapStateToProps)(ReplyFeedback);
4 changes: 4 additions & 0 deletions ducks/replyDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ export const load = id => dispatch => {
}
replyId
user {
id
name
}
feedbacks {
score
user {
id
}
}
status
canUpdateStatus
Expand Down
6 changes: 2 additions & 4 deletions pages/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class ArticlePage extends React.Component {
};

render() {
const { data, isLoading, isReplyLoading, authId } = this.props;
const { data, isLoading, isReplyLoading } = this.props;

const article = data.get('article');
const replyConnections = data.get('replyConnections');
Expand Down Expand Up @@ -265,7 +265,6 @@ class ArticlePage extends React.Component {
>
<h2>現有回應</h2>
<CurrentReplies
authId={authId}
replyConnections={replyConnections}
disabled={isReplyLoading}
onDelete={this.handleReplyConnectionDelete}
Expand Down Expand Up @@ -310,9 +309,8 @@ function bootstrapFn(dispatch, { query: { id } }) {
return dispatch(loadAuth(id));
}

function mapStateToProps({ articleDetail, auth }) {
function mapStateToProps({ articleDetail }) {
return {
authId: auth.getIn(['user', 'id']),
isLoading: articleDetail.getIn(['state', 'isLoading']),
isReplyLoading: articleDetail.getIn(['state', 'isReplyLoading']),
data: articleDetail.get('data'),
Expand Down