diff --git a/client/homebrew/navbar/vault.navitem.jsx b/client/homebrew/navbar/vault.navitem.jsx index 51c5c341d..087297011 100644 --- a/client/homebrew/navbar/vault.navitem.jsx +++ b/client/homebrew/navbar/vault.navitem.jsx @@ -5,11 +5,11 @@ const Nav = require('naturalcrit/nav/nav.jsx'); module.exports = function (props) { return ( Vault diff --git a/client/homebrew/pages/basePages/listPage/brewItem/brewItem.jsx b/client/homebrew/pages/basePages/listPage/brewItem/brewItem.jsx index bace49e23..1100d8845 100644 --- a/client/homebrew/pages/basePages/listPage/brewItem/brewItem.jsx +++ b/client/homebrew/pages/basePages/listPage/brewItem/brewItem.jsx @@ -141,7 +141,7 @@ const BrewItem = createClass({ : <> } - {brew.authors?.map((author, index) => ( + {brew.authors?.map((author, index)=>( {author === 'hidden' ? {author} diff --git a/client/homebrew/pages/errorPage/errors/errorIndex.js b/client/homebrew/pages/errorPage/errors/errorIndex.js index 91051d3ce..c5c455bbe 100644 --- a/client/homebrew/pages/errorPage/errors/errorIndex.js +++ b/client/homebrew/pages/errorPage/errors/errorIndex.js @@ -174,7 +174,7 @@ const errorIndex = (props)=>{ '90' : dedent` An unexpected error occurred while looking for these brews. Try again in a few minutes.`, - + '91' : dedent` An unexpected error occurred while trying to get the total of brews.`, }; }; diff --git a/client/homebrew/pages/vaultPage/vaultPage.jsx b/client/homebrew/pages/vaultPage/vaultPage.jsx index cbd31dc85..4c3ef6700 100644 --- a/client/homebrew/pages/vaultPage/vaultPage.jsx +++ b/client/homebrew/pages/vaultPage/vaultPage.jsx @@ -15,7 +15,7 @@ const ErrorIndex = require('../errorPage/errors/errorIndex.js'); const request = require('../../utils/request-middleware.js'); -const VaultPage = (props) => { +const VaultPage = (props)=>{ const [pageState, setPageState] = useState(parseInt(props.query.page) || 1); //Response state @@ -32,33 +32,33 @@ const VaultPage = (props) => { const legacyRef = useRef(null); const submitButtonRef = useRef(null); - useEffect(() => { + useEffect(()=>{ disableSubmitIfFormInvalid(); loadPage(pageState, true); }, []); - const updateStateWithBrews = (brews, page) => { + const updateStateWithBrews = (brews, page)=>{ setBrewCollection(brews || null); setPageState(parseInt(page) || 1); setSearching(false); }; - const updateUrl = (titleValue, authorValue, countValue, v3Value, legacyValue, page) => { + const updateUrl = (titleValue, authorValue, countValue, v3Value, legacyValue, page)=>{ const url = new URL(window.location.href); const urlParams = new URLSearchParams(url.search); - + urlParams.set('title', titleValue); urlParams.set('author', authorValue); urlParams.set('count', countValue); urlParams.set('v3', v3Value); urlParams.set('legacy', legacyValue); urlParams.set('page', page); - + url.search = urlParams.toString(); window.history.replaceState(null, '', url.toString()); }; - const performSearch = async (title, author, count, v3, legacy, page) => { + const performSearch = async (title, author, count, v3, legacy, page)=>{ updateUrl(title, author, count, v3, legacy, page); const response = await request.get( @@ -71,14 +71,14 @@ const VaultPage = (props) => { ); updateStateWithBrews([], 1); }); - - if (response.ok) + + if(response.ok) updateStateWithBrews(response.body.brews, page); }; - const loadTotal = async (title, author, v3, legacy) => { + const loadTotal = async (title, author, v3, legacy)=>{ setTotalBrews(null); - + const response = await request.get( `/api/vault/total?title=${title}&author=${author}&v3=${v3}&legacy=${legacy}` ).catch((error)=>{ @@ -89,13 +89,13 @@ const VaultPage = (props) => { ); updateStateWithBrews([], 1); }); - - if (response.ok) + + if(response.ok) setTotalBrews(response.body.totalBrews); }; - const loadPage = async (page, updateTotal) => { - if (!validateForm()) + const loadPage = async (page, updateTotal)=>{ + if(!validateForm()) return; setSearching(true); @@ -109,14 +109,14 @@ const VaultPage = (props) => { performSearch(title, author, count, v3, legacy, page); - if (updateTotal) + if(updateTotal) loadTotal(title, author, v3, legacy); }; - const renderNavItems = () => ( + const renderNavItems = ()=>( - + Vault: Search for brews @@ -129,7 +129,7 @@ const VaultPage = (props) => { ); - const validateForm = () => { + const validateForm = ()=>{ //form validity: title or author must be written, and at least one renderer set const isTitleValid = titleRef.current.validity.valid && titleRef.current.value; const isAuthorValid = authorRef.current.validity.valid && authorRef.current.value; @@ -140,29 +140,29 @@ const VaultPage = (props) => { return isFormValid; }; - const disableSubmitIfFormInvalid = () => { + const disableSubmitIfFormInvalid = ()=>{ submitButtonRef.current.disabled = !validateForm(); }; - const renderForm = () => ( -
-

Brew Lookup

-
+ const renderForm = ()=>( +
+

Brew Lookup

+
@@ -170,34 +170,34 @@ const VaultPage = (props) => { Author of the brew { - if (e.key === 'Enter' && !submitButtonRef.current.disabled) + onKeyDown={(e)=>{ + if(e.key === 'Enter' && !submitButtonRef.current.disabled) loadPage(1, true); }} - placeholder="Username" + placeholder='Username' />
); - const renderPaginationControls = () => { - if (!totalBrews) return null; + const renderPaginationControls = ()=>{ + if(!totalBrews) return null; const countInt = parseInt(props.query.count || 20); const totalPages = Math.ceil(totalBrews / countInt); let startPage, endPage; - if (pageState <= 6) { + if(pageState <= 6) { startPage = 1; endPage = Math.min(totalPages, 10); - } else if (pageState + 4 >= totalPages) { + } else if(pageState + 4 >= totalPages) { startPage = Math.max(1, totalPages - 9); endPage = totalPages; } else { @@ -273,32 +273,32 @@ const VaultPage = (props) => { const pagesAroundCurrent = new Array(endPage - startPage + 1) .fill() - .map((_, index) => ( + .map((_, index)=>( loadPage(startPage + index, false)} + onClick={()=>loadPage(startPage + index, false)} > {startPage + index} )); return ( -
+
-
    +
      {startPage > 1 && ( loadPage(1, false)} + className='pageNumber firstPage' + onClick={()=>loadPage(1, false)} > 1 ... @@ -306,90 +306,90 @@ const VaultPage = (props) => { {pagesAroundCurrent} {endPage < totalPages && ( loadPage(totalPages, false)} + className='pageNumber lastPage' + onClick={()=>loadPage(totalPages, false)} > ... {totalPages} )}
); }; - const renderFoundBrews = () => { - if (searching) { + const renderFoundBrews = ()=>{ + if(searching) { return ( -
-

Searching

+
+

Searching

); } - if (error) { + if(error) { const errorText = ErrorIndex({ brew })[brew.HBErrorCode.toString()] || ''; console.log('render Error: ', error); return ( -
+

Error: {errorText}

); } - if (!brewCollection) { + if(!brewCollection) { return ( -
+

No search yet

); } - if (brewCollection.length === 0) { + if(brewCollection.length === 0) { return ( -
+

No brews found

); } return ( -
- - {`Brews found: `} - {totalBrews} - - {brewCollection.map((brew, index) => { - return ( - - ); - })} - {renderPaginationControls()} -
- ); - }; +
+ + {`Brews found: `} + {totalBrews} + + {brewCollection.map((brew, index)=>{ + return ( + + ); + })} + {renderPaginationControls()} +
+ ); + }; return ( -
- - +
+ + {renderNavItems()} -
+
-
{renderForm()}
+
{renderForm()}
-
+
{renderFoundBrews()}
diff --git a/server/vault.api.js b/server/vault.api.js index fc31a0180..41ceeab8e 100644 --- a/server/vault.api.js +++ b/server/vault.api.js @@ -4,32 +4,32 @@ const HomebrewModel = require('./homebrew.model.js').model; const router = express.Router(); -const titleConditions = (title) => { - if (!title) return {}; +const titleConditions = (title)=>{ + if(!title) return {}; return { - $text: { - $search: title, - $caseSensitive: false, + $text : { + $search : title, + $caseSensitive : false, }, }; }; -const authorConditions = (author) => { - if (!author) return {}; +const authorConditions = (author)=>{ + if(!author) return {}; return { authors: author }; }; -const rendererConditions = (legacy, v3) => { - if (legacy === 'true' && v3 !== 'true') - return { renderer: 'legacy'}; +const rendererConditions = (legacy, v3)=>{ + if(legacy === 'true' && v3 !== 'true') + return { renderer: 'legacy' }; - if (v3 === 'true' && legacy !== 'true') - return { renderer: 'V3'}; + if(v3 === 'true' && legacy !== 'true') + return { renderer: 'V3' }; return {}; // If all renderers selected, renderer field not needed in query for speed }; -const findBrews = async (req, res) => { +const findBrews = async (req, res)=>{ const title = req.query.title || ''; const author = req.query.author || ''; const page = Math.max(parseInt(req.query.page) || 1, 1); @@ -37,7 +37,7 @@ const findBrews = async (req, res) => { const skip = (page - 1) * count; const combinedQuery = { - $and: [ + $and : [ { published: true }, rendererConditions(req.query.legacy, req.query.v3), titleConditions(title), @@ -58,28 +58,27 @@ const findBrews = async (req, res) => { .limit(count) .maxTimeMS(5000) .exec() - .then((brews) => { + .then((brews)=>{ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; - const processedBrews = brews.map((brew) => { - brew.authors = brew.authors.map(author => - emailRegex.test(author) ? 'hidden' : author + const processedBrews = brews.map((brew)=>{ + brew.authors = brew.authors.map((author)=>emailRegex.test(author) ? 'hidden' : author ); return brew; }); res.json({ brews: processedBrews, page }); }) - .catch((error) => { - throw {...error, message: "Error finding brews in Vault search", HBErrorCode: 90}; + .catch((error)=>{ + throw { ...error, message: 'Error finding brews in Vault search', HBErrorCode: 90 }; }); }; -const findTotal = async (req, res) => { +const findTotal = async (req, res)=>{ const title = req.query.title || ''; const author = req.query.author || ''; const combinedQuery = { - $and: [ + $and : [ { published: true }, rendererConditions(req.query.legacy, req.query.v3), titleConditions(title), @@ -88,12 +87,12 @@ const findTotal = async (req, res) => { }; await HomebrewModel.countDocuments(combinedQuery) - .then((totalBrews) => { - console.log(`when returning, the total of brews is ${totalBrews} for the query ${JSON.stringify(combinedQuery)}`); + .then((totalBrews)=>{ + console.log(`when returning, the total of brews is ${totalBrews} for the query ${JSON.stringify(combinedQuery)}`); res.json({ totalBrews }); }) - .catch((error) => { - throw {...error, message: "Error finding brews in Vault search findTotal function", HBErrorCode: 91}; + .catch((error)=>{ + throw { ...error, message: 'Error finding brews in Vault search findTotal function', HBErrorCode: 91 }; }); };