1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

switch back to server search results

This commit is contained in:
Matthias Kretschmann 2019-09-13 14:21:02 +02:00
parent ca6029b034
commit affdcd48df
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 314 additions and 234 deletions

396
client/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,16 +3,16 @@ import { render, fireEvent } from '@testing-library/react'
import FilterItem from './FilterItem' import FilterItem from './FilterItem'
describe('FilterItem', () => { describe('FilterItem', () => {
const filterByCategory = jest.fn() const setCategory = jest.fn()
const filterByLicense = jest.fn() const setLicense = jest.fn()
it('renders without crashing', () => { it('renders without crashing', () => {
const { container } = render( const { container } = render(
<FilterItem <FilterItem
isActive={false} isActive={false}
filter={{ label: 'Category' }} filter={{ label: 'Category' }}
filterByCategory={filterByCategory} setCategory={setCategory}
filterByLicense={filterByLicense} setLicense={setLicense}
option="Hello" option="Hello"
/> />
) )
@ -24,13 +24,13 @@ describe('FilterItem', () => {
<FilterItem <FilterItem
isActive isActive
filter={{ label: 'Category' }} filter={{ label: 'Category' }}
filterByCategory={filterByCategory} setCategory={setCategory}
filterByLicense={filterByLicense} setLicense={setLicense}
option="Hello" option="Hello"
/> />
) )
fireEvent.click(getByText(/Hello/)) fireEvent.click(getByText(/Hello/))
expect(filterByCategory).toHaveBeenCalled() expect(setCategory).toHaveBeenCalled()
fireEvent.click(getByTitle('Clear')) fireEvent.click(getByTitle('Clear'))
}) })
@ -39,13 +39,13 @@ describe('FilterItem', () => {
<FilterItem <FilterItem
isActive isActive
filter={{ label: 'License' }} filter={{ label: 'License' }}
filterByCategory={filterByCategory} setCategory={setCategory}
filterByLicense={filterByLicense} setLicense={setLicense}
option="Hello" option="Hello"
/> />
) )
fireEvent.click(getByText(/Hello/)) fireEvent.click(getByText(/Hello/))
expect(filterByLicense).toHaveBeenCalled() expect(setLicense).toHaveBeenCalled()
fireEvent.click(getByTitle('Clear')) fireEvent.click(getByTitle('Clear'))
}) })
}) })

View File

