File size: 4,646 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { localize, fixMe } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import DocumentHead from 'calypso/components/data/document-head';
import EmptyContent from 'calypso/components/empty-content';
import InlineSupportLink from 'calypso/components/inline-support-link';
import { JetpackConnectionHealthBanner } from 'calypso/components/jetpack/connection-health';
import Main from 'calypso/components/main';
import NavigationHeader from 'calypso/components/navigation-header';
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
import { preventWidows } from 'calypso/lib/formatting';
import { withJetpackConnectionProblem } from 'calypso/state/jetpack-connection-health/selectors/is-jetpack-connection-problem.js';
import { getPreference } from 'calypso/state/preferences/selectors';
import { canCurrentUser } from 'calypso/state/selectors/can-current-user';
import { getSiteId } from 'calypso/state/sites/selectors';
import isJetpackSite from 'calypso/state/sites/selectors/is-jetpack-site';
import CommentList from './comment-list';
import CommentTips, { COMMENTS_TIPS_DISMISSED_PREFERENCE } from './comment-tips';
import { NEWEST_FIRST } from './constants';

import './style.scss';

export class CommentsManagement extends Component {
	static propTypes = {
		analyticsPath: PropTypes.string,
		comments: PropTypes.array,
		page: PropTypes.number,
		postId: PropTypes.number,
		showPermissionError: PropTypes.bool,
		siteId: PropTypes.number,
		siteFragment: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] ),
		status: PropTypes.string,
		translate: PropTypes.func,
	};

	static defaultProps = {
		page: 1,
		status: 'all',
	};

	state = {
		order: NEWEST_FIRST,
		filterUnreplied: false,
	};

	setOrder = ( order ) => () => this.setState( { order } );

	setFilterUnreplied = ( filterUnreplied ) => () => this.setState( { filterUnreplied } );

	render() {
		const {
			analyticsPath,
			changePage,
			isJetpack,
			isPossibleJetpackConnectionProblem,
			page,
			postId,
			showCommentList,
			showPermissionError,
			siteId,
			siteFragment,
			status,
			translate,
			hideModerationTips,
		} = this.props;
		const { filterUnreplied, order } = this.state;

		return (
			<Main className="comments" wideLayout>
				<PageViewTracker path={ analyticsPath } title="Comments" />
				{ isJetpack && isPossibleJetpackConnectionProblem && (
					<JetpackConnectionHealthBanner siteId={ siteId } />
				) }
				<DocumentHead title={ translate( 'Comments' ) } />
				{ ! showPermissionError && (
					<NavigationHeader
						screenOptionsTab="edit-comments.php"
						navigationItems={ [] }
						title={ translate( 'Comments' ) }
						subtitle={ translate(
							'View, reply to, and manage all the comments across your site. {{learnMoreLink}}Learn more{{/learnMoreLink}}.',
							{
								components: {
									learnMoreLink: <InlineSupportLink supportContext="comments" showIcon={ false } />,
								},
							}
						) }
					/>
				) }
				{ showPermissionError && (
					<EmptyContent
						title={ preventWidows(
							fixMe( {
								text: "You don't have permission to manage comments.",
								newCopy: translate( "You don't have permission to manage comments." ),
								oldCopy: translate( "Oops! You don't have permission to manage comments." ),
							} )
						) }
						line={ preventWidows(
							translate( "If you think you should, contact this site's administrator." )
						) }
					/>
				) }
				{ showCommentList && (
					<>
						{ ! hideModerationTips && <CommentTips /> }
						<CommentList
							key={ `${ siteId }-${ status }` }
							changePage={ changePage }
							filterUnreplied={ filterUnreplied }
							order={ order }
							page={ page }
							postId={ postId }
							setFilterUnreplied={ this.setFilterUnreplied }
							setOrder={ this.setOrder }
							siteId={ siteId }
							siteFragment={ siteFragment }
							status={ status }
						/>
					</>
				) }
			</Main>
		);
	}
}

const mapStateToProps = ( state, { siteFragment } ) => {
	const siteId = getSiteId( state, siteFragment );
	const canModerateComments = canCurrentUser( state, siteId, 'edit_posts' );
	const showPermissionError = ! canModerateComments;

	const showCommentList = ! showPermissionError;

	return {
		isJetpack: isJetpackSite( state, siteId ),
		siteId,
		showCommentList,
		showPermissionError,
		hideModerationTips: getPreference( state, COMMENTS_TIPS_DISMISSED_PREFERENCE ),
	};
};

export default connect( mapStateToProps )(
	localize( withJetpackConnectionProblem( CommentsManagement ) )
);