Skip to content

Commit

Permalink
FIX: Advanced Search: Only show results when search is submitted
Browse files Browse the repository at this point in the history
  • Loading branch information
dwhieb committed Jul 28, 2024
1 parent e23f6e1 commit 07a101a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
10 changes: 5 additions & 5 deletions layouts/main/components/nav/nav.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
</button>

<ul aria-labelledby=nav-heading class=links data-hook=nav__links role=list>
<li><a href=/ {{/if}}>About</a></li>
<li><a href=/search {{/if}}>Explore the Data</a></li>
<li><a href=/grammar {{/if}}>Algonquian Word-Structure Basics</a></li>
<li><a href=/research {{/if}}>Research</a></li>
<li><a href=/bibliography {{/if}}>Bibliography</a></li>
<li><a href=/>About</a></li>
<li><a href=/search>Explore the Data</a></li>
<li><a href=/grammar>Algonquian Word-Structure Basics</a></li>
<li><a href=/research>Research</a></li>
<li><a href=/bibliography>Bibliography</a></li>
</ul>

</nav>
31 changes: 19 additions & 12 deletions pages/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import changeParam from './scripts/changeParam.js'
import SortDirectives from '../../scripts/SortDirectives.js'
import toCSV from './scripts/toCSV.js'

const defaults = {
limit: 100,
offset: 0,
sort: ``,
}

export function Search(req, res) {

const { db } = req.app
const query = new Map(Object.entries(req.query))

const context = {
advanced: query.has(`advanced`),
languages: db.languages.toJSON().sort((a, b) => a.name.localeCompare(b.name)),
numComponents: db.index.size.toLocaleString(),
numLanguages: db.languages.size.toLocaleString(),
Expand All @@ -17,21 +25,19 @@ export function Search(req, res) {
}

// No query submitted. Load default search page.
if (!(`q` in req.query || `advanced` in req.query || `language` in req.query)) {
if (
!query.size
|| (query.size === 1 && query.has(`advanced`))
) {
return res.render(`Search/Search`, context)
}

let {
limit = 100,
offset = 0,
q,
sort = ``,
} = req.query

// Search

let results = []

// NB: The Database methods expect options to be an Object rather than a Map.
// Use the original req.query rather than the Mappified version.
if (req.query.advanced) {
results = req.app.db.search(req.query)
} else {
Expand All @@ -42,7 +48,8 @@ export function Search(req, res) {

// Sort

sort = new SortDirectives(sort)
const sortQuery = query.get(`sort`) ?? defaults.sort
const sort = new SortDirectives(sortQuery)

if (sort.size) {
results.sort((a, b) => {
Expand All @@ -63,8 +70,9 @@ export function Search(req, res) {
// Pagination
// NB: offset = # of records to SKIP

limit = Number(limit)
offset = Number(offset)
const limit = Number(query.get(`limit`) ?? defaults.limit)
const offset = Number(query.get(`offset`) ?? defaults.offset)

results = results.slice(offset, offset + limit)

const numAdjacentPages = 5
Expand Down Expand Up @@ -118,7 +126,6 @@ export function Search(req, res) {
// Render page

Object.assign(context, {
advanced: req.query.advanced,
hasResults: true,
numResults: results.length.toLocaleString(),
pagination: {
Expand Down

0 comments on commit 07a101a

Please sign in to comment.