File size: 11,545 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
 * Component which handle remote credentials for installing Jetpack
 */
import page from '@automattic/calypso-router';
import { Button, Card, FormInputValidation, FormLabel, Gridicon } from '@automattic/components';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { flowRight } from 'lodash';
import { Component, Fragment } from 'react';
import { connect } from 'react-redux';
import FormattedHeader from 'calypso/components/formatted-header';
import FormButton from 'calypso/components/forms/form-button';
import FormPasswordInput from 'calypso/components/forms/form-password-input';
import FormTextInput from 'calypso/components/forms/form-text-input';
import LoggedOutFormLinkItem from 'calypso/components/logged-out-form/link-item';
import LoggedOutFormLinks from 'calypso/components/logged-out-form/links';
import { addQueryArgs } from 'calypso/lib/route';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { getConnectingSite } from 'calypso/state/jetpack-connect/selectors';
import {
	jetpackRemoteInstall,
	jetpackRemoteInstallUpdateError,
} from 'calypso/state/jetpack-remote-install/actions';
import getJetpackRemoteInstallErrorCode from 'calypso/state/selectors/get-jetpack-remote-install-error-code';
import getJetpackRemoteInstallErrorMessage from 'calypso/state/selectors/get-jetpack-remote-install-error-message';
import isJetpackRemoteInstallComplete from 'calypso/state/selectors/is-jetpack-remote-install-complete';
import isRemoteInstallingJetpack from 'calypso/state/selectors/is-remote-installing-jetpack';
import {
	ACTIVATION_FAILURE,
	ACTIVATION_RESPONSE_ERROR,
	INSTALL_RESPONSE_ERROR,
	INVALID_CREDENTIALS,
	INVALID_PERMISSIONS,
	UNKNOWN_REMOTE_INSTALL_ERROR,
} from './connection-notice-types';
import { REMOTE_PATH_AUTH } from './constants';
import HelpButton from './help-button';
import JetpackConnectNotices from './jetpack-connect-notices';
import JetpackRemoteInstallNotices from './jetpack-remote-install-notices';
import MainWrapper from './main-wrapper';
import { addCalypsoEnvQueryArg } from './utils';

export class OrgCredentialsForm extends Component {
	state = {
		username: '',
		password: '',
		isUnloading: false,
	};

	handleSubmit = ( event ) => {
		const { siteToConnect } = this.props;
		event.preventDefault();

		if ( this.props.isRemoteInstalling ) {
			return;
		}

		this.props.recordTracksEvent( 'calypso_jpc_remoteinstall_submit', {
			url: siteToConnect,
		} );
		this.props.jetpackRemoteInstall( siteToConnect, this.state.username, this.state.password );
	};

	componentDidMount() {
		const { siteToConnect } = this.props;

		if ( ! siteToConnect ) {
			page.redirect( '/jetpack/connect' );
		}

		this.props.recordTracksEvent( 'calypso_jpc_remoteinstall_view', {
			url: siteToConnect,
		} );

		window.addEventListener( 'beforeunload', this.beforeUnloadHandler );
	}

	componentWillUnmount() {
		window.removeEventListener( 'beforeunload', this.beforeUnloadHandler );
	}

	componentDidUpdate() {
		const { isResponseCompleted } = this.props;

		if ( isResponseCompleted ) {
			// Login to remote site and redirect to JP connect URL
			this.buildRemoteSiteLoginForm().submit();
		}
	}

	buildRemoteSiteLoginForm() {
		const { siteToConnect } = this.props;

		const form = document.createElement( 'form' );
		form.setAttribute( 'method', 'post' );

		const redirectUrl = addCalypsoEnvQueryArg( siteToConnect + REMOTE_PATH_AUTH );
		const actionUrl = addQueryArgs( { redirect_to: redirectUrl }, siteToConnect + '/wp-login.php' );
		form.setAttribute( 'action', actionUrl );

		const user = document.createElement( 'input' );
		user.setAttribute( 'type', 'hidden' );
		user.setAttribute( 'name', 'log' );
		user.setAttribute( 'value', this.state.username );
		form.appendChild( user );

		const pwd = document.createElement( 'input' );
		pwd.setAttribute( 'type', 'hidden' );
		pwd.setAttribute( 'name', 'pwd' );
		pwd.setAttribute( 'value', this.state.password );
		form.appendChild( pwd );

		document.body.appendChild( form );
		return form;
	}

