File size: 5,469 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { Button, FoldableCard } from '@automattic/components';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { Component } from 'react';
import { connect } from 'react-redux';
import { withLocalizedMoment } from 'calypso/components/localized-moment';
import safeProtocolUrl from 'calypso/lib/safe-protocol-url';
import ConnectedApplicationIcon from 'calypso/me/connected-application-icon';
import { recordGoogleEvent } from 'calypso/state/analytics/actions';
import { deleteConnectedApplication } from 'calypso/state/connected-applications/actions';

import './style.scss';

class ConnectedApplicationItem extends Component {
	static defaultProps = {
		isPlaceholder: false,
	};

	recordClickEvent = ( action, label = null ) => {
		this.props.recordGoogleEvent( 'Me', 'Clicked on ' + action, label );
	};

	getClickHandler = ( action ) => {
		return () => this.recordClickEvent( action );
	};

	disconnect = ( event ) => {
		if ( this.props.isPlaceholder ) {
			return;
		}

		const {
			connection: { title, ID },
		} = this.props;
		event.stopPropagation();
		this.recordClickEvent( 'Disconnect Connected Application Link', title );
		this.props.deleteConnectedApplication( ID );
	};

	renderAccessScopeBadge() {
		const {
			connection: { scope, site },
		} = this.props;
		let meta = '';

		if ( ! this.props.connection ) {
			return;
		}

		if ( 'auth' === scope ) {
			meta = this.props.translate( 'Authentication' );
		} else if ( 'global' === scope ) {
			meta = this.props.translate( 'Global' );
		} else if ( site ) {
			meta = site.site_name;
		}

		if ( meta.length ) {
			return <span className="connected-application-item__meta">{ meta }</span>;
		}
	}

	renderScopeMessage() {
		const {
			connection: { scope, site },
		} = this.props;
		let message;
		if ( ! this.props.connection ) {
			return;
		}

		if ( 'global' === scope ) {
			message = this.props.translate(
				'This connection is allowed to manage all of your blogs on WordPress.com, ' +
					'including any Jetpack blogs that are connected to your WordPress.com account.'
			);
		} else if ( 'auth' === scope ) {
			message = this.props.translate(
				'This connection is not allowed to manage any of your blogs.'
			);
		} else if ( false !== site ) {
			message = this.props.translate(
				'This connection is only allowed to access {{siteLink}}%(siteName)s{{/siteLink}}',
				{
					components: {
						siteLink: (
							<a
								target="_blank"
								rel="noopener noreferrer"
								href={ safeProtocolUrl( this.props.connection.site.site_URL ) }
								onClick={ this.getClickHandler( 'Connected Application Scope Blog Link' ) }
							/>
						),
					},
					args: {
						siteName: site.site_name,
					},
				}
			);
		}

		if ( ! message ) {
			return;
		}

		return (
			<div>
				<div className="connected-application-item__access-scope">
					<h2>{ this.props.translate( 'Access scope' ) }</h2>
					{ this.renderAccessScopeBadge() }
				</div>
				<p className="connected-application-item__connection-detail-description">{ message }</p>
			</div>
		);
	}

	renderDetail() {
		const {
			connection: { URL, authorized, permissions },
		} = this.props;
		if ( this.props.isPlaceholder ) {
			return;
		}

		return (
			<div>
				<h2>{ this.props.translate( 'Application website' ) }</h2>
				<p>
					<a
						href={ safeProtocolUrl( URL ) }
						onClick={ this.getClickHandler( 'Connected Application Website Link' ) }
						target="_blank"
						rel="noopener noreferrer"
					>
						{ safeProtocolUrl( URL ) }
					</a>
				</p>

				{ this.props.translate(
					'{{detailTitle}}Authorized on{{/detailTitle}}{{detailDescription}}%(date)s{{/detailDescription}}',
					{
						components: {
							// eslint-disable-next-line jsx-a11y/heading-has-content
							detailTitle: <h2 />,
							detailDescription: (
								<p className="connected-application-item__connection-detail-description" />
							),
						},
						args: {
							date: this.props.moment( authorized ).format( 'lll' ),
						},
					}
				) }
				<div>{ this.renderScopeMessage() }</div>

				<h2>{ this.props.translate( 'Access permissions' ) }</h2>
				<ul className="connected-application-item__connection-detail-descriptions">
					{ permissions.map( ( { name, description } ) => (
						<li key={ `permission-${ name }` }>{ description }</li>
					) ) }
				</ul>
			</div>
		);
	}

	renderHeader() {
		return (
			<div className="connected-application-item__header">
				<ConnectedApplicationIcon image={ this.props.connection.icon } />
				<h3>{ this.props.connection.title }</h3>
			</div>
		);
	}

	renderSummary() {
		return (
			<div>
				{ this.props.isPlaceholder ? (
					<Button compact disabled>
						{ this.props.translate( 'Loading…' ) }
					</Button>
				) : (
					<Button compact onClick={ this.disconnect }>
						{ this.props.translate( 'Disconnect' ) }
					</Button>
				) }
			</div>
		);
	}

	render() {
		const classes = clsx( {
			'connected-application-item': true,
			'is-placeholder': this.props.isPlaceholder,
		} );

		return (
			<FoldableCard
				header={ this.renderHeader() }
				summary={ this.renderSummary() }
				expandedSummary={ this.renderSummary() }
				clickableHeader
				compact
				className={ classes }
			>
				{ this.renderDetail() }
			</FoldableCard>
		);
	}
}

export default connect( null, {
	deleteConnectedApplication,
	recordGoogleEvent,
} )( localize( withLocalizedMoment( ConnectedApplicationItem ) ) );