/* globals gettext */
import 'whatwg-fetch';
import PropTypes from 'prop-types';
import React from 'react';
import { Button, StatusAlert } from '@edx/paragon/static';
import PasswordResetInput from './PasswordResetInput';
// NOTE: Use static paragon with this because some internal classes (StatusAlert at least)
// conflict with some standard LMS ones ('alert' at least). This means that you need to do
// something like the following on any templates that use this class:
//
//
//
class PasswordResetConfirmation extends React.Component {
constructor(props) {
super(props);
this.state = {
password: '',
passwordConfirmation: '',
showMatchError: false,
isValid: true,
validationMessage: '',
};
this.onBlurPassword1 = this.onBlurPassword1.bind(this);
this.onBlurPassword2 = this.onBlurPassword2.bind(this);
}
onBlurPassword1(password) {
this.updatePasswordState(password, this.state.passwordConfirmation);
this.validatePassword(password);
}
onBlurPassword2(passwordConfirmation) {
this.updatePasswordState(this.state.password, passwordConfirmation);
}
updatePasswordState(password, passwordConfirmation) {
this.setState({
password,
passwordConfirmation,
showMatchError: !!password && !!passwordConfirmation && (password !== passwordConfirmation),
});
}
validatePassword(password) {
fetch('/api/user/v1/validation/registration', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
password,
}),
})
.then(res => res.json())
.then((response) => {
let validationMessage = '';
// Be careful about grabbing this message, since we could have received an HTTP error or the
// endpoint didn't give us what we expect. We only care if we get a clear error message.
if (response.validation_decisions && response.validation_decisions.password) {
validationMessage = response.validation_decisions.password;
}
this.setState({
isValid: !validationMessage,
validationMessage,
});
});
}
render() {
return (