From a8e6f5cf26ddd86b02fb0b8a5903ad5c6b11efae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Losada=20Hern=C3=A1ndez?= Date: Fri, 9 Aug 2024 22:53:20 +0200 Subject: [PATCH] "Refactor VaultPage component: moved form validation logic, removed unnecessary variables, and rearranged code structure." --- client/homebrew/pages/vaultPage/vaultPage.jsx | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/client/homebrew/pages/vaultPage/vaultPage.jsx b/client/homebrew/pages/vaultPage/vaultPage.jsx index a3293e764..271304efb 100644 --- a/client/homebrew/pages/vaultPage/vaultPage.jsx +++ b/client/homebrew/pages/vaultPage/vaultPage.jsx @@ -38,9 +38,7 @@ const VaultPage = (props) => { useEffect(() => { disableSubmitIfFormInvalid(); - if (validateForm()) { - loadPage(page, false, true); - } + loadPage(page, false, true); }, []); const updateStateWithBrews = (brews, page) => { @@ -67,24 +65,17 @@ const VaultPage = (props) => { }; const loadPage = async (page, update, total) => { + if (!validateForm()) { + console.log( + 'Invalid search, title should be more than 3 characters, or an author specified, and at least one renderer specified.' + ); + return; + } + setSearching(true); setError(null); - const title = titleRef.current.value || ''; - const author = authorRef.current.value || ''; - const count = countRef.current.value || 10; - const v3 = v3Ref.current.checked != false; - const legacy = legacyRef.current.checked != false; - - if (!title || title.length < 3) { return } - - const performSearch = async ({ - title, - author, - count, - v3, - legacy, - }) => { + const performSearch = async ({ title, author, count, v3, legacy }) => { updateUrl(title, author, count, v3, legacy, page); if ((title || author) && (v3 || legacy)) { try { @@ -137,7 +128,11 @@ const VaultPage = (props) => { } }; - + const title = titleRef.current.value || ''; + const author = authorRef.current.value || ''; + const count = countRef.current.value || 10; + const v3 = v3Ref.current.checked != false; + const legacy = legacyRef.current.checked != false; if (update) { setTitle(title); @@ -183,7 +178,7 @@ const VaultPage = (props) => { const isAuthorValid = authorInput.validity.valid && authorInput.value; const isCheckboxChecked = legacyCheckbox.checked || v3Checkbox.checked; - const isFormValid = isTitleValid && isAuthorValid && isCheckboxChecked + const isFormValid = isTitleValid && isAuthorValid && isCheckboxChecked; return isFormValid; }; @@ -192,7 +187,7 @@ const VaultPage = (props) => { const { current: submitButton } = searchButtonRef; submitButton.disabled = validateForm(); - } + }; const renderForm = () => (
@@ -374,7 +369,7 @@ const VaultPage = (props) => { ); } - if (title === '' && author === '' && !brewCollection) { + if (validateForm() && !brewCollection) { return (

No search yet