From a0d1c77f48bb1f9c7975cdddd26742725fccb6fe Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 12 Sep 2021 23:27:03 +0200 Subject: [PATCH] Issue-545: WIP Create SearchClient component --- src/components/HeaderHome.jsx | 3 +- src/components/Search/SearchClient.jsx | 159 +++++++++++++++++++ src/components/Search/SearchComponent.jsx | 183 ++++------------------ 3 files changed, 188 insertions(+), 157 deletions(-) create mode 100644 src/components/Search/SearchClient.jsx diff --git a/src/components/HeaderHome.jsx b/src/components/HeaderHome.jsx index 2ad511fa..5895c5cb 100644 --- a/src/components/HeaderHome.jsx +++ b/src/components/HeaderHome.jsx @@ -3,7 +3,7 @@ import { StaticQuery, graphql } from 'gatsby' import { ReactComponent as Logo } from '@oceanprotocol/art/logo/logo.svg' import Content from '../components/Content' import styles from './HeaderHome.module.scss' - +import SearchComponent from './Search/SearchComponent' const HeaderHome = () => ( (

{siteTitle}

{siteDescription}

+ ) diff --git a/src/components/Search/SearchClient.jsx b/src/components/Search/SearchClient.jsx new file mode 100644 index 00000000..d08fea5e --- /dev/null +++ b/src/components/Search/SearchClient.jsx @@ -0,0 +1,159 @@ +import React, { useState, useEffect } from 'react' +import * as JsSearch from 'js-search' +import { Link } from 'gatsby' +import PropTypes from 'prop-types' +import { makeStyles } from '@material-ui/core/styles' +import Modal from '@material-ui/core/Modal' +import Backdrop from '@material-ui/core/Backdrop' +import Fade from '@material-ui/core/Fade' +import List from '@material-ui/core/List' +import ListItem from '@material-ui/core/ListItem' +import styles from './SearchComponent.module.scss' +import Button from '@material-ui/core/Button' +import SearchIcon from '@material-ui/icons/Search' + +const useStyles = makeStyles((theme) => ({ + modal: { + display: 'flex', + alignItems: 'top', + justifyContent: 'center' + }, + paper: { + backgroundColor: theme.palette.background.paper, + boxShadow: theme.shadows[5], + padding: theme.spacing(2, 4, 3), + margin: theme.spacing(3) + } +})) + +const SearchClient = ({ searchableData }) => { + const [searchState, setSearchState] = useState({ + isLoading: true, + searchResults: [], + search: null, + isError: false, + termFrequency: true, + removeStopWords: false, + searchQuery: '', + selectedStrategy: '', + selectedSanitizer: '', + touched: false + }) + + const classes = useStyles() + const [open, setOpen] = React.useState(false) + + const handleOpen = () => { + setOpen(true) + } + + const handleClose = () => { + setOpen(false) + } + + useEffect(() => { + rebuildIndex(searchableData) + }, []) + + const rebuildIndex = (searchableData) => { + // const { + // removeStopWords, + // selectedStrategy, + // selectedSanitizer, + // termFrequency + // } = searchState + const dataToSearch = new JsSearch.Search('title') + dataToSearch.addIndex('title') + + dataToSearch.addDocuments(searchableData) + setSearchState({ + ...searchState, + isLoading: false, + search: dataToSearch + }) + } + + const searchData = (e) => { + const { search } = searchState + const queryResult = search.search(e.target.value) + setSearchState({ + ...searchState, + touched: true, + searchQuery: e.target.value, + searchResults: queryResult + }) + } + const handleSubmit = (e) => { + e.preventDefault() + } + + return ( +
+ + + +
+
+
+
+ +
+
+
+ {searchState.touched ? ( + + ) : null} +
+
+
+
+
+
+ ) +} + +SearchClient.propTypes = { + searchableData: PropTypes.array.isRequired +} + +const ResultList = ({ searchResults }) => { + return ( +
+
Total results found: {searchResults.length}
+ + {searchResults.map((element) => ( + + {element.title} + + ))} + +
+ ) +} + +ResultList.propTypes = { + searchResults: PropTypes.array.isRequired +} + +export default SearchClient diff --git a/src/components/Search/SearchComponent.jsx b/src/components/Search/SearchComponent.jsx index 8ce867b4..3254bced 100644 --- a/src/components/Search/SearchComponent.jsx +++ b/src/components/Search/SearchComponent.jsx @@ -1,32 +1,12 @@ -import React, { useState, useEffect } from 'react' -import * as JsSearch from 'js-search' -import { Link } from 'gatsby' +import React from 'react' + +import { graphql } from 'gatsby' import PropTypes from 'prop-types' -import { makeStyles } from '@material-ui/core/styles' -import Modal from '@material-ui/core/Modal' -import Backdrop from '@material-ui/core/Backdrop' -import Fade from '@material-ui/core/Fade' -import List from '@material-ui/core/List' -import ListItem from '@material-ui/core/ListItem' -import styles from './SearchComponent.module.scss' -import Button from '@material-ui/core/Button' -import SearchIcon from '@material-ui/icons/Search' -const useStyles = makeStyles((theme) => ({ - modal: { - display: 'flex', - alignItems: 'top', - justifyContent: 'center' - }, - paper: { - backgroundColor: theme.palette.background.paper, - boxShadow: theme.shadows[5], - padding: theme.spacing(2, 4, 3), - margin: theme.spacing(3) - } -})) +import SearchClient from './SearchClient' -const SearchComponent = ({ pageContext }) => { +const SearchComponent = ({ data, pageContext }) => { + console.log('data', data) const { searchData } = pageContext const searchableData = searchData.map(({ node }) => { return { @@ -38,143 +18,34 @@ const SearchComponent = ({ pageContext }) => { }) return ( <> - + ) } SearchComponent.propTypes = { - pageContext: PropTypes.object.isRequired + pageContext: PropTypes.object, + data: PropTypes.object } -const ClientSearch = ({ searchableData }) => { - const [searchState, setSearchState] = useState({ - isLoading: true, - searchResults: [], - search: null, - isError: false, - termFrequency: true, - removeStopWords: false, - searchQuery: '', - selectedStrategy: '', - selectedSanitizer: '', - touched: false - }) - - const classes = useStyles() - const [open, setOpen] = React.useState(false) - - const handleOpen = () => { - setOpen(true) +export const SearchComponentQuery = graphql` + query { + allMarkdownRemark(filter: { fileAbsolutePath: { regex: "/content/" } }) { + edges { + node { + fields { + slug + section + } + frontmatter { + title + description + } + id + } + } + } } - - const handleClose = () => { - setOpen(false) - } - - useEffect(() => { - rebuildIndex(searchableData) - }, []) - - const rebuildIndex = (searchableData) => { - // const { - // removeStopWords, - // selectedStrategy, - // selectedSanitizer, - // termFrequency - // } = searchState - const dataToSearch = new JsSearch.Search('title') - dataToSearch.addIndex('title') - - dataToSearch.addDocuments(searchableData) - setSearchState({ - ...searchState, - isLoading: false, - search: dataToSearch - }) - } - - const searchData = (e) => { - const { search } = searchState - const queryResult = search.search(e.target.value) - setSearchState({ - ...searchState, - touched: true, - searchQuery: e.target.value, - searchResults: queryResult - }) - } - const handleSubmit = (e) => { - e.preventDefault() - } - - return ( -
- - - -
-
-
-
- -
-
-
- {searchState.touched ? ( - - ) : null} -
-
-
-
-
-
- ) -} - -ClientSearch.propTypes = { - searchableData: PropTypes.array.isRequired -} - -const ResultList = ({ searchResults }) => { - return ( -
-
Total results found: {searchResults.length}
- - {searchResults.map((element) => ( - - {element.title} - - ))} - -
- ) -} - -ResultList.propTypes = { - searchResults: PropTypes.array.isRequired -} +` export default SearchComponent