added form logic and aligned search items from top
This commit is contained in:
parent
2c2f586420
commit
3841aabcbb
1 changed files with 83 additions and 76 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { useEffect, useState } from "react"
|
import { useEffect, useState, useRef } from "react"
|
||||||
import styled from "styled-components"
|
import styled from "styled-components"
|
||||||
import { useLocation, useHistory } from "react-router-dom"
|
import { useLocation, useHistory } from "react-router-dom"
|
||||||
import { Helmet } from "react-helmet-async"
|
import { Helmet } from "react-helmet-async"
|
||||||
|
@ -29,16 +29,16 @@ const StyledSearch = styled.div`
|
||||||
|
|
||||||
const StyledSearchContainer = styled.div`
|
const StyledSearchContainer = styled.div`
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: flex-start;
|
||||||
|
|
||||||
@media screen and (max-width: ${theming.size.screen_size2}) {
|
@media screen and (max-width: ${theming.size.screen_size2}) {
|
||||||
flex-direction: column-reverse;
|
flex-direction: column-reverse;
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
const StyledSearchControlContainer = styled.div`
|
const StyledSearchControlContainer = styled.form`
|
||||||
width: 100%;
|
width: 100%;
|
||||||
margin: 0 0 0 1rem;
|
margin-left: 1rem;
|
||||||
|
|
||||||
@media screen and (max-width: ${theming.size.screen_size2}) {
|
@media screen and (max-width: ${theming.size.screen_size2}) {
|
||||||
margin-top: 2rem;
|
margin-top: 2rem;
|
||||||
|
@ -87,9 +87,7 @@ export default function Search() {
|
||||||
|
|
||||||
function _Search() {
|
function _Search() {
|
||||||
const [index, setIndex] = useState({} as elasticlunr.Index<unknown>)
|
const [index, setIndex] = useState({} as elasticlunr.Index<unknown>)
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null)
|
||||||
// load search index only once
|
|
||||||
useEffect(() => setIndex(elasticlunr.Index.load(searchIndex as never)), [])
|
|
||||||
|
|
||||||
const _history = useHistory()
|
const _history = useHistory()
|
||||||
const _location = useLocation()
|
const _location = useLocation()
|
||||||
|
@ -124,6 +122,64 @@ function _Search() {
|
||||||
|
|
||||||
const [searchInput, setSearchInput] = useState(query.query)
|
const [searchInput, setSearchInput] = useState(query.query)
|
||||||
|
|
||||||
|
function doSearch() {
|
||||||
|
if (inputRef.current) inputRef.current.blur()
|
||||||
|
|
||||||
|
_history.push({
|
||||||
|
pathname: "/search",
|
||||||
|
search: queryString.stringify({
|
||||||
|
...(query.from && {
|
||||||
|
from: query.from,
|
||||||
|
}),
|
||||||
|
...(query.to && {
|
||||||
|
to: query.to,
|
||||||
|
}),
|
||||||
|
...(query.tags.length > 0 && {
|
||||||
|
tags: query.tags.join(","),
|
||||||
|
}),
|
||||||
|
query: searchInput,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
try {
|
||||||
|
const _postCards: unknown[] = []
|
||||||
|
for (const res of index.search(searchInput)) {
|
||||||
|
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) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// load search index only once
|
||||||
|
useEffect(() => {
|
||||||
|
setIndex(elasticlunr.Index.load(searchIndex as never))
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
doSearch()
|
||||||
|
}, [dateRange])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Helmet>
|
<Helmet>
|
||||||
|
@ -173,70 +229,21 @@ function _Search() {
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<StyledSearchControlContainer>
|
<StyledSearchControlContainer
|
||||||
|
onSubmit={(event) => {
|
||||||
|
event.preventDefault()
|
||||||
|
doSearch()
|
||||||
|
}}
|
||||||
|
>
|
||||||
<StyledInput
|
<StyledInput
|
||||||
type="text"
|
type="text"
|
||||||
|
ref={inputRef}
|
||||||
value={searchInput}
|
value={searchInput}
|
||||||
onChange={(event) =>
|
onChange={(event) =>
|
||||||
setSearchInput(event.target.value)
|
setSearchInput(event.target.value)
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<button
|
<input type="submit" value="Search"></input>
|
||||||
onClick={() => {
|
|
||||||
_history.push({
|
|
||||||
pathname: "/search",
|
|
||||||
search: queryString.stringify({
|
|
||||||
...(query.from && {
|
|
||||||
from: query.from,
|
|
||||||
}),
|
|
||||||
...(query.to && {
|
|
||||||
to: query.to,
|
|
||||||
}),
|
|
||||||
...(query.tags.length > 0 && {
|
|
||||||
tags: query.tags.join(","),
|
|
||||||
}),
|
|
||||||
query: searchInput,
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
try {
|
|
||||||
const _postCards: unknown[] = []
|
|
||||||
for (const res of index.search(
|
|
||||||
searchInput
|
|
||||||
)) {
|
|
||||||
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) {}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
Search
|
|
||||||
</button>
|
|
||||||
<br />
|
<br />
|
||||||
<br />
|
<br />
|
||||||
<small>
|
<small>
|
||||||
|
@ -251,7 +258,8 @@ function _Search() {
|
||||||
<br />
|
<br />
|
||||||
date to: {query.to}
|
date to: {query.to}
|
||||||
<br />
|
<br />
|
||||||
<button
|
<input
|
||||||
|
type="submit"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
_history.push({
|
_history.push({
|
||||||
pathname: "/search",
|
pathname: "/search",
|
||||||
|
@ -269,11 +277,11 @@ function _Search() {
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
}}
|
}}
|
||||||
>
|
value="Search test 1"
|
||||||
Search test 1
|
/>
|
||||||
</button>
|
|
||||||
|
|
|
|
||||||
<button
|
<input
|
||||||
|
type="submit"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
_history.push({
|
_history.push({
|
||||||
pathname: "/search",
|
pathname: "/search",
|
||||||
|
@ -291,11 +299,11 @@ function _Search() {
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
}}
|
}}
|
||||||
>
|
value="Search test 2"
|
||||||
Search test 2
|
/>
|
||||||
</button>
|
|
||||||
<br />
|
<br />
|
||||||
<button
|
<input
|
||||||
|
type="submit"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
_history.push({
|
_history.push({
|
||||||
pathname: "/search",
|
pathname: "/search",
|
||||||
|
@ -311,9 +319,8 @@ function _Search() {
|
||||||
|
|
||||||
setDateRange(defaultDateRange)
|
setDateRange(defaultDateRange)
|
||||||
}}
|
}}
|
||||||
>
|
value="clear date"
|
||||||
clear date
|
/>
|
||||||
</button>
|
|
||||||
<br />
|
<br />
|
||||||
</StyledSearchControlContainer>
|
</StyledSearchControlContainer>
|
||||||
</StyledSearchContainer>
|
</StyledSearchContainer>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue