import { FormInputValidation, FormLabel } from '@automattic/components';
import { generatePassword } from '@automattic/generate-password';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { debounce, flowRight as compose, isEmpty } from 'lodash';
import { Component } from 'react';
import { connect } from 'react-redux';
import FormButton from 'calypso/components/forms/form-button';
import FormButtonsBar from 'calypso/components/forms/form-buttons-bar';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormPasswordInput from 'calypso/components/forms/form-password-input';
import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation';
import { protectForm } from 'calypso/lib/protect-form';
import wp from 'calypso/lib/wp';
import { recordGoogleEvent } from 'calypso/state/analytics/actions';
import { errorNotice } from 'calypso/state/notices/actions';
import { saveUserSettings } from 'calypso/state/user-settings/actions';
import {
hasUserSettingsRequestFailed,
isPendingPasswordChange,
} from 'calypso/state/user-settings/selectors';
import './style.scss';
class AccountPassword extends Component {
state = {
password: '',
validation: null,
pendingValidation: true,
};
componentDidUpdate( prevProps ) {
if (
prevProps.isPendingPasswordChange &&
! this.props.isPendingPasswordChange &&
! this.props.hasUserSettingsRequestFailed
) {
this.props.markSaved();
window.location = '?updated=password';
}
}
generateStrongPassword = () => {
this.setState( {
password: generatePassword(),
pendingValidation: true,
} );
this.props.markChanged();
this.validatePassword();
};
validatePassword = debounce( async () => {
const password = this.state.password;
if ( '' === password ) {
this.setState( { validation: null, pendingValidation: false } );
return;
}
const validation = await wp.req
.post( '/me/settings/password/validate', { password } )
.catch( () => ( {
passed: false,
test_results: {
failed: [
{
test_name: 'network_error',
explanation: this.props.translate(
'The password could not be validated. Please check your network connection and try again.'
),
},
],
},
} ) );
this.setState( { pendingValidation: false, validation } );
}, 300 );
handlePasswordChange = ( event ) => {
const newPassword = event.currentTarget.value;
this.validatePassword();
this.setState( {
password: newPassword,
pendingValidation: true,
} );
if ( '' !== newPassword ) {
this.props.markChanged();
} else {
this.props.markSaved();
}
};
handleSaveButtonClick = () => {
this.props.recordGoogleEvent( 'Me', 'Clicked on Save Password Button' );
};
handleGenerateButtonClick = () => {
this.props.recordGoogleEvent( 'Me', 'Clicked on Generate Strong Password Button' );
this.generateStrongPassword();
};
handleNewPasswordFieldFocus = () => {
this.props.recordGoogleEvent( 'Me', 'Focused on New Password Field' );
};
submitForm = ( event ) => {
event.preventDefault();
this.props.saveUserSettings( { password: this.state.password } );
};
renderValidationNotices = () => {
const { translate } = this.props;
const failure = this.state.validation?.test_results.failed?.[ 0 ];
if ( this.state.validation?.passed ) {
return (