worked on preparing search query
This commit is contained in:
parent
632f0c859a
commit
4bccb7f883
1 changed files with 124 additions and 58 deletions
|
@ -1,6 +1,6 @@
|
||||||
import { useState } from "react"
|
import { useState } from "react"
|
||||||
import styled from "styled-components"
|
import styled from "styled-components"
|
||||||
import { Link, BrowserRouter, useLocation } from "react-router-dom"
|
import { BrowserRouter, useLocation, useHistory } from "react-router-dom"
|
||||||
import { Helmet } from "react-helmet-async"
|
import { Helmet } from "react-helmet-async"
|
||||||
import { DateRange } from "react-date-range"
|
import { DateRange } from "react-date-range"
|
||||||
import queryString from "query-string"
|
import queryString from "query-string"
|
||||||
|
@ -13,19 +13,36 @@ import map from "../data/map.json"
|
||||||
import Tag from "../components/Tag"
|
import Tag from "../components/Tag"
|
||||||
|
|
||||||
const StyledSearch = styled.div`
|
const StyledSearch = styled.div`
|
||||||
margin: auto;
|
|
||||||
margin-top: 2rem;
|
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
|
||||||
color: ${(props) =>
|
color: ${(props) =>
|
||||||
theming.theme(props.theme.currentTheme, {
|
theming.theme(props.theme.currentTheme, {
|
||||||
light: "#111111",
|
light: theming.dark.color1,
|
||||||
dark: "#EEEEEE",
|
dark: theming.dark.color1,
|
||||||
})};
|
})};
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const StyledSearchContainer = styled.div`
|
||||||
|
display: flex;
|
||||||
|
|
||||||
|
@media screen and (max-width: ${theming.size.screen_size2}) {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
const StyledSearchControlContainer = styled.div`
|
||||||
|
width: 100%;
|
||||||
|
margin: 0 0 0 1rem;
|
||||||
|
|
||||||
|
@media screen and (max-width: ${theming.size.screen_size2}) {
|
||||||
|
margin-top: 2rem;
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
const StyledSearchResult = styled.div``
|
||||||
|
|
||||||
const StyledTagTable = styled.table`
|
const StyledTagTable = styled.table`
|
||||||
margin-left: auto;
|
margin: 0 auto 0 auto;
|
||||||
margin-right: auto;
|
|
||||||
`
|
`
|
||||||
|
|
||||||
export default function Search() {
|
export default function Search() {
|
||||||
|
@ -36,11 +53,20 @@ export default function Search() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// have to be in a separate component for tags to update when the urls change
|
||||||
|
// todo: check if using keys will fix the issue
|
||||||
function _Search() {
|
function _Search() {
|
||||||
const parsedQuery = queryString.parse(useLocation().search)
|
const _history = useHistory()
|
||||||
parsedQuery.tags = parsedQuery.tags
|
const _location = useLocation()
|
||||||
? (parsedQuery.tags as string).split(",")
|
|
||||||
: []
|
// todo: handle duplicate/missing keys
|
||||||
|
const _query = queryString.parse(_location.search)
|
||||||
|
|
||||||
|
const query = {
|
||||||
|
from: _query.from ? _query.from?.toString() : "",
|
||||||
|
to: _query.to ? _query.to?.toString() : "",
|
||||||
|
tags: _query.tags ? _query.tags.toString().split(",") : [],
|
||||||
|
}
|
||||||
|
|
||||||
const [dateRange, setDateRange] = useState([
|
const [dateRange, setDateRange] = useState([
|
||||||
{
|
{
|
||||||
|
@ -66,53 +92,93 @@ function _Search() {
|
||||||
</Helmet>
|
</Helmet>
|
||||||
|
|
||||||
<StyledSearch className="card main-content">
|
<StyledSearch className="card main-content">
|
||||||
<DateRange
|
<h1>Search</h1>
|
||||||
editableDateInputs={true}
|
|
||||||
moveRangeOnFirstSelection={false}
|
<StyledSearchContainer>
|
||||||
retainEndDateOnFirstSelection={true}
|
<DateRange
|
||||||
ranges={dateRange}
|
editableDateInputs={true}
|
||||||
onChange={(item) => {
|
moveRangeOnFirstSelection={false}
|
||||||
setDateRange([item.selection])
|
retainEndDateOnFirstSelection={true}
|
||||||
}}
|
ranges={dateRange}
|
||||||
/>
|
onChange={(item) => {
|
||||||
<br />
|
const historyToPush = {
|
||||||
available tags:
|
from: query.from,
|
||||||
<small>
|
to: query.to,
|
||||||
<StyledTagTable>
|
tags: query.tags.join(","),
|
||||||
{map.meta.tags.map((tag) => {
|
}
|
||||||
return (
|
|
||||||
<td key={tag}>
|
// convert Date to YYYY-MM-DD string if it exists
|
||||||
<Tag text={tag} />
|
if (item.selection.startDate != null)
|
||||||
</td>
|
historyToPush.from = item.selection.startDate
|
||||||
)
|
.toISOString()
|
||||||
})}
|
.split("T")[0]
|
||||||
</StyledTagTable>
|
|
||||||
</small>
|
if (item.selection.endDate != null)
|
||||||
<br />
|
historyToPush.to = item.selection.endDate
|
||||||
<br />
|
.toISOString()
|
||||||
Selected tags:
|
.split("T")[0]
|
||||||
<small>
|
|
||||||
<StyledTagTable>
|
_history.push({
|
||||||
{parsedQuery.tags?.map((tag) => {
|
pathname: "/search",
|
||||||
return (
|
search: queryString.stringify(historyToPush),
|
||||||
<td key={tag}>
|
})
|
||||||
<Tag text={tag} />
|
|
||||||
</td>
|
setDateRange([item.selection])
|
||||||
)
|
}}
|
||||||
})}
|
/>
|
||||||
</StyledTagTable>
|
|
||||||
</small>
|
<StyledSearchControlContainer>
|
||||||
<br />
|
<input type="text" />
|
||||||
date from: {parsedQuery.from}
|
<br />
|
||||||
<br />
|
<br />
|
||||||
date to: {parsedQuery.to}
|
<small>
|
||||||
<br />
|
<StyledTagTable>
|
||||||
<Link to="/search?&from=YYYYMMDD&to=YYYYMMDD&tags=include,!exclude">
|
{query.tags?.map((tag) => {
|
||||||
Search1
|
return (
|
||||||
</Link>
|
<td key={tag}>
|
||||||
<Link to="/search?&from=YYYYMMDD&to=YYYYMMDD&tags=include2,!exclude2">
|
<Tag text={tag} />
|
||||||
Search2
|
</td>
|
||||||
</Link>
|
)
|
||||||
|
})}
|
||||||
|
</StyledTagTable>
|
||||||
|
</small>
|
||||||
|
<br />
|
||||||
|
date from: {query.from}
|
||||||
|
<br />
|
||||||
|
date to: {query.to}
|
||||||
|
<br />
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
_history.push({
|
||||||
|
pathname: "/search",
|
||||||
|
search: queryString.stringify({
|
||||||
|
from: query.from,
|
||||||
|
to: query.to,
|
||||||
|
tags: "include,!exclude",
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Search test 1
|
||||||
|
</button>
|
||||||
|
|
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
_history.push({
|
||||||
|
pathname: "/search",
|
||||||
|
search: queryString.stringify({
|
||||||
|
from: query.from,
|
||||||
|
to: query.to,
|
||||||
|
tags: "include2,!exclude2",
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Search test 2
|
||||||
|
</button>
|
||||||
|
</StyledSearchControlContainer>
|
||||||
|
</StyledSearchContainer>
|
||||||
|
<StyledSearchResult>{map.meta.tags}</StyledSearchResult>
|
||||||
</StyledSearch>
|
</StyledSearch>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue