Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: backend error clear on focus #116

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/forms/fields/email-field/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import PropTypes from 'prop-types';
import messages from './messages';
import validateEmail from './validator';
import { useDispatch, useSelector } from '../../../data/storeHooks';
import { clearRegistrationBackendError, fetchRealtimeValidations } from '../../registration-popup/data/reducers';
import { fetchRealtimeValidations } from '../../registration-popup/data/reducers';
import getValidationMessage from '../../reset-password-popup/forgot-password/data/utils';

import './index.scss';
Expand All @@ -25,6 +25,7 @@ const EmailField = forwardRef((props, ref) => {
floatingLabel,
errorMessage = '',
handleErrorChange = () => {},
handleFocus = () => {},
validateEmailFromBackend = true,
} = props;

Expand Down Expand Up @@ -52,7 +53,7 @@ const EmailField = forwardRef((props, ref) => {

const handleOnFocus = () => {
handleErrorChange('email', '');
dispatch(clearRegistrationBackendError('email'));
handleFocus('', 'email');
};

const handleSuggestionClick = (event) => {
Expand Down Expand Up @@ -134,6 +135,7 @@ EmailField.propTypes = {
value: PropTypes.string.isRequired,
handleChange: PropTypes.func.isRequired,
handleErrorChange: PropTypes.func,
handleFocus: PropTypes.func,
floatingLabel: PropTypes.string.isRequired,
errorMessage: PropTypes.string,
isRegistration: PropTypes.bool,
Expand Down
9 changes: 6 additions & 3 deletions src/forms/fields/email-field/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { BrowserRouter as Router } from 'react-router-dom';
import configureStore from 'redux-mock-store';

import { OnboardingComponentContext } from '../../../data/storeHooks';
import { clearRegistrationBackendError, fetchRealtimeValidations } from '../../registration-popup/data/reducers';
import { fetchRealtimeValidations } from '../../registration-popup/data/reducers';
import getValidationMessage from '../../reset-password-popup/forgot-password/data/utils';
import { EmailField } from '../index';

Expand Down Expand Up @@ -67,6 +67,7 @@ describe('EmailField', () => {
handleChange: jest.fn(),
floatingLabel: '',
handleErrorChange: jest.fn(),
handleFocus: jest.fn(),
confirmEmailValue: '',
};
window.location = { search: '' };
Expand Down Expand Up @@ -200,8 +201,10 @@ describe('EmailField', () => {

const emailInput = container.querySelector('input#email');
fireEvent.focus(emailInput, { target: { value: '[email protected]', name: 'email' } });

expect(store.dispatch).toHaveBeenCalledWith(clearRegistrationBackendError('email'));
expect(props.handleFocus).toHaveBeenCalledWith(
'',
'email',
);
});

it('should clear email suggestions when close icon is clicked', () => {
Expand Down
7 changes: 3 additions & 4 deletions src/forms/fields/name-field/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { useIntl } from '@edx/frontend-platform/i18n';
import PropTypes from 'prop-types';

import validateName from './validator';
import { useDispatch } from '../../../data/storeHooks';
import { clearRegistrationBackendError } from '../../registration-popup/data/reducers';
import TextField from '../text-field';

/**
Expand All @@ -20,11 +18,11 @@ import TextField from '../text-field';
*/
const NameField = (props) => {
const { formatMessage } = useIntl();
const dispatch = useDispatch();

const {
handleErrorChange,
errorMessage = '',
handleFocus,
} = props;

const handleOnBlur = (e) => {
Expand All @@ -38,7 +36,7 @@ const NameField = (props) => {

const handleOnFocus = () => {
handleErrorChange('name', '');
dispatch(clearRegistrationBackendError('name'));
handleFocus('', 'name');
};

return (
Expand All @@ -56,6 +54,7 @@ NameField.propTypes = {
value: PropTypes.string.isRequired,
handleChange: PropTypes.func.isRequired,
handleErrorChange: PropTypes.func.isRequired,
handleFocus: PropTypes.func.isRequired,
};

export default NameField;
7 changes: 5 additions & 2 deletions src/forms/fields/name-field/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { fireEvent, render } from '@testing-library/react';
import configureStore from 'redux-mock-store';

import { OnboardingComponentContext } from '../../../data/storeHooks';
import { clearRegistrationBackendError } from '../../registration-popup/data/reducers';
import { NameField } from '../index';

const IntlNameField = injectIntl(NameField);
Expand Down Expand Up @@ -34,6 +33,7 @@ describe('NameField', () => {
errorMessage: '',
handleChange: jest.fn(),
handleErrorChange: jest.fn(),
handleFocus: jest.fn(),
floatingLabel: '',
label: '',
};
Expand Down Expand Up @@ -105,7 +105,10 @@ describe('NameField', () => {

fireEvent.focus(nameInput, { target: { value: 'test', name: 'name' } });

expect(store.dispatch).toHaveBeenCalledWith(clearRegistrationBackendError('name'));
expect(props.handleFocus).toHaveBeenCalledWith(
'',
'name',
);
});
});
});
5 changes: 2 additions & 3 deletions src/forms/fields/password-field/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import messages from './messages';
import validatePasswordField from './validator';
import { useDispatch, useSelector } from '../../../data/storeHooks';
import { LETTER_REGEX, NUMBER_REGEX } from '../../registration-popup/data/constants';
import { clearRegistrationBackendError, fetchRealtimeValidations } from '../../registration-popup/data/reducers';
import { fetchRealtimeValidations } from '../../registration-popup/data/reducers';
import './index.scss';

/**
Expand Down Expand Up @@ -91,11 +91,10 @@ const PasswordField = forwardRef((props, ref) => {
}

if (props.handleFocus) {
props.handleFocus(e);
props.handleFocus(e, 'password');
}
if (handleErrorChange) {
handleErrorChange('password', '');
dispatch(clearRegistrationBackendError('password'));
}
setShowPasswordRequirements(showPasswordTooltip && true);
};
Expand Down
11 changes: 9 additions & 2 deletions src/forms/registration-popup/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ const RegistrationForm = () => {
}));
};

const handleOnFocus = (e, name) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to use e param?
It could be:

Suggested change
const handleOnFocus = (e, name) => {
const handleOnFocus = (name) => {

if (registrationError[name]) {
dispatch(clearRegistrationBackendError(name));
}
};

const handleUserRegistration = () => {
const totalRegistrationTime = (Date.now() - formStartTime) / 1000;
const userCountryCode = getCountryCookieValue();
Expand Down Expand Up @@ -360,6 +366,7 @@ const RegistrationForm = () => {
errorMessage={errors.email}
handleChange={handleOnChange}
handleErrorChange={handleErrorChange}
handleFocus={handleOnFocus}
floatingLabel={formatMessage(messages.registrationFormEmailFieldLabel)}
ref={emailRef}
/>
Expand All @@ -370,7 +377,7 @@ const RegistrationForm = () => {
errorMessage={errors.name}
handleChange={handleOnChange}
handleErrorChange={handleErrorChange}
handleFocus={() => { }}
handleFocus={handleOnFocus}
/>
{!currentProvider && (
<PasswordField
Expand All @@ -379,7 +386,7 @@ const RegistrationForm = () => {
errorMessage={errors.password}
handleChange={handleOnChange}
handleErrorChange={handleErrorChange}
handleFocus={() => { }}
handleFocus={handleOnFocus}
floatingLabel={formatMessage(messages.registrationFormPasswordFieldLabel)}
/>
)}
Expand Down
Loading