	getChangeHandler = ( field ) => ( event ) => {
		this.setState( { [ field ]: event.target.value } );
	};

	getHeaderText() {
		const { translate } = this.props;

		return translate( "Let's connect your site" );
	}

	getSubHeaderText() {
		const { translate } = this.props;
		const subheader = translate(
			"Add your self-hosted site login details below and we'll get Jetpack installed for you. Your details are not stored."
		);
		return <span>{ subheader }</span>;
	}

	getError( installError ) {
		if ( installError === null ) {
			return undefined;
		}

		if (
			installError === 'ACTIVATION_FAILURE' ||
			installError === 'ACTIVATION_ON_INSTALL_FAILURE'
		) {
			return ACTIVATION_FAILURE;
		}
		if ( installError === 'INVALID_CREDENTIALS' ) {
			return INVALID_CREDENTIALS;
		}
		if ( installError === 'ACTIVATION_RESPONSE_ERROR' ) {
			return ACTIVATION_RESPONSE_ERROR;
		}
		if ( installError === 'INSTALL_RESPONSE_ERROR' ) {
			return INSTALL_RESPONSE_ERROR;
		}
		if ( installError === 'FORBIDDEN' ) {
			return INVALID_PERMISSIONS;
		}
		if ( installError === 'LOGIN_FAILURE' ) {
			// Non-credentials login failure. We don't know of any action that can be taken.
			return UNKNOWN_REMOTE_INSTALL_ERROR;
		}

		return UNKNOWN_REMOTE_INSTALL_ERROR;
	}

	isInvalidCreds() {
		return this.getError( this.props.installError ) === INVALID_CREDENTIALS;
	}

	isInvalidUsername() {
		return this.props.installErrorMessage === 'bad username';
	}

	isInvalidPassword() {
		return this.props.installErrorMessage === 'bad password';
	}