@ -5,15 +5,15 @@ import styles from './FilterItem.module.scss'
export default function FilterItem({ export default function FilterItem({
isActive, isActive,
filter, filter,
filterByCategory, setCategory,
filterByLicense, setLicense,
option option
}: { }: {
isActive: boolean isActive: boolean
filter: any filter: any
option: string option: string
filterByCategory(category: string): void setCategory(category: string): void
filterByLicense(license: string): void setLicense(license: string): void
}) { }) {
return ( return (
<li className={isActive ? styles.active : styles.item}> <li className={isActive ? styles.active : styles.item}>
@ -22,8 +22,8 @@ export default function FilterItem({
className={styles.option} className={styles.option}
onClick={() => onClick={() =>
filter.label === 'Category' filter.label === 'Category'
? filterByCategory(option) ? setCategory(option)
: filterByLicense(option) : setLicense(option)
} }
> >
{option}{' '} {option}{' '}
@ -35,8 +35,8 @@ export default function FilterItem({
title="Clear" title="Clear"
onClick={() => onClick={() =>
filter.label === 'Category' filter.label === 'Category'
? filterByCategory('') ? setCategory('')
: filterByLicense('') : setLicense('')
} }
> >
&times; &times;

View File

@ -6,14 +6,12 @@ import Pagination from '../../components/molecules/Pagination'
import styles from './Results.module.scss' import styles from './Results.module.scss'
export default function Results({ export default function Results({
title,
results, results,
totalResults, totalResults,
totalPages, totalPages,
currentPage, currentPage,
handlePageClick handlePageClick
}: { }: {
title: string
results: any[] results: any[]
totalResults: number totalResults: number
totalPages: number totalPages: number
@ -23,7 +21,7 @@ export default function Results({
return results && results.length ? ( return results && results.length ? (
<> <>
<h2 className={styles.resultsTitle}> <h2 className={styles.resultsTitle}>
{totalResults} results for <span>{title}</span> {totalResults} {totalResults === 1 ? 'result' : 'results'}
</h2> </h2>
<div className={styles.results}> <div className={styles.results}>
{results.map((asset: any) => ( {results.map((asset: any) => (

View File

@ -43,14 +43,14 @@ export default function Sidebar({
category, category,
license, license,
results, results,
filterByCategory, setCategory,
filterByLicense setLicense
}: { }: {
category: string category: string
license: string license: string
results: any[] results: any[]
filterByCategory(category: string): void setCategory(category: string): void
filterByLicense(license: string): void setLicense(license: string): void
}) { }) {
const { filterCategories, filterLicenses } = getFilterMetadata(results) const { filterCategories, filterLicenses } = getFilterMetadata(results)
@ -80,8 +80,8 @@ export default function Sidebar({
key={shortid.generate()} key={shortid.generate()}
isActive={isActive} isActive={isActive}
filter={filter} filter={filter}
filterByCategory={filterByCategory} setCategory={setCategory}
filterByLicense={filterByLicense} setLicense={setLicense}
option={option} option={option}
/> />
) )

View File

@ -6,6 +6,7 @@ import Spinner from '../../components/atoms/Spinner'
import Route from '../../components/templates/Route' import Route from '../../components/templates/Route'
import { User } from '../../context' import { User } from '../../context'
import Content from '../../components/atoms/Content' import Content from '../../components/atoms/Content'
import Button from '../../components/atoms/Button'
import Input from '../../components/atoms/Form/Input' import Input from '../../components/atoms/Form/Input'
import withTracker from '../../hoc/withTracker' import withTracker from '../../hoc/withTracker'
import Sidebar from './Sidebar' import Sidebar from './Sidebar'
@ -19,7 +20,6 @@ interface SearchProps {
interface SearchState { interface SearchState {
results: any[] results: any[]
resultsFiltered: any[]
totalResults: number totalResults: number
offset: number offset: number
totalPages: number totalPages: number
@ -37,7 +37,6 @@ class Search extends PureComponent<SearchProps, SearchState> {
public state = { public state = {
results: [], results: [],
resultsFiltered: [],
totalResults: 0, totalResults: 0,
offset: 25, offset: 25,
totalPages: 1, totalPages: 1,
@ -69,7 +68,9 @@ class Search extends PureComponent<SearchProps, SearchState> {
this.setState(update, () => this.searchAssets()) this.setState(update, () => this.searchAssets())
} }
private searchAssets = async () => { private searchAssets = async (event?: any) => {
event && event.preventDefault()
const { ocean } = this.context const { ocean } = this.context
const { offset, currentPage, search, category, license } = this.state const { offset, currentPage, search, category, license } = this.state
@ -112,7 +113,6 @@ class Search extends PureComponent<SearchProps, SearchState> {
const search = await ocean.assets.query(searchQuery) const search = await ocean.assets.query(searchQuery)
this.setState({ this.setState({
results: search.results, results: search.results,
resultsFiltered: search.results,
totalResults: search.totalResults, totalResults: search.totalResults,
totalPages: search.totalPages, totalPages: search.totalPages,
isLoading: false isLoading: false
@ -132,63 +132,15 @@ class Search extends PureComponent<SearchProps, SearchState> {
) )
} }
private inputChange = ( private inputChange = (event: ChangeEvent<HTMLInputElement>) => {
event: ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLSelectElement> if (this.state.search !== event.target.value)
) => { this.setState({ search: event.target.value })
this.setState(
{
[event.currentTarget.name]: event.currentTarget.value
} as any,
() => {
this.pendingSearch()
}
)
}
private pendingSearch = () => {
this.setState({ isLoading: true })
if (this.timeout) {
clearTimeout(this.timeout)
}
this.timeout = setTimeout(this.executeSearch, 500)
}
private executeSearch = () => {
this.timeout = false
this.searchAssets()
} }
public setCategory = (category: string) => { public setCategory = (category: string) => {
this.setState({ category, isLoading: true }, () => this.searchAssets()) this.setState({ category, isLoading: true }, () => this.searchAssets())
} }
public filterByCategory = (category: string) => {
const resultsFiltered: any[] = this.state.results.filter(
(asset: DDO) => {
const { metadata } = asset.findServiceByType('Metadata')
const { categories } = metadata.base
if (!categories) return true
return category === '' ? true : categories.includes(category)
}
)
this.setState({ resultsFiltered, category })
}
public filterByLicense = (name: string) => {
const resultsFiltered: any[] = this.state.results.filter(
(asset: any) => {
const { metadata } = asset.findServiceByType('Metadata')
const { license } = metadata.base
return name === '' ? true : license === name
}
)
this.setState({ resultsFiltered, license: name })
}
public setLicense = (license: string) => { public setLicense = (license: string) => {
this.setState({ license, isLoading: true }, () => this.searchAssets()) this.setState({ license, isLoading: true }, () => this.searchAssets())
} }
@ -201,7 +153,6 @@ class Search extends PureComponent<SearchProps, SearchState> {
search, search,
category, category,
license, license,
resultsFiltered,
totalPages, totalPages,
currentPage currentPage
} = this.state } = this.state
@ -209,19 +160,21 @@ class Search extends PureComponent<SearchProps, SearchState> {
return ( return (
<Route title="Search"> <Route title="Search">
<Content> <Content>
<Input <form onSubmit={this.searchAssets}>
type="search" <Input
name="search" type="search"
label="" name="search"
placeholder="e.g. shapes of plants" label=""
value={search} placeholder="e.g. shapes of plants"
onChange={this.inputChange} value={search}
// group={ onChange={this.inputChange}
// <Button primary onClick={this.executeSearch}> group={
// Search <Button primary onClick={this.searchAssets}>
// </Button> Search
// } </Button>
/> }
/>
</form>
</Content> </Content>
<Content wide> <Content wide>
<div className={styles.content}> <div className={styles.content}>
@ -230,10 +183,7 @@ class Search extends PureComponent<SearchProps, SearchState> {
<Spinner message="Searching..." /> <Spinner message="Searching..." />
) : ( ) : (
<Results <Results
title={decodeURIComponent( results={results}
search || category
)}
results={resultsFiltered}
totalResults={totalResults} totalResults={totalResults}
totalPages={totalPages} totalPages={totalPages}
currentPage={currentPage} currentPage={currentPage}
@ -246,8 +196,8 @@ class Search extends PureComponent<SearchProps, SearchState> {
category={category} category={category}
license={license} license={license}
results={results} results={results}
filterByCategory={this.filterByCategory} setCategory={this.setCategory}
filterByLicense={this.filterByLicense} setLicense={this.setLicense}
/> />
</div> </div>
</Content> </Content>