1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-26 19:49:26 +01:00

Issue-#545: Improve result render

This commit is contained in:
Akshay 2021-09-13 23:17:42 +02:00
parent efb1b20e6d
commit 4893625443
5 changed files with 79 additions and 29 deletions

View File

@ -179,19 +179,15 @@ exports.createPages = ({ graphql, actions }) => {
} }
const createSearchPage = async (createPage) => { const createSearchPage = async (createPage) => {
try { const pageTemplate = path.resolve(
const pageTemplate = path.resolve( './src/components/Search/SearchComponent.jsx'
'./src/components/Search/SearchComponent.jsx' )
) const url = `/search/`
const url = `/search/`
createPage({ createPage({
path: pageTemplate, path: url,
component: url component: pageTemplate
}) })
} catch (error) {
console.log(error.message)
}
} }
const createDeploymentsPage = async (createPage) => { const createDeploymentsPage = async (createPage) => {

View File

@ -5,8 +5,8 @@ import { IconButton } from '@material-ui/core'
import SearchIcon from '@material-ui/icons/Search' import SearchIcon from '@material-ui/icons/Search'
const SearchButton = () => { const SearchButton = () => {
return ( return (
<IconButton> <IconButton onClick={() => navigate('/search')}>
<SearchIcon onClick={() => navigate('/search')} /> <SearchIcon />
</IconButton> </IconButton>
) )
} }

View File

@ -1,14 +1,14 @@
import React, { useState, useEffect } from 'react' import React, { useState, useEffect } from 'react'
import * as JsSearch from 'js-search' import * as JsSearch from 'js-search'
import { Link } from 'gatsby'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { makeStyles } from '@material-ui/core/styles' import { makeStyles } from '@material-ui/core/styles'
import List from '@material-ui/core/List' import List from '@material-ui/core/List'
import ListItem from '@material-ui/core/ListItem' import ListItem from '@material-ui/core/ListItem'
import Pagination from '@material-ui/lab/Pagination'
import TextField from '@material-ui/core/TextField' import TextField from '@material-ui/core/TextField'
import InputAdornment from '@material-ui/core/InputAdornment' import InputAdornment from '@material-ui/core/InputAdornment'
import SearchIcon from '@material-ui/icons/Search' import SearchIcon from '@material-ui/icons/Search'
import SearchResultElement from './SearchResultElement'
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
parent: { parent: {
overflow: 'hidden', overflow: 'hidden',
@ -89,7 +89,6 @@ const SearchClient = ({ searchableData }) => {
<TextField <TextField
variant="outlined" variant="outlined"
placeholder="Search" placeholder="Search"
fullwidth
style={{ style={{
margin: '10px auto', margin: '10px auto',
width: '100%' width: '100%'
@ -113,9 +112,7 @@ const SearchClient = ({ searchableData }) => {
> >
{searchState.touched ? ( {searchState.touched ? (
<div> <div>
<div>Total results found: {searchState.searchResults.length}</div> {/* <List>
<List>
{searchState.searchResults.map((element) => ( {searchState.searchResults.map((element) => (
<ListItem <ListItem
style={{ before: { content: null } }} style={{ before: { content: null } }}
@ -124,7 +121,9 @@ const SearchClient = ({ searchableData }) => {
<Link to={element.slug}>{element.title} </Link> <Link to={element.slug}>{element.title} </Link>
</ListItem> </ListItem>
))} ))}
</List>
</List> */}
<ResultList searchResults={searchState.searchResults} />
</div> </div>
) : null} ) : null}
</div> </div>
@ -140,15 +139,14 @@ const ResultList = ({ searchResults }) => {
const url = typeof window !== 'undefined' ? window.location.host : '' const url = typeof window !== 'undefined' ? window.location.host : ''
console.log('url', url) console.log('url', url)
return ( return (
<div style={{ maxHeight: '100%', overflowY: 'scroll' }}> <div style={{ maxHeight: '100%' }}>
<div>Total results found: {searchResults.length} </div> <div>Total results found: {searchResults.length} </div>
<Pagination count={10} size="small" />
<div> <div>
<List style={{ maxHeight: '100%', overflowY: 'scroll' }}> <List style={{ maxHeight: '100%' }}>
{searchResults.map((element) => ( {searchResults.map((element) => (
<ListItem style={{ before: { content: null } }} key={element.id}> <ListItem style={{ before: { content: null } }} key={element.id}>
<Link to={element.slug}>{element.title} </Link> <SearchResultElement element={element} />
</ListItem> </ListItem>
))} ))}
</List> </List>

View File

@ -27,15 +27,17 @@ const SearchComponent = () => {
} }
} }
`) `)
console.log('data', data)
const searchableData = data.allMarkdownRemark.edges.map(({ node }) => { const searchableData = data.allMarkdownRemark.edges.map(({ node }) => {
var section = 'Concepts'
if (node.fields.slug.startsWith('/tutorials')) section = 'Tutorials'
return { return {
title: node.frontmatter.title, title: node.frontmatter.title,
description: node.frontmatter.description, description: node.frontmatter.description,
slug: slug: node.fields.slug,
node.fields.slug[0] === '/' ? node.fields.slug : '/' + node.fields.slug,
id: node.id, id: node.id,
text: node.plainText text: node.plainText,
section: section
} }
}) })

View File

@ -0,0 +1,54 @@
import React from 'react'
import { Link } from 'gatsby'
import PropTypes from 'prop-types'
import Card from '@material-ui/core/Card'
import CardContent from '@material-ui/core/CardContent'
import Typography from '@material-ui/core/Typography'
import { makeStyles } from '@material-ui/core/styles'
const useStyles = makeStyles({
root: {
minWidth: 275
},
bullet: {
display: 'inline-block',
margin: '0 2px',
transform: 'scale(0.8)'
},
title: {
fontSize: 14
},
pos: {
marginBottom: 12
}
})
const SearchResultElement = ({ element }) => {
const classes = useStyles()
const { slug, title, section, description } = element
return (
<Card container alignItems="center" style={{ width: '100%' }}>
<CardContent>
<Typography
className={classes.title}
color="textSecondary"
gutterBottom
>
{section}
</Typography>
<Typography variant="h6" component="h2">
<Link to={slug}>{title}</Link>
</Typography>
<Typography className={classes.pos} color="textSecondary">
{description ? description.substring(0, 100) + '...' : null}
</Typography>
</CardContent>
</Card>
)
}
SearchResultElement.propTypes = {
element: PropTypes.object.isRequired
}
export default SearchResultElement