changed input bar size, better query string conversion, and moved search button location
This commit is contained in:
parent
31c29df20d
commit
1fdd5dcc0c
1 changed files with 96 additions and 57 deletions
|
@ -54,6 +54,13 @@ const StyledDateRange = styled(DateRange)`
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const StyledInput = styled.input`
|
||||||
|
width: 100%;
|
||||||
|
height: 2rem;
|
||||||
|
|
||||||
|
background-color: ${theming.dark.color0};
|
||||||
|
`
|
||||||
|
|
||||||
function isDateInRange(
|
function isDateInRange(
|
||||||
dateToCompare: string,
|
dateToCompare: string,
|
||||||
from: string,
|
from: string,
|
||||||
|
@ -72,16 +79,16 @@ function isDateInRange(
|
||||||
return Date.parse(from) < compareDate && compareDate < Date.parse(to)
|
return Date.parse(from) < compareDate && compareDate < Date.parse(to)
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: find ways to get rid of wrapper component
|
// Search doesn't work on url change if component is not wrapped
|
||||||
|
// todo: find ways to get rid of wrapper component and use class component
|
||||||
export default function Search() {
|
export default function Search() {
|
||||||
return <_Search />
|
return <_Search />
|
||||||
}
|
}
|
||||||
|
|
||||||
// have to be in a separate component for tags to update when the urls change
|
|
||||||
// todo: check if using keys will allow me to use class components
|
|
||||||
function _Search() {
|
function _Search() {
|
||||||
const [index, setIndex] = useState({} as elasticlunr.Index<unknown>)
|
const [index, setIndex] = useState({} as elasticlunr.Index<unknown>)
|
||||||
|
|
||||||
|
// load search index only once
|
||||||
useEffect(() => setIndex(elasticlunr.Index.load(searchIndex as never)), [])
|
useEffect(() => setIndex(elasticlunr.Index.load(searchIndex as never)), [])
|
||||||
|
|
||||||
const _history = useHistory()
|
const _history = useHistory()
|
||||||
|
@ -94,11 +101,12 @@ function _Search() {
|
||||||
from: _query.from ? _query.from?.toString() : "",
|
from: _query.from ? _query.from?.toString() : "",
|
||||||
to: _query.to ? _query.to?.toString() : "",
|
to: _query.to ? _query.to?.toString() : "",
|
||||||
tags: _query.tags ? _query.tags.toString().split(",") : [],
|
tags: _query.tags ? _query.tags.toString().split(",") : [],
|
||||||
|
query: _query.query ? _query.query.toString() : "",
|
||||||
}
|
}
|
||||||
|
|
||||||
const [dateRange, setDateRange] = useState([
|
const [dateRange, setDateRange] = useState([
|
||||||
{
|
{
|
||||||
startDate: new Date(0),
|
startDate: new Date(0), // default date
|
||||||
endDate: null,
|
endDate: null,
|
||||||
key: "selection",
|
key: "selection",
|
||||||
},
|
},
|
||||||
|
@ -125,8 +133,12 @@ function _Search() {
|
||||||
ranges={dateRange}
|
ranges={dateRange}
|
||||||
onChange={(item) => {
|
onChange={(item) => {
|
||||||
const historyToPush = {
|
const historyToPush = {
|
||||||
|
...(query.from && {
|
||||||
from: query.from,
|
from: query.from,
|
||||||
|
}),
|
||||||
|
...(query.to && {
|
||||||
to: query.to,
|
to: query.to,
|
||||||
|
}),
|
||||||
tags: query.tags.join(","),
|
tags: query.tags.join(","),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,12 +163,68 @@ function _Search() {
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<StyledSearchControlContainer>
|
<StyledSearchControlContainer>
|
||||||
<input
|
<StyledInput
|
||||||
type="text"
|
type="text"
|
||||||
onChange={(event) =>
|
onChange={(event) =>
|
||||||
setSearchInput(event.target.value)
|
setSearchInput(event.target.value)
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
try {
|
||||||
|
const _postCards: unknown[] = []
|
||||||
|
for (const res of index.search(
|
||||||
|
query.query
|
||||||
|
)) {
|
||||||
|
const postData = map.posts[res.ref]
|
||||||
|
|
||||||
|
// if post data exists,
|
||||||
|
// date is within range,
|
||||||
|
// and if post include tags
|
||||||
|
if (
|
||||||
|
postData &&
|
||||||
|
isDateInRange(
|
||||||
|
postData.date,
|
||||||
|
query.from,
|
||||||
|
query.to
|
||||||
|
) &&
|
||||||
|
true
|
||||||
|
) {
|
||||||
|
_postCards.push(
|
||||||
|
<PostCard
|
||||||
|
key={res.ref}
|
||||||
|
postData={{
|
||||||
|
url: res.ref,
|
||||||
|
...postData,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// apply search result
|
||||||
|
setPostCards(_postCards)
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
} catch (err) {}
|
||||||
|
|
||||||
|
_history.push({
|
||||||
|
pathname: "/search",
|
||||||
|
search: queryString.stringify({
|
||||||
|
...(query.from && {
|
||||||
|
from: query.from,
|
||||||
|
}),
|
||||||
|
...(query.to && {
|
||||||
|
to: query.to,
|
||||||
|
}),
|
||||||
|
...(query.tags && {
|
||||||
|
tags: query.tags.join(","),
|
||||||
|
}),
|
||||||
|
query: searchInput,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Search
|
||||||
|
</button>
|
||||||
<br />
|
<br />
|
||||||
<br />
|
<br />
|
||||||
<small>
|
<small>
|
||||||
|
@ -176,8 +244,12 @@ function _Search() {
|
||||||
_history.push({
|
_history.push({
|
||||||
pathname: "/search",
|
pathname: "/search",
|
||||||
search: queryString.stringify({
|
search: queryString.stringify({
|
||||||
|
...(query.from && {
|
||||||
from: query.from,
|
from: query.from,
|
||||||
|
}),
|
||||||
|
...(query.to && {
|
||||||
to: query.to,
|
to: query.to,
|
||||||
|
}),
|
||||||
tags: "include,!exclude",
|
tags: "include,!exclude",
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
@ -191,8 +263,12 @@ function _Search() {
|
||||||
_history.push({
|
_history.push({
|
||||||
pathname: "/search",
|
pathname: "/search",
|
||||||
search: queryString.stringify({
|
search: queryString.stringify({
|
||||||
|
...(query.from && {
|
||||||
from: query.from,
|
from: query.from,
|
||||||
|
}),
|
||||||
|
...(query.to && {
|
||||||
to: query.to,
|
to: query.to,
|
||||||
|
}),
|
||||||
tags: "include2,!exclude2",
|
tags: "include2,!exclude2",
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
@ -203,57 +279,20 @@ function _Search() {
|
||||||
<br />
|
<br />
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setDateRange([
|
_history.push({
|
||||||
{
|
pathname: "/search",
|
||||||
startDate: new Date(0),
|
search: queryString.stringify({
|
||||||
endDate: null,
|
query: query.query,
|
||||||
key: "selection",
|
...(query.tags && {
|
||||||
},
|
tags: query.tags.join(","),
|
||||||
])
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
clear date
|
clear date
|
||||||
</button>
|
</button>
|
||||||
<br />
|
<br />
|
||||||
<button
|
|
||||||
onClick={() => {
|
|
||||||
try {
|
|
||||||
const _postCards: unknown[] = []
|
|
||||||
for (const res of index.search(
|
|
||||||
searchInput
|
|
||||||
)) {
|
|
||||||
const postData = map.posts[res.ref]
|
|
||||||
if (
|
|
||||||
// check if post data exists
|
|
||||||
postData &&
|
|
||||||
// check if date is within the range
|
|
||||||
isDateInRange(
|
|
||||||
postData.date,
|
|
||||||
query.from,
|
|
||||||
query.to
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
_postCards.push(
|
|
||||||
<PostCard
|
|
||||||
key={res.ref}
|
|
||||||
postData={{
|
|
||||||
url: res.ref,
|
|
||||||
...postData,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// apply search result
|
|
||||||
setPostCards(_postCards)
|
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line no-empty
|
|
||||||
} catch (err) {}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
Search
|
|
||||||
</button>
|
|
||||||
</StyledSearchControlContainer>
|
</StyledSearchControlContainer>
|
||||||
</StyledSearchContainer>
|
</StyledSearchContainer>
|
||||||
</StyledSearch>
|
</StyledSearch>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue