mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-03 21:22:39 +00:00
"Refactor VaultPage component: moved form validation logic, removed unnecessary variables, and rearranged code structure."
This commit is contained in:
@@ -38,9 +38,7 @@ const VaultPage = (props) => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
disableSubmitIfFormInvalid();
|
disableSubmitIfFormInvalid();
|
||||||
if (validateForm()) {
|
loadPage(page, false, true);
|
||||||
loadPage(page, false, true);
|
|
||||||
}
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const updateStateWithBrews = (brews, page) => {
|
const updateStateWithBrews = (brews, page) => {
|
||||||
@@ -67,24 +65,17 @@ const VaultPage = (props) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const loadPage = async (page, update, total) => {
|
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);
|
setSearching(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
const title = titleRef.current.value || '';
|
const performSearch = async ({ title, author, count, v3, legacy }) => {
|
||||||
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,
|
|
||||||
}) => {
|
|
||||||
updateUrl(title, author, count, v3, legacy, page);
|
updateUrl(title, author, count, v3, legacy, page);
|
||||||
if ((title || author) && (v3 || legacy)) {
|
if ((title || author) && (v3 || legacy)) {
|
||||||
try {
|
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) {
|
if (update) {
|
||||||
setTitle(title);
|
setTitle(title);
|
||||||
@@ -183,7 +178,7 @@ const VaultPage = (props) => {
|
|||||||
const isAuthorValid = authorInput.validity.valid && authorInput.value;
|
const isAuthorValid = authorInput.validity.valid && authorInput.value;
|
||||||
const isCheckboxChecked = legacyCheckbox.checked || v3Checkbox.checked;
|
const isCheckboxChecked = legacyCheckbox.checked || v3Checkbox.checked;
|
||||||
|
|
||||||
const isFormValid = isTitleValid && isAuthorValid && isCheckboxChecked
|
const isFormValid = isTitleValid && isAuthorValid && isCheckboxChecked;
|
||||||
|
|
||||||
return isFormValid;
|
return isFormValid;
|
||||||
};
|
};
|
||||||
@@ -192,7 +187,7 @@ const VaultPage = (props) => {
|
|||||||
const { current: submitButton } = searchButtonRef;
|
const { current: submitButton } = searchButtonRef;
|
||||||
|
|
||||||
submitButton.disabled = validateForm();
|
submitButton.disabled = validateForm();
|
||||||
}
|
};
|
||||||
|
|
||||||
const renderForm = () => (
|
const renderForm = () => (
|
||||||
<div className="brewLookup">
|
<div className="brewLookup">
|
||||||
@@ -374,7 +369,7 @@ const VaultPage = (props) => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (title === '' && author === '' && !brewCollection) {
|
if (validateForm() && !brewCollection) {
|
||||||
return (
|
return (
|
||||||
<div className="foundBrews noBrews">
|
<div className="foundBrews noBrews">
|
||||||
<h3>No search yet</h3>
|
<h3>No search yet</h3>
|
||||||
|
|||||||
Reference in New Issue
Block a user