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: Entering the incorrect magic code spins the 'sign-in' button continuously offline #18960

Merged
merged 10 commits into from
May 19, 2023
46 changes: 37 additions & 9 deletions src/components/MagicCodeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import CONST from '../CONST';
import Text from './Text';
import TextInput from './TextInput';
import FormHelpMessage from './FormHelpMessage';
import {withNetwork} from './OnyxProvider';
import networkPropTypes from './networkPropTypes';
import useOnNetworkReconnect from './hooks/useOnNetworkReconnect';
import * as Browser from '../libs/Browser';

const propTypes = {
/** Information about the network */
network: networkPropTypes.isRequired,

/** Name attribute for the input */
name: PropTypes.string,

Expand Down Expand Up @@ -103,11 +109,28 @@ function MagicCodeInput(props) {
},
}));

/**
* Check if current magic code is ready for submit
* @returns {boolean}
*/
const isReadyToSubmit = () => {
const numbers = decomposeString(props.value);
return props.shouldSubmitOnComplete && _.filter(numbers, (n) => ValidationUtils.isNumeric(n)).length === CONST.MAGIC_CODE_LENGTH && !props.network.isOffline;
};

useOnNetworkReconnect(() => {
if (!isReadyToSubmit()) {
return;
}
inputRefs.current[editIndex].blur();
setFocusedIndex(undefined);
props.onFulfill(props.value);
Copy link
Collaborator

@Santhosh-Sellavel Santhosh-Sellavel May 17, 2023

Choose a reason for hiding this comment

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

The whole block is redundant I mentioned it here already. Check lines 133 to 138 can we this into a function. isReadyToSubmit is not need can be moved in the new method. Ex: validateAndSubmit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Santhosh-Sellavel Agreed. I've just resolved it now

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's fast 🚀

});

useEffect(() => {
// Blurs the input and removes focus from the last input and, if it should submit
// on complete, it will call the onFulfill callback.
const numbers = decomposeString(props.value);
if (!props.shouldSubmitOnComplete || _.filter(numbers, (n) => ValidationUtils.isNumeric(n)).length !== CONST.MAGIC_CODE_LENGTH) {
if (!isReadyToSubmit()) {
return;
}
inputRefs.current[editIndex].blur();
Expand Down Expand Up @@ -255,6 +278,9 @@ function MagicCodeInput(props) {
setEditIndex(newFocusedIndex);
inputRefs.current[newFocusedIndex].focus();
} else if (keyValue === 'Enter') {
if (!isReadyToSubmit()) {
return;
}
setInput('');
props.onFulfill(props.value);
}
Expand Down Expand Up @@ -321,10 +347,12 @@ function MagicCodeInput(props) {
MagicCodeInput.propTypes = propTypes;
MagicCodeInput.defaultProps = defaultProps;

export default forwardRef((props, ref) => (
<MagicCodeInput
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
));
export default withNetwork()(
forwardRef((props, ref) => (
<MagicCodeInput
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
)),
);
7 changes: 3 additions & 4 deletions src/components/Pressable/PressableWithFeedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ const PressableWithFeedback = forwardRef((props, ref) => {
setDisabled(props.disabled);
return;
}
onPress
.finally(() => {
setDisabled(props.disabled);
});
onPress.finally(() => {
setDisabled(props.disabled);
});
});
}}
>
Expand Down