Skip to content

Commit

Permalink
Fix invalid URL - Closes #3887 (#4052)
Browse files Browse the repository at this point in the history
* Remove

* Create removeTrailingSlash function

* Use regex and add documentation

* Only setAddress for last input

Co-authored-by: Manu <[email protected]>
  • Loading branch information
Iris Salcedo and ManuGowda authored Jan 11, 2022
1 parent cf63445 commit 3cd82c1
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/components/screens/login/networkSelector/customNode/editMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ const validateNode = async (address) => {
}
};

/**
* Removes the trailing slash if string is not exactly equal to 'http:/','http://','https:/' or 'https://'
*
* @param {string} url - A URL that might end in a slash
* @returns {string} A URL without a trailing slash
*/
const removeTrailingSlash = (url) => {
if (url.charAt(url.length - 1) !== '/' || /http(s?):(\/){1,2}$/.test(url)) {
return url;
}

return url.substring(0, url.length - 1);
};

const EditMode = ({
t, setMode, dropdownRef,
storedCustomNetwork, networkSelected, customNetworkStored,
Expand All @@ -33,29 +47,36 @@ const EditMode = ({
feedback: '',
});
const timeout = useRef();
const lastInput = useRef();

const validate = (value) => {
const validate = (input) => {
clearTimeout(timeout.current);
lastInput.current = input;
// validate the URL with debouncer
timeout.current = setTimeout(() => {
const value = removeTrailingSlash(input);
const normalized = addHttp(value);
setLoading(true);
validateNode(normalized)
.then(() => {
setAddress({
value,
error: 0,
feedback: '',
});
setLoading(false);
if (input === lastInput.current) {
setAddress({
value,
error: 0,
feedback: '',
});
setLoading(false);
}
})
.catch(() => {
setAddress({
value,
error: 1,
feedback: t('Unable to connect to Lisk Service, please check the address and try again'),
});
setLoading(false);
if (input === lastInput.current) {
setAddress({
value,
error: 1,
feedback: t('Unable to connect to Lisk Service, please check the address and try again'),
});
setLoading(false);
}
});
}, 500);
};
Expand Down

0 comments on commit 3cd82c1

Please sign in to comment.