0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-04 01:42:42 +00:00

suggested changes 2

This commit is contained in:
Víctor Losada Hernández
2024-09-01 11:10:46 +02:00
parent c528c8639a
commit f0b447866c
4 changed files with 65 additions and 93 deletions

View File

@@ -144,12 +144,10 @@ const BrewItem = createClass({
<i className='fas fa-user'/> <i className='fas fa-user'/>
{brew.authors?.map((author, index) => ( {brew.authors?.map((author, index) => (
<React.Fragment key={index}> <React.Fragment key={index}>
{author === 'hidden' ? ( {author === 'hidden'
<span title="This username had an email in it, so we hid it to keep the user's privacy, sorry">&nbsp;{author}</span> ? <span title="Username contained an email address; hidden to protect user's privacy">{author}</span>
) : ( : <a href={`/user/${author}`}>{author}</a>
<a href={`/user/${author}`}>{author}</a> }
)}
{index < brew.authors.length - 1 && ', '}
</React.Fragment> </React.Fragment>
))} ))}
</span> </span>

View File

@@ -172,8 +172,8 @@ const errorIndex = (props)=>{
**Brew Title:** ${props.brew.brewTitle}`, **Brew Title:** ${props.brew.brewTitle}`,
'99' : dedent` An unexpected error ocurred while looking for these brews, try again in a few minutes, if this error persists, '90' : dedent` An unexpected error occurred while looking for these brews.
contact the developers at the subreddit or discord provided in the home page.`, Try again in a few minutes.`,
}; };
}; };

View File

