File size: 6,111 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
import { localize } from 'i18n-calypso';
import { flowRight as compose } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import QuerySmsCountries from 'calypso/components/data/query-countries/sms';
import FormButton from 'calypso/components/forms/form-button';
import FormButtonsBar from 'calypso/components/forms/form-buttons-bar';
import FormPhoneInput from 'calypso/components/forms/form-phone-input';
import Notice from 'calypso/components/notice';
import { gaRecordEvent } from 'calypso/lib/analytics/ga';
import { protectForm } from 'calypso/lib/protect-form';
import getCountries from 'calypso/state/selectors/get-countries';
import getUserSettings from 'calypso/state/selectors/get-user-settings';
import { setUserSetting, saveUserSettings } from 'calypso/state/user-settings/actions';
import { isUpdatingUserSettings } from 'calypso/state/user-settings/selectors';
import { saveTwoStepSMSSettings } from 'calypso/state/user-settings/thunks';
import './style.scss';

class Security2faSMSSettings extends Component {
	static propTypes = {
		countriesList: PropTypes.array.isRequired,
		onCancel: PropTypes.func.isRequired,
		onVerifyByApp: PropTypes.func.isRequired,
		onVerifyBySMS: PropTypes.func.isRequired,
		markChanged: PropTypes.func.isRequired,
		markSaved: PropTypes.func.isRequired,
	};

	constructor( props ) {
		super( props );

		let phoneNumber = null;
		const storedCountry = this.props.userSettings.two_step_sms_country;
		const storedNumber = this.props.userSettings.two_step_sms_phone_number;

		if ( storedCountry && storedNumber ) {
			phoneNumber = {
				countryCode: storedCountry,
				phoneNumber: storedNumber,
				isValid: true,
			};
		}

		this.state = {
			lastError: false,
			phoneNumber,
		};
	}

	getSubmitDisabled() {
		if ( this.props.isUpdatingUserSettings ) {
			return true;
		}

		// empty phone number, disable the submit button
		if ( ! this.state.phoneNumber ) {
			return true;
		}

		if ( ! this.state.phoneNumber.phoneNumber.length ) {
			return true;
		}

		return false;
	}

	onVerifyByApp = ( event ) => {
		event.preventDefault();
		this.submitSMSSettings( true );
	};

	onVerifyBySMS = ( event ) => {
		event.preventDefault();
		this.submitSMSSettings();
	};

	async submitSMSSettings( verifyByApp = false ) {
		const { onVerifyByApp, onVerifyBySMS } = this.props;
		const phoneNumber = this.state.phoneNumber;

		if ( ! phoneNumber.isValid ) {
			this.setState( { lastError: phoneNumber.validation } );
			return;
		}

		if (
			phoneNumber.countryCode === this.props.userSettings.two_step_sms_country &&
			phoneNumber.phoneNumber === this.props.userSettings.two_step_sms_phone_number
		) {
			verifyByApp ? onVerifyByApp() : onVerifyBySMS();
			return;
		}

		try {
			await this.props.saveTwoStepSMSSettings( phoneNumber.countryCode, phoneNumber.phoneNumber );
			verifyByApp ? onVerifyByApp() : onVerifyBySMS();
		} catch ( error ) {
			this.setState( { lastError: error } );
		}
	}

	onChangePhoneInput = ( phoneNumber ) => {
		this.setState( {
			phoneNumber: {
				phoneNumber: phoneNumber.phoneNumber,
				countryCode: phoneNumber.countryData.code,
				isValid: phoneNumber.isValid,
				validation: phoneNumber.validation,
			},
		} );
	};

	clearLastError = () => {
		this.setState( { lastError: false } );
	};

	possiblyRenderError() {
		let errorMessage;

		if ( ! this.state.lastError ) {
			return null;
		}

		if ( ! this.state.lastError.message ) {
			errorMessage = this.props.translate( 'An unknown error occurred. Please try again later.' );
		} else {
			errorMessage = this.state.lastError.message;
		}

		return (
			<Notice status="is-error" onDismissClick={ this.clearLastError } text={ errorMessage } />
		);
	}

	render() {
		const savingLabel = this.props.translate( 'Saving…' );

		return (
			<div className="security-2fa-sms-settings__container">
				<form className="security-2fa-sms-settings">
					<p>
						{ this.props.translate(
							'We need your mobile phone number to send you two-step verification codes when you log in.'
						) }
					</p>

					<div className="security-2fa-sms-settings__fieldset-container">
						<QuerySmsCountries />
						<FormPhoneInput
							countriesList={ this.props.countriesList }
							disabled={ this.props.isUpdatingUserSettings }
							countrySelectProps={ {
								onFocus() {
									gaRecordEvent( 'Me', 'Focused On 2fa SMS Country Select' );
								},
							} }
							phoneInputProps={ {
								onFocus() {
									gaRecordEvent( 'Me', 'Focused On 2fa SMS Phone Number' );
								},
							} }
							initialCountryCode={ this.props.userSettings.two_step_sms_country }
							initialPhoneNumber={ this.props.userSettings.two_step_sms_phone_number }
							onChange={ this.onChangePhoneInput }
						/>

						{ this.possiblyRenderError() }
					</div>

					<FormButtonsBar className="security-2fa-sms-settings__buttons">
						<FormButton
							type="button"
							className="security-2fa-sms-settings__cancel-button"
							isPrimary={ false }
							onClick={ ( event ) => {
								gaRecordEvent( 'Me', 'Clicked On Step 1 2fa Cancel Button' );
								this.props.onCancel( event );
							} }
						>
							{ this.props.isUpdatingUserSettings ? savingLabel : this.props.translate( 'Cancel' ) }
						</FormButton>

						<FormButton
							disabled={ this.getSubmitDisabled() }
							isPrimary
							onClick={ ( event ) => {
								gaRecordEvent( 'Me', 'Clicked On 2fa Use SMS Button' );
								this.onVerifyBySMS( event );
							} }
						>
							{ this.props.isUpdatingUserSettings
								? savingLabel
								: this.props.translate( 'Continue' ) }
						</FormButton>
					</FormButtonsBar>
				</form>
			</div>
		);
	}
}

export default compose(
	protectForm,
	localize,
	connect(
		( state ) => ( {
			isUpdatingUserSettings: isUpdatingUserSettings( state ),
			userSettings: getUserSettings( state ),
			countriesList: getCountries( state, 'sms' ),
		} ),
		{
			setUserSetting,
			saveUserSettings,
			saveTwoStepSMSSettings,
		}
	)
)( Security2faSMSSettings );