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

152 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-04-03 12:51:20 +02:00
import React, { PureComponent } from 'react'
2019-04-02 14:25:16 +02:00
import queryString from 'query-string'
2019-04-30 19:19:28 +02:00
import { History, Location } from 'history'
2019-04-02 14:51:59 +02:00
import { Logger } from '@oceanprotocol/squid'
2019-12-13 12:22:52 +01:00
import SearchResults, {
SearchResultsState
} from '../components/molecules/SearchResults'
2019-02-08 14:22:40 +01:00
import Route from '../components/templates/Route'
import { User } from '../context'
2019-04-05 12:19:17 +02:00
import Pagination from '../components/molecules/Pagination'
2019-02-13 17:03:26 +01:00
import styles from './Search.module.scss'
import Content from '../components/atoms/Content'
import withTracker from '../hoc/withTracker'
2019-01-23 17:39:34 +01:00
2019-04-03 12:51:20 +02:00
interface SearchProps {
location: Location
2019-04-30 19:19:28 +02:00
history: History
2019-04-03 12:51:20 +02:00
}
2019-12-13 12:22:52 +01:00
interface SearchState extends SearchResultsState {
2019-04-29 17:57:45 +02:00
searchTerm: string
searchCategories: string
2019-01-23 17:39:34 +01:00
}
class Search extends PureComponent<SearchProps, SearchState> {
public static contextType = User
2019-04-03 12:51:20 +02:00
public state = {
2019-04-05 11:18:30 +02:00
results: [],
2019-04-05 15:24:06 +02:00
totalResults: 0,
offset: 25,
2019-04-05 12:19:17 +02:00
totalPages: 1,
currentPage: 1,
2019-04-29 17:57:45 +02:00
isLoading: true,
searchTerm: '',
searchCategories: ''
2019-04-03 12:51:20 +02:00
}
2019-01-23 17:39:34 +01:00
public async componentDidMount() {
2019-05-15 16:58:57 +02:00
const { search } = this.props.location
const { text, page, categories } = queryString.parse(search)
2019-05-15 16:58:57 +02:00
if (text) {
2019-12-13 12:22:52 +01:00
await this.setState({
2019-05-23 19:53:20 +02:00
searchTerm: decodeURIComponent(`${text}`)
2019-05-15 16:58:57 +02:00
})
}
if (categories) {
2019-12-13 12:22:52 +01:00
await this.setState({
2019-05-23 19:53:20 +02:00
searchCategories: decodeURIComponent(`${categories}`)
})
}
2019-04-29 17:57:45 +02:00
// switch to respective page if query string is present
2019-05-15 16:58:57 +02:00
if (page) {
const currentPage = Number(page)
2019-12-13 12:22:52 +01:00
await this.setState({ currentPage })
}
2019-01-23 17:39:34 +01:00
2019-04-03 12:51:20 +02:00
this.searchAssets()
}
2019-04-02 14:51:59 +02:00
2019-04-03 12:51:20 +02:00
private searchAssets = async () => {
2019-04-30 19:19:28 +02:00
const { ocean } = this.context
const { offset, currentPage, searchTerm, searchCategories } = this.state
2019-04-30 19:19:28 +02:00
2019-05-15 16:58:57 +02:00
const queryValues =
searchCategories !== '' && searchTerm !== ''
? { text: [searchTerm], categories: [searchCategories] }
: searchCategories !== '' && searchTerm === ''
? { categories: [searchCategories] }
: { text: [searchTerm] }
2019-04-03 12:22:33 +02:00
const searchQuery = {
offset,
page: currentPage,
2019-04-02 14:51:59 +02:00
query: {
2019-05-27 14:54:07 +02:00
...queryValues
2019-04-02 14:51:59 +02:00
},
sort: {
2019-05-27 17:53:48 +02:00
created: -1
2019-04-02 14:51:59 +02:00
}
}
2019-04-30 19:19:28 +02:00
try {
2019-11-14 13:16:53 +01:00
const search = await ocean.assets.query(searchQuery)
2019-04-30 19:19:28 +02:00
this.setState({
results: search.results,
totalResults: search.totalResults,
totalPages: search.totalPages,
isLoading: false
})
} catch (error) {
2019-05-15 17:37:36 +02:00
Logger.error(error.message)
2019-04-30 19:19:28 +02:00
this.setState({ isLoading: false })
}
2019-01-23 17:39:34 +01:00
}
private onPageClick = async (data: { selected: number }) => {
2019-04-15 22:31:05 +02:00
// react-pagination starts counting at 0, we start at 1
2019-09-02 13:25:46 +02:00
const toPage = data.selected + 1
2019-04-15 22:13:40 +02:00
2019-04-09 13:51:00 +02:00
this.props.history.push({
pathname: this.props.location.pathname,
2019-04-29 17:57:45 +02:00
search: `?text=${this.state.searchTerm}&page=${toPage}`
2019-04-09 13:51:00 +02:00
})
this.setState({ currentPage: toPage, isLoading: true })
await this.searchAssets()
2019-04-05 12:19:17 +02:00
}
public render() {
2019-12-13 12:22:52 +01:00
const {
totalResults,
totalPages,
currentPage,
isLoading,
results,
searchTerm,
searchCategories
} = this.state
2019-04-05 12:19:17 +02:00
return (
2019-04-29 12:02:51 +02:00
<Route title="Search" wide>
<Content wide>
2019-12-13 12:22:52 +01:00
{!isLoading && (
2019-05-24 11:15:08 +02:00
<h2 className={styles.resultsTitle}>
{totalResults} results for{' '}
<span>
{decodeURIComponent(
2019-12-13 12:22:52 +01:00
searchTerm || searchCategories
2019-05-24 11:15:08 +02:00
)}
</span>
</h2>
)}
2019-12-13 12:22:52 +01:00
<SearchResults isLoading={isLoading} results={results} />
<Pagination
totalPages={totalPages}
currentPage={currentPage}
handlePageClick={this.onPageClick}
2019-04-29 12:02:51 +02:00
/>
</Content>
2019-02-08 14:22:40 +01:00
</Route>
)
}
}
export default withTracker(Search)