File size: 5,755 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
import { Card, Button, FormLabel, FormInputValidation, Gridicon } from '@automattic/components';
import { localizeUrl } from '@automattic/i18n-utils';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import FormTextInput from 'calypso/components/forms/form-text-input';
import SuggestionSearch from 'calypso/components/suggestion-search';
import { isValidUrl } from 'calypso/lib/importer/url-validation';
import { NOT_EXISTS } from './connection-notice-types';

const noop = () => {};

class JetpackConnectSiteUrlInput extends Component {
	static propTypes = {
		handleOnClickTos: PropTypes.func,
		isError: PropTypes.oneOfType( [ PropTypes.string, PropTypes.bool ] ),
		isFetching: PropTypes.bool,
		isInstall: PropTypes.bool,
		onChange: PropTypes.func,
		onSubmit: PropTypes.func,
		translate: PropTypes.func.isRequired,
		url: PropTypes.string,
		autoFocus: PropTypes.bool,
		isSearch: PropTypes.bool,
	};

	static defaultProps = {
		candidateSites: [],
		onChange: noop,
		url: '',
		autoFocus: true,
	};

	state = {
		errorMessage: null,
	};

	focusInput = noop;

	refInput = ( formInputComponent ) => {
		this.focusInput = () => formInputComponent.focus();
	};

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

	componentDidUpdate() {
		if ( ! this.props.isError ) {
			return;
		}

		this.focusInput();
	}

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

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

	handleKeyPress = ( event ) => {
		if ( 13 === event.keyCode && ! this.isFormSubmitDisabled() && ! this.isFormSubmitBusy() ) {
			this.handleFormSubmit();
		}
	};

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

		if ( ! this.props.isFetching && ! this.state.isUnloading ) {
			if ( ! this.props.isInstall ) {
				return translate( 'Continue' );
			}

			return isSearch ? translate( 'Get Search' ) : translate( 'Start Installation' );
		}
		return translate( 'Setting up…' );
	}

	getTermsOfJetpackSyncUrl() {
		return localizeUrl( 'https://jetpack.com/support/what-data-does-jetpack-sync/' );
	}

	getTermsOfServiceUrl() {
		return localizeUrl( 'https://wordpress.com/tos/' );
	}

	isFormSubmitDisabled() {
		const { isError, url } = this.props;
		const hasError = isError && NOT_EXISTS !== isError;

		return ! url || hasError;
	}

	isFormSubmitBusy() {
		const { isFetching } = this.props;
		const { isUnloading } = this.state ?? {};

		return isFetching || isUnloading;
	}

	validateForm() {
		const { url, translate } = this.props;
		let errorMessage;

		if ( ! isValidUrl( url ) ) {
			errorMessage = translate( 'Please enter a valid URL.' );
		}

		if ( errorMessage ) {
			this.setState( { errorMessage } );
			return false;
		}

		return true;
	}

	handleFormSubmit = () => {
		const { onSubmit } = this.props;

		const isFormValid = this.validateForm();

		if ( isFormValid ) {
			onSubmit();
		}
	};

	handleChange = ( event ) => {
		const { onChange } = this.props;

		this.setState( { errorMessage: null } );
		onChange( event );
	};

	renderError() {
		const { errorMessage } = this.state;

		if ( errorMessage ) {
			return <FormInputValidation isError text={ errorMessage } />;
		}
	}

	renderTermsOfServiceLink() {
		return (
			<p className="jetpack-connect__tos-link">
				{ this.props.translate(
					'By setting up Jetpack you agree to our {{tosLinkText}}Terms of Service{{/tosLinkText}} ' +
						'and to sync {{syncLinkText}}certain data and settings{{/syncLinkText}} to WordPress.com',
					{
						components: {
							tosLinkText: (
								<a
									className="jetpack-connect__tos-link-text"
									href={ this.getTermsOfServiceUrl() }
									onClick={ this.props.handleOnClickTos }
									target="_blank"
									rel="noopener noreferrer"
								/>
							),
							syncLinkText: (
								<a
									className="jetpack-connect__sync-link-text"
									href={ this.getTermsOfJetpackSyncUrl() }
									onClick={ this.props.handleOnClickTos }
									target="_blank"
									rel="noopener noreferrer"
								/>
							),
						},
					}
				) }
			</p>
		);
	}

	render() {
		const { candidateSites, isSearch, translate, url, autoFocus } = this.props;

		const isDisabled = this.isFormSubmitDisabled();
		const isBusy = this.isFormSubmitBusy();

		return (
			<div>
				<FormLabel htmlFor="siteUrl">{ translate( 'Site address' ) }</FormLabel>
				<div className="jetpack-connect__site-address-container">
					<Gridicon className="jetpack-connect__site-address-icon" size={ 24 } icon="domains" />
					{ ! isSearch && (
						<FormTextInput
							ref={ this.refInput }
							id="siteUrl"
							autoCapitalize="off"
							autoFocus={ autoFocus } // eslint-disable-line jsx-a11y/no-autofocus
							onChange={ this.handleChange }
							disabled={ isBusy }
							placeholder="https://yourjetpack.blog"
							onKeyUp={ this.handleKeyPress }
							value={ url }
						/>
					) }
					{ isSearch && (
						<SuggestionSearch
							id="siteSelection"
							placeholder="Type your site"
							onChange={ this.handleChange }
							suggestions={ candidateSites }
							value={ url }
						/>
					) }
					{ this.renderError() }
				</div>
				<Card className="jetpack-connect__connect-button-card">
					{ this.renderTermsOfServiceLink() }
					<Button
						className="jetpack-connect__connect-button"
						primary
						disabled={ isDisabled && ! isBusy }
						busy={ isBusy }
						onClick={ this.handleFormSubmit }
					>
						{ this.renderButtonLabel() }
					</Button>
				</Card>
			</div>
		);
	}
}

export default localize( JetpackConnectSiteUrlInput );