@@ -2,7 +2,6 @@ require('./vaultPage.less');
const React = require('react'); const React = require('react');
const { useState, useEffect, useRef } = React; const { useState, useEffect, useRef } = React;
const cx = require('classnames');
const Nav = require('naturalcrit/nav/nav.jsx'); const Nav = require('naturalcrit/nav/nav.jsx');
const Navbar = require('../../navbar/navbar.jsx'); const Navbar = require('../../navbar/navbar.jsx');
@@ -63,69 +62,57 @@ const VaultPage = (props) => {
window.history.replaceState(null, null, url); window.history.replaceState(null, null, url);
}; };
const performSearch = async ({ title, author, count, v3, legacy }) => {
updateUrl(title, author, count, v3, legacy, page);
console.log(title, author, count, v3, legacy);
if ((title || author) && (v3 || legacy)) {
const response = await request.get(
`/api/vault?title=${title}&author=${author}&v3=${v3}&legacy=${legacy}&count=${count}&page=${page}`
).catch((error)=>{
console.log('error at loadPage: ', error);
setError(`${error.response
? error.response.status
: error.message}`
);
updateStateWithBrews([], 1);
});
if (response.ok)
updateStateWithBrews(response.body.brews, page);
} else {
setError('404');
}
};
const loadTotal = async ({ title, v3, legacy }) => {
setTotalBrews(null);
setError(null);
if ((title || author) && (v3 || legacy)) {
const response = await request.get(
`/api/vault/total?title=${title}&author=${author}&v3=${v3}&legacy=${legacy}`
).catch((error)=>{
console.log('error at loadTotal: ', error);
setError(`${error.response
? error.response.status
: error.message}`
);
updateStateWithBrews([], 1);
});
if (response.ok)
updateStateWithBrews(response.body.brews, page);
}
};
const loadPage = async (page, update, total) => { const loadPage = async (page, update, total) => {
//Different searches use the update or total props to make only the necessary queries and functions //Different searches use the update or total props to make only the necessary queries and functions
if (!validateForm()) { if (!validateForm()) {
return; return;
} }
setSearching(true); setSearching(true);
setError(null); setError(null);
const performSearch = async ({ title, author, count, v3, legacy }) => {
updateUrl(title, author, count, v3, legacy, page);
console.log(title, author, count, v3, legacy);
if ((title || author) && (v3 || legacy)) {
try {
const response = await request.get(
`/api/vault?title=${title}&author=${author}&v3=${v3}&legacy=${legacy}&count=${count}&page=${page}`
);
if (response.ok) {
updateStateWithBrews(response.body.brews, page);
} else {
throw new Error(`Error: ${response.status}`);
}
} catch (error) {
console.log('error at loadPage: ', error);
setError(
`${
error.response
? error.response.status
: error.message
}`
);
updateStateWithBrews([], 1);
}
} else {
setError('404');
}
};
const loadTotal = async ({ title, v3, legacy }) => {
setTotalBrews(null);
setError(null);
if ((title || author) && (v3 || legacy)) {
try {
const response = await request.get(
`/api/vault/total?title=${title}&author=${author}&v3=${v3}&legacy=${legacy}`
);
if (response.ok) {
setTotalBrews(response.body.totalBrews);
} else {
throw new Error(
`Failed to load total brews: ${response.statusText}`
);
}
} catch (error) {
console.log('error at loadTotal: ', error);
setError(`${error.response.status}`);
updateStateWithBrews([], 1);
}
}
};
const title = titleRef.current.value || ''; const title = titleRef.current.value || '';
const author = authorRef.current.value || ''; const author = authorRef.current.value || '';
const count = countRef.current.value || 10; const count = countRef.current.value || 10;
@@ -167,25 +154,18 @@ 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: titleInput } = titleRef;
const { current: legacyCheckbox } = legacyRef;
const { current: v3Checkbox } = v3Ref;
const { current: authorInput } = authorRef;
const isTitleValid = titleInput.validity.valid && titleInput.value; const isTitleValid = titleRef.current.validity.valid && titleRef.current.value;
//because a pattern attr is set in the input, title must be over 2 chars long const isAuthorValid = authorRef.current.validity.valid && authorRef.current.value;
const isAuthorValid = authorInput.validity.valid && authorInput.value; const isCheckboxChecked = legacyRef.current.checked || v3Ref.current.checked;
const isCheckboxChecked = legacyCheckbox.checked || v3Checkbox.checked;
const isFormValid = const isFormValid = (isTitleValid || isAuthorValid) && isCheckboxChecked;
(isTitleValid || isAuthorValid) && isCheckboxChecked;
return isFormValid; return isFormValid;
}; };
const disableSubmitIfFormInvalid = () => { const disableSubmitIfFormInvalid = () => {
const { current: submitButton } = searchButtonRef; submitButtonRef.current.disabled = !validateForm();
submitButton.disabled = !validateForm();
}; };
const renderForm = () => ( const renderForm = () => (
@@ -274,10 +254,7 @@ const VaultPage = (props) => {
> >
Search Search
<i <i
className={cx('fas', { className={searching ? 'fas fa-spin fa-spinner': 'fas fa-search'}
'fa-search': !searching,
'fa-spin fa-spinner': searching,
})}
/> />
</button> </button>
</div> </div>
@@ -285,30 +262,27 @@ const VaultPage = (props) => {
<h3>Tips and tricks</h3> <h3>Tips and tricks</h3>
<ul> <ul>
<li> <li>
You can only search brews with this tool if they are Only <b>published</b> brews are searchable via this tool
published
</li> </li>
<li> <li>
Usernames are case sensitive, make sure you are writing Usernames are case sensitive, make sure you are writing
it correctly it correctly
</li> </li>
<li> <li>
You can use <code>-</code> to negate words, assuming Use <code>"word"</code> to match an exact string, and
there is any word not negated, and <code>"word"</code> <code>-</code> to exclude words (at least one word
to specify an exact string. must not be negated).
</li> </li>
<li> <li>
Some words like a, after, through, itself, or here, are Some common words like "a", "after", "through", "itself", "here", etc.,
ignored in searches, make sure your search has relevant are ignored in searches. The full list can be found
words. The full list can be found &nbsp; <a href="https://github.com/mongodb/mongo/blob/0e3b3ca8480ddddf5d0105d11a94bd4698335312/src/mongo/db/fts/stop_words_english.txt">
<a href="https://github.com/mongodb/mongo/blob/0e3b3ca8480ddddf5d0105d11a94bd4698335312/src/mongo/db/fts/stop_words_english.txt"> here
here </a>
</a>
</li> </li>
</ul> </ul>
</legend> </legend>
<small></small>
</div> </div>
); );

View File

@@ -76,7 +76,7 @@ const findBrews = async (req, res) => {
}) })
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);
throw {...err, message: "Error finding brews in Vault search", HBErrorCode: '99'}; throw {...err, message: "Error finding brews in Vault search", HBErrorCode: 90};
}); });
}; };
@@ -109,7 +109,7 @@ const findTotal = async (req, res) => {
}) })
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);
throw {...err, message: "Error finding brews in Vault search", HBErrorCode: '99'}; throw {...err, message: "Error finding brews in Vault search", HBErrorCode: 90};
}); });
}; };