	formFields() {
		const { translate, isRemoteInstalling } = this.props;
		const { password, username, isUnloading } = this.state;

		const isDisabled = isRemoteInstalling || isUnloading;

		const userClassName = clsx( 'jetpack-connect__credentials-form-input', {
			'is-error': this.isInvalidUsername(),
		} );
		const passwordClassName = clsx( 'jetpack-connect__password-form-input', {
			'is-error': this.isInvalidPassword(),
		} );
		const removedProtocolURL = this.props.siteToConnect.replace( /(^\w+:|^)\/\//, '' );
		return (
			<Fragment>
				<div className="jetpack-connect__site-address">
					<div className="jetpack-connect__globe">
						<Gridicon size={ 24 } icon="globe" />
					</div>{ ' ' }
					{ removedProtocolURL }
				</div>
				<FormLabel htmlFor="username">{ translate( 'WordPress username or email' ) }</FormLabel>
				<div className="jetpack-connect__site-address-container">
					<Gridicon size={ 24 } icon="user" />
					<FormTextInput
						autoCapitalize="off"
						autoCorrect="off"
						className={ userClassName }
						disabled={ isDisabled }
						id="username"
						name="username"
						onChange={ this.getChangeHandler( 'username' ) }
						value={ username || '' }
					/>
					{ this.isInvalidUsername() && (
						<FormInputValidation
							isError
							text={ translate( 'Username or email does not exist. Please try again.' ) }
						/>
					) }
				</div>
				<div className="jetpack-connect__password-container">
					<FormLabel htmlFor="password">{ translate( 'WordPress password' ) }</FormLabel>
					<div className="jetpack-connect__password-form">
						<Gridicon size={ 24 } icon="lock" />
						<FormPasswordInput
							className={ passwordClassName }
							disabled={ isDisabled }
							id="password"
							name="password"
							onChange={ this.getChangeHandler( 'password' ) }
							value={ password || '' }
						/>
						{ this.isInvalidPassword() && (
							<FormInputValidation
								isError
								text={ translate( 'Your password is incorrect, please try again' ) }
							/>
						) }
					</div>
				</div>
			</Fragment>
		);
	}

	renderButtonLabel() {
		const { isResponseCompleted, translate, isRemoteInstalling } = this.props;

		if ( isResponseCompleted ) {
			return translate( 'Connecting account…' );
		}

		if ( ! isRemoteInstalling ) {
			return translate( 'Install Jetpack' );
		}

		return translate( 'Installing Jetpack…' );
	}

	formFooter() {
		const { isRemoteInstalling } = this.props;
		const { username, password, isUnloading } = this.state;

		const isBusy = isRemoteInstalling || isUnloading;

		return (
			<div className="jetpack-connect__creds-form-footer">
				<FormButton
					className="jetpack-connect__credentials-submit"
					disabled={ ! username || ! password }
					busy={ isBusy }
				>
					{ this.renderButtonLabel() }
				</FormButton>
			</div>
		);
	}

	onClickBack = () => {
		const { installError, siteToConnect } = this.props;
		if ( installError && ! this.isInvalidCreds() ) {
			this.props.jetpackRemoteInstallUpdateError( siteToConnect, null, null );
			return;
		}
		page.redirect( '/jetpack/connect' );
	};

	beforeUnloadHandler = () => {
		this.setState( {
			isUnloading: true,
		} );
	};

	footerLink() {
		const { installError, siteToConnect, translate } = this.props;
		const manualInstallUrl = addQueryArgs(
			{ url: siteToConnect },
			'/jetpack/connect/instructions'
		);
		const manualInstallClick = () => {
			this.props.recordTracksEvent( 'calypso_jpc_remoteinstall_instructionsclick', {
				url: siteToConnect,
			} );
		};

		return (
			<LoggedOutFormLinks>
				{ ( this.isInvalidCreds() || ! installError ) && (
					<LoggedOutFormLinkItem href={ manualInstallUrl } onClick={ manualInstallClick }>
						{ translate( 'Install Jetpack manually' ) }
					</LoggedOutFormLinkItem>
				) }
				<HelpButton />
				<div className="jetpack-connect__navigation">
					<Button
						compact
						borderless
						className="jetpack-connect__back-button"
						onClick={ this.onClickBack }
					>
						<Gridicon icon="arrow-left" size={ 18 } />
						{ translate( 'Back' ) }
					</Button>
				</div>
			</LoggedOutFormLinks>
		);
	}

	renderHeadersText() {
		return (
			<FormattedHeader
				headerText={ this.getHeaderText() }
				subHeaderText={ this.getSubHeaderText() }
			/>
		);
	}

	render() {
		const { installError } = this.props;

		return (
			<MainWrapper>
				{ ! this.isInvalidCreds() && installError && (
					<div className="jetpack-connect__notice">
						<JetpackRemoteInstallNotices noticeType={ this.getError( installError ) } />
					</div>
				) }
				{ ( this.isInvalidCreds() || ! installError ) && this.props.siteToConnect && (
					<div className="jetpack-connect__site-url-entry-container">
						{ this.renderHeadersText() }
						<Card className="jetpack-connect__site-url-input-container">
							{ this.isInvalidCreds() && (
								<JetpackConnectNotices noticeType={ this.getError( installError ) } />
							) }
							<form onSubmit={ this.handleSubmit }>
								{ this.formFields() }
								{ this.formFooter() }
							</form>
						</Card>
					</div>
				) }
				{ this.footerLink() }
			</MainWrapper>
		);
	}
}

const connectComponent = connect(
	( state ) => {
		const jetpackConnectSite = getConnectingSite( state );
		const siteData = jetpackConnectSite.data || {};
		const siteToConnect = siteData.urlAfterRedirects || jetpackConnectSite.url;

		return {
			installError: getJetpackRemoteInstallErrorCode( state, siteToConnect ),
			installErrorMessage: getJetpackRemoteInstallErrorMessage( state, siteToConnect ),
			isRemoteInstalling: isRemoteInstallingJetpack( state, siteToConnect ),
			isResponseCompleted: isJetpackRemoteInstallComplete( state, siteToConnect ),
			siteToConnect,
		};
	},
	{
		jetpackRemoteInstall,
		jetpackRemoteInstallUpdateError,
		recordTracksEvent,
	}
);

export default flowRight( connectComponent, localize )( OrgCredentialsForm );