Skip to content

Commit

Permalink
fix(Next > useSeach): Move searchBox to hook
Browse files Browse the repository at this point in the history
  • Loading branch information
danactive committed Dec 12, 2021
1 parent e2da9be commit eaea8da
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
10 changes: 2 additions & 8 deletions app/pages/[gallery]/[album].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,16 @@ export async function getStaticPaths() {
const AlbumPage = ({ items = [] }) => {
const {
filtered,
keyword,
setKeyword,
shareUrlStem,
searchBox,
} = useSearch(items)
const keywordResultLabel = keyword === '' ? null : (<> for &quot;{keyword}&quot;</>)

return (
<div>
<Head>
<title>History App - Album</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<h3>Search results {filtered?.length} of {items?.length}{keywordResultLabel}</h3>
<nav>{shareUrlStem}</nav>
<input type="text" onChange={(event) => setKeyword(event.target.value)} value={keyword} />
{searchBox}
<ul>
{filtered.map((item) => (
<li key={item.filename} id={`select${item.id}`}>
Expand Down
9 changes: 2 additions & 7 deletions app/pages/[gallery]/all.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,17 @@ const AllPage = ({ items = [] }) => {
const {
filtered,
keyword,
setKeyword,
shareUrlStem,
searchBox,
} = useSearch(items)
const showThumbnail = (kw = '') => kw.length > 2
const keywordResultLabel = keyword === '' ? null : (<> for &quot;{keyword}&quot;</>)

return (
<div>
<Head>
<title>History App - All</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<h3>Search results {filtered?.length} of {items?.length}{keywordResultLabel}</h3>
<nav>{shareUrlStem}</nav>
<input type="text" onChange={(event) => setKeyword(event.target.value)} value={keyword} />
{searchBox}
<ul>
{filtered.map((item) => (
<li key={item.filename}>
Expand Down
32 changes: 19 additions & 13 deletions app/src/hooks/useSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,31 @@ const useSearch = (items) => {
return `${router.asPath}?keyword=${keyword}`
}

const keywordResultLabel = keyword === '' ? null : (<> for &quot;{keyword}&quot;</>)
const getSearchBox = (filtered) => (
<>
<h3>Search results {filtered?.length} of {items?.length}{keywordResultLabel}</h3>
<nav>{getShareUrlStem()}</nav>
<input type="text" onChange={(event) => setKeyword(event.target.value)} value={keyword} />
</>
)

const defaultReturn = {
filtered: items,
keyword: '',
setKeyword,
searchBox: getSearchBox(items),
}

useEffect(() => {
if (router.isReady && router.query.keyword) {
setKeyword(router.query.keyword)
}
return {
filtered: items,
keyword: '',
setKeyword,
shareUrlStem: getShareUrlStem(),
}
return defaultReturn
}, [router.isReady])

if (!router.isReady) {
return {
filtered: items,
keyword: '',
setKeyword,
shareUrlStem: getShareUrlStem(),
}
return defaultReturn
}

const filtered = items.filter((item) => {
Expand All @@ -56,7 +62,7 @@ const useSearch = (items) => {
filtered,
keyword,
setKeyword,
shareUrlStem: getShareUrlStem(),
searchBox: getSearchBox(filtered),
}
}

Expand Down

0 comments on commit eaea8da

Please sign in to comment.