mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-10 11:22:40 +00:00
remove unnecesary state
This commit is contained in:
@@ -16,12 +16,6 @@ const ErrorIndex = require('../errorPage/errors/errorIndex.js');
|
|||||||
const request = require('../../utils/request-middleware.js');
|
const request = require('../../utils/request-middleware.js');
|
||||||
|
|
||||||
const VaultPage = (props) => {
|
const VaultPage = (props) => {
|
||||||
//Form state
|
|
||||||
const [titleState, setTitle] = useState(props.query.title || '');
|
|
||||||
const [authorState, setAuthor] = useState(props.query.author || '');
|
|
||||||
const [legacyState, setLegacy] = useState(props.query.legacy !== 'false');
|
|
||||||
const [v3State, setV3] = useState(props.query.v3 !== 'false');
|
|
||||||
const [countState, setCount] = useState(props.query.count || 20);
|
|
||||||
const [pageState, setPage] = useState(parseInt(props.query.page) || 1);
|
const [pageState, setPage] = useState(parseInt(props.query.page) || 1);
|
||||||
|
|
||||||
//Response state
|
//Response state
|
||||||
@@ -51,24 +45,21 @@ const VaultPage = (props) => {
|
|||||||
|
|
||||||
const updateUrl = (titleValue, authorValue, countValue, v3Value, legacyValue, page) => {
|
const updateUrl = (titleValue, authorValue, countValue, v3Value, legacyValue, page) => {
|
||||||
const url = new URL(window.location.href);
|
const url = new URL(window.location.href);
|
||||||
const urlParams = new URLSearchParams();
|
const urlParams = new URLSearchParams(url.search);
|
||||||
|
|
||||||
Object.entries({
|
urlParams.set('title', titleValue);
|
||||||
titleValue,
|
urlParams.set('author', authorValue);
|
||||||
authorValue,
|
urlParams.set('count', countValue);
|
||||||
countValue,
|
urlParams.set('v3', v3Value);
|
||||||
v3Value,
|
urlParams.set('legacy', legacyValue);
|
||||||
legacyValue,
|
urlParams.set('page', page);
|
||||||
page,
|
|
||||||
}).forEach(([key, value]) => urlParams.set(key, value));
|
|
||||||
|
|
||||||
url.search = urlParams.toString();
|
url.search = urlParams.toString();
|
||||||
window.history.replaceState(null, null, url);
|
window.history.replaceState(null, '', url.toString());
|
||||||
};
|
};
|
||||||
|
|
||||||
const performSearch = async ({ titleValue, authorValue, countValue, v3Value, legacyValue, page }) => {
|
const performSearch = async ({ titleValue, authorValue, countValue, v3Value, legacyValue, page }) => {
|
||||||
updateUrl(titleValue, authorValue, countValue, v3Value, legacyValue, page);
|
updateUrl(titleValue, authorValue, countValue, v3Value, legacyValue, page);
|
||||||
console.log(titleValue, authorValue, countValue, v3Value, legacyValue);
|
|
||||||
if ((titleValue || authorValue) && (v3Value || legacyValue)) {
|
if ((titleValue || authorValue) && (v3Value || legacyValue)) {
|
||||||
const response = await request.get(
|
const response = await request.get(
|
||||||
`/api/vault?title=${titleValue}&author=${authorValue}&v3=${v3Value}&legacy=${legacyValue}&count=${countValue}&page=${page}`
|
`/api/vault?title=${titleValue}&author=${authorValue}&v3=${v3Value}&legacy=${legacyValue}&count=${countValue}&page=${page}`
|
||||||
@@ -119,11 +110,6 @@ const VaultPage = (props) => {
|
|||||||
const legacyValue = legacyRef.current.checked != false;
|
const legacyValue = legacyRef.current.checked != false;
|
||||||
|
|
||||||
if (update) {
|
if (update) {
|
||||||
setTitle(titleValue);
|
|
||||||
setAuthor(authorValue);
|
|
||||||
setCount(countValue);
|
|
||||||
setV3(v3Value);
|
|
||||||
setLegacy(legacyValue);
|
|
||||||
setPage(page);
|
setPage(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +163,7 @@ const VaultPage = (props) => {
|
|||||||
ref={titleRef}
|
ref={titleRef}
|
||||||
type="text"
|
type="text"
|
||||||
name="title"
|
name="title"
|
||||||
defaultValue={titleState}
|
defaultValue={props.query.title || ''}
|
||||||
onKeyUp={disableSubmitIfFormInvalid}
|
onKeyUp={disableSubmitIfFormInvalid}
|
||||||
pattern=".{3,}"
|
pattern=".{3,}"
|
||||||
title="At least 3 characters"
|
title="At least 3 characters"
|
||||||
@@ -196,7 +182,7 @@ const VaultPage = (props) => {
|
|||||||
type="text"
|
type="text"
|
||||||
name="author"
|
name="author"
|
||||||
pattern=".{1,}"
|
pattern=".{1,}"
|
||||||
defaultValue={authorState}
|
defaultValue={props.query.author || ''}
|
||||||
onKeyUp={disableSubmitIfFormInvalid}
|
onKeyUp={disableSubmitIfFormInvalid}
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
if (e.key === 'Enter' && !submitButtonRef.current.disabled)
|
if (e.key === 'Enter' && !submitButtonRef.current.disabled)
|
||||||
@@ -208,7 +194,7 @@ const VaultPage = (props) => {
|
|||||||
|
|
||||||
<label>
|
<label>
|
||||||
Results per page
|
Results per page
|
||||||
<select ref={countRef} name="count" defaultValue={countState}>
|
<select ref={countRef} name="count" defaultValue={props.query.count || 20}>
|
||||||
<option value="10">10</option>
|
<option value="10">10</option>
|
||||||
<option value="20">20</option>
|
<option value="20">20</option>
|
||||||
<option value="40">40</option>
|
<option value="40">40</option>
|
||||||
@@ -221,7 +207,7 @@ const VaultPage = (props) => {
|
|||||||
className="renderer"
|
className="renderer"
|
||||||
ref={v3Ref}
|
ref={v3Ref}
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
defaultChecked={v3State}
|
defaultChecked={props.query.v3 !== 'false'}
|
||||||
onChange={disableSubmitIfFormInvalid}
|
onChange={disableSubmitIfFormInvalid}
|
||||||
/>
|
/>
|
||||||
Search for v3 brews
|
Search for v3 brews
|
||||||
@@ -232,7 +218,7 @@ const VaultPage = (props) => {
|
|||||||
className="renderer"
|
className="renderer"
|
||||||
ref={legacyRef}
|
ref={legacyRef}
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
defaultChecked={legacyState}
|
defaultChecked={props.query.legacy !== 'false'}
|
||||||
onChange={disableSubmitIfFormInvalid}
|
onChange={disableSubmitIfFormInvalid}
|
||||||
/>
|
/>
|
||||||
Search for legacy brews
|
Search for legacy brews
|
||||||
@@ -282,7 +268,7 @@ const VaultPage = (props) => {
|
|||||||
const renderPaginationControls = () => {
|
const renderPaginationControls = () => {
|
||||||
if (!totalBrews) return null;
|
if (!totalBrews) return null;
|
||||||
|
|
||||||
const countInt = parseInt(countState);
|
const countInt = parseInt(props.query.count || 20);
|
||||||
const totalPages = Math.ceil(totalBrews / countInt);
|
const totalPages = Math.ceil(totalBrews / countInt);
|
||||||
|
|
||||||
let startPage, endPage;
|
let startPage, endPage;
|
||||||
|
|||||||
Reference in New Issue
Block a user