mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-19 09:52:44 +00:00
"Refactor VaultPage component: update form validation and submission logic"
This commit is contained in:
@@ -37,8 +37,8 @@ const VaultPage = (props) => {
|
|||||||
const searchButtonRef = useRef(null);
|
const searchButtonRef = useRef(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
validateForm();
|
disableSubmitIfFormInvalid();
|
||||||
if (title) {
|
if (validateForm()) {
|
||||||
loadPage(page, false, true);
|
loadPage(page, false, true);
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
@@ -70,6 +70,14 @@ const VaultPage = (props) => {
|
|||||||
setSearching(true);
|
setSearching(true);
|
||||||
setError(null);
|
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 ({
|
const performSearch = async ({
|
||||||
title,
|
title,
|
||||||
author,
|
author,
|
||||||
@@ -129,11 +137,7 @@ 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);
|
||||||
@@ -170,7 +174,6 @@ const VaultPage = (props) => {
|
|||||||
|
|
||||||
const validateForm = () => {
|
const validateForm = () => {
|
||||||
//form validity: title or author must be written, and at least one renderer set
|
//form validity: title or author must be written, and at least one renderer set
|
||||||
const { current: submitButton } = searchButtonRef;
|
|
||||||
const { current: titleInput } = titleRef;
|
const { current: titleInput } = titleRef;
|
||||||
const { current: legacyCheckbox } = legacyRef;
|
const { current: legacyCheckbox } = legacyRef;
|
||||||
const { current: v3Checkbox } = v3Ref;
|
const { current: v3Checkbox } = v3Ref;
|
||||||
@@ -180,10 +183,17 @@ 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;
|
||||||
|
|
||||||
submitButton.disabled =
|
const isFormValid = isTitleValid && isAuthorValid && isCheckboxChecked
|
||||||
!(isTitleValid || isAuthorValid) || !isCheckboxChecked;
|
|
||||||
|
return isFormValid;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const disableSubmitIfFormInvalid = () => {
|
||||||
|
const { current: submitButton } = searchButtonRef;
|
||||||
|
|
||||||
|
submitButton.disabled = validateForm();
|
||||||
|
}
|
||||||
|
|
||||||
const renderForm = () => (
|
const renderForm = () => (
|
||||||
<div className="brewLookup">
|
<div className="brewLookup">
|
||||||
<h2 className="formTitle">Brew Lookup</h2>
|
<h2 className="formTitle">Brew Lookup</h2>
|
||||||
@@ -195,7 +205,7 @@ const VaultPage = (props) => {
|
|||||||
type="text"
|
type="text"
|
||||||
name="title"
|
name="title"
|
||||||
defaultValue={title}
|
defaultValue={title}
|
||||||
onKeyUp={validateForm}
|
onKeyUp={disableSubmitIfFormInvalid}
|
||||||
pattern=".{3,}"
|
pattern=".{3,}"
|
||||||
title="At least 3 characters"
|
title="At least 3 characters"
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
@@ -220,7 +230,7 @@ const VaultPage = (props) => {
|
|||||||
name="author"
|
name="author"
|
||||||
pattern=".{1,}"
|
pattern=".{1,}"
|
||||||
defaultValue={author}
|
defaultValue={author}
|
||||||
onKeyUp={validateForm}
|
onKeyUp={disableSubmitIfFormInvalid}
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Enter') {
|
||||||
if (!searchButtonRef.current.disabled) {
|
if (!searchButtonRef.current.disabled) {
|
||||||
@@ -246,7 +256,7 @@ const VaultPage = (props) => {
|
|||||||
ref={v3Ref}
|
ref={v3Ref}
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
defaultChecked={v3}
|
defaultChecked={v3}
|
||||||
onChange={validateForm}
|
onChange={disableSubmitIfFormInvalid}
|
||||||
/>
|
/>
|
||||||
Search for v3 brews
|
Search for v3 brews
|
||||||
</label>
|
</label>
|
||||||
@@ -256,7 +266,7 @@ const VaultPage = (props) => {
|
|||||||
ref={legacyRef}
|
ref={legacyRef}
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
defaultChecked={legacy}
|
defaultChecked={legacy}
|
||||||
onChange={validateForm}
|
onChange={disableSubmitIfFormInvalid}
|
||||||
/>
|
/>
|
||||||
Search for legacy brews
|
Search for legacy brews
|
||||||
</label>
|
</label>
|
||||||
|
|||||||
Reference in New Issue
Block a user