From 4bccb7f883875d54075e405bc3015c8b2aa009a9 Mon Sep 17 00:00:00 2001 From: developomp Date: Sat, 31 Jul 2021 22:36:59 +0900 Subject: [PATCH] worked on preparing search query --- source/src/pages/Search.tsx | 182 ++++++++++++++++++++++++------------ 1 file changed, 124 insertions(+), 58 deletions(-) diff --git a/source/src/pages/Search.tsx b/source/src/pages/Search.tsx index 7718b5b..06d7c15 100644 --- a/source/src/pages/Search.tsx +++ b/source/src/pages/Search.tsx @@ -1,6 +1,6 @@ import { useState } from "react" 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 { DateRange } from "react-date-range" import queryString from "query-string" @@ -13,19 +13,36 @@ import map from "../data/map.json" import Tag from "../components/Tag" const StyledSearch = styled.div` - margin: auto; - margin-top: 2rem; text-align: center; + color: ${(props) => theming.theme(props.theme.currentTheme, { - light: "#111111", - dark: "#EEEEEE", + light: theming.dark.color1, + 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` - margin-left: auto; - margin-right: auto; + margin: 0 auto 0 auto; ` 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() { - const parsedQuery = queryString.parse(useLocation().search) - parsedQuery.tags = parsedQuery.tags - ? (parsedQuery.tags as string).split(",") - : [] + const _history = useHistory() + const _location = useLocation() + + // 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([ { @@ -66,53 +92,93 @@ function _Search() { - { - setDateRange([item.selection]) - }} - /> -
- available tags: - - - {map.meta.tags.map((tag) => { - return ( - - - - ) - })} - - -
-
- Selected tags: - - - {parsedQuery.tags?.map((tag) => { - return ( - - - - ) - })} - - -
- date from: {parsedQuery.from} -
- date to: {parsedQuery.to} -
- - Search1 - - - Search2 - +

Search

+ + + { + const historyToPush = { + from: query.from, + to: query.to, + tags: query.tags.join(","), + } + + // convert Date to YYYY-MM-DD string if it exists + if (item.selection.startDate != null) + historyToPush.from = item.selection.startDate + .toISOString() + .split("T")[0] + + if (item.selection.endDate != null) + historyToPush.to = item.selection.endDate + .toISOString() + .split("T")[0] + + _history.push({ + pathname: "/search", + search: queryString.stringify(historyToPush), + }) + + setDateRange([item.selection]) + }} + /> + + + +
+
+ + + {query.tags?.map((tag) => { + return ( + + + + ) + })} + + +
+ date from: {query.from} +
+ date to: {query.to} +
+ + | + +
+
+ {map.meta.tags}
)