From 3cd82c1c251303522e19cffad9562e535f4b3406 Mon Sep 17 00:00:00 2001 From: Iris Salcedo Date: Tue, 11 Jan 2022 19:13:00 +0100 Subject: [PATCH] Fix invalid URL - Closes #3887 (#4052) * Remove * Create removeTrailingSlash function * Use regex and add documentation * Only setAddress for last input Co-authored-by: Manu --- .../networkSelector/customNode/editMode.js | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/src/components/screens/login/networkSelector/customNode/editMode.js b/src/components/screens/login/networkSelector/customNode/editMode.js index 348c5fa5e1..028c1b1b18 100644 --- a/src/components/screens/login/networkSelector/customNode/editMode.js +++ b/src/components/screens/login/networkSelector/customNode/editMode.js @@ -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, @@ -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); };