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

fix code duplication

This commit is contained in:
Matthias Kretschmann 2019-12-13 12:22:52 +01:00
parent e9e5dd9049
commit c5ea3b7c8a
Signed by: m
GPG Key ID: 606EEEF3C479A91F
8 changed files with 116 additions and 125 deletions

View File

@ -0,0 +1,36 @@
@import '../../styles/variables';
.results {
display: grid;
grid-template-columns: 1fr;
grid-gap: $spacer;
max-width: calc(18rem + #{$spacer * 2});
margin: auto;
margin-top: $spacer * 2;
@media (min-width: $break-point--small) {
margin-left: 0;
margin-right: 0;
max-width: none;
grid-template-columns: repeat(2, 1fr);
}
@media (min-width: $break-point--medium) {
grid-template-columns: repeat(3, 1fr);
}
}
.simple {
composes: results;
margin-top: 0;
@media (min-width: $break-point--medium) {
grid-template-columns: repeat(2, 1fr);
}
}
.empty {
text-align: center;
margin-top: $spacer * 4;
color: $brand-grey-light;
}

View File

@ -0,0 +1,40 @@
import React from 'react'
import { Link } from 'react-router-dom'
import { DDO } from '@oceanprotocol/squid'
import Spinner from '../atoms/Spinner'
import AssetTeaser from './AssetTeaser'
import styles from './SearchResults.module.scss'
export interface SearchResultsState {
results: DDO[]
totalResults: number
offset: number
totalPages: number
currentPage: number
isLoading: boolean
}
export default function SearchResults({
isLoading,
results,
simpleGrid
}: {
isLoading: boolean
results: DDO[]
simpleGrid?: boolean
}) {
return isLoading ? (
<Spinner message="Searching..." />
) : results && results.length ? (
<div className={simpleGrid ? styles.simple : styles.results}>
{results.map((asset: any) => (
<AssetTeaser key={asset.id} asset={asset} />
))}
</div>
) : (
<div className={styles.empty}>
<p>No Data Sets Found.</p>
<Link to="/publish">+ Publish A Data Set</Link>
</div>
)
}

View File

@ -74,13 +74,3 @@
.channelTeaser { .channelTeaser {
color: $brand-grey; color: $brand-grey;
} }
.channelResults {
display: grid;
grid-template-columns: 1fr;
grid-gap: $spacer;
@media (min-width: $break-point--small) {
grid-template-columns: 1fr 1fr;
}
}

View File

@ -1,19 +1,18 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import { User } from '../../context' import { User } from '../../context'
import { Logger } from '@oceanprotocol/squid' import { Logger, DDO } from '@oceanprotocol/squid'
import Spinner from '../atoms/Spinner' import CategoryImage from '../atoms/CategoryImage'
import AssetTeaser from '../molecules/AssetTeaser' import SearchResults from '../molecules/SearchResults'
import styles from './ChannelTeaser.module.scss' import styles from './ChannelTeaser.module.scss'
import channels from '../../data/channels.json' import channels from '../../data/channels.json'
import CategoryImage from '../atoms/CategoryImage'
interface ChannelTeaserProps { interface ChannelTeaserProps {
channel: string channel: string
} }
interface ChannelTeaserState { interface ChannelTeaserState {
channelAssets?: any[] channelAssets?: DDO[]
isLoadingChannel?: boolean isLoadingChannel?: boolean
} }
@ -81,17 +80,11 @@ export default class ChannelTeaser extends Component<
</header> </header>
</div> </div>
<div> <div>
{isLoadingChannel ? ( <SearchResults
<Spinner message="Loading..." /> isLoading={isLoadingChannel}
) : channelAssets && channelAssets.length ? ( results={channelAssets}
<div className={styles.channelResults}> simpleGrid
{channelAssets.map((asset: any) => ( />
<AssetTeaser key={asset.id} asset={asset} />
))}
</div>
) : (
<div>No data sets found.</div>
)}
</div> </div>
</div> </div>
) )

View File

@ -1,21 +1 @@
@import '../../styles/variables'; @import '../../styles/variables';
.results {
display: grid;
grid-template-columns: 1fr;
grid-gap: $spacer;
max-width: calc(18rem + #{$spacer * 2});
margin: auto;
margin-top: $spacer * 2;
@media (min-width: $break-point--small) {
margin-left: 0;
margin-right: 0;
max-width: none;
grid-template-columns: repeat(2, 1fr);
}
@media (min-width: $break-point--medium) {
grid-template-columns: repeat(3, 1fr);
}
}

View File

@ -1,12 +1,10 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import { Logger } from '@oceanprotocol/squid' import { Logger } from '@oceanprotocol/squid'
import { History } from 'history' import { History } from 'history'
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 AssetTeaser from '../molecules/AssetTeaser'
import Pagination from '../../components/molecules/Pagination' import Pagination from '../../components/molecules/Pagination'
import styles from './Channel.module.scss' import SearchResults, { SearchResultsState } from '../molecules/SearchResults'
import Content from '../../components/atoms/Content' import Content from '../../components/atoms/Content'
import channels from '../../data/channels.json' import channels from '../../data/channels.json'
import CategoryImage from '../atoms/CategoryImage' import CategoryImage from '../atoms/CategoryImage'
@ -20,13 +18,7 @@ interface ChannelProps {
} }
} }
interface ChannelState { interface ChannelState extends SearchResultsState {
results: any[]
totalResults: number
offset: number
totalPages: number
currentPage: number
isLoading: boolean
title: string title: string
description: string description: string
} }
@ -91,21 +83,15 @@ export default class Channel extends PureComponent<ChannelProps, ChannelState> {
await this.getChannelAssets() await this.getChannelAssets()
} }
public renderResults = () =>
this.state.isLoading ? (
<Spinner message="Searching..." />
) : this.state.results && this.state.results.length ? (
<div className={styles.results}>
{this.state.results.map((asset: any) => (
<AssetTeaser key={asset.id} asset={asset} />
))}
</div>
) : (
<div>No data sets found.</div>
)
public render() { public render() {
const { title, description, totalPages, currentPage } = this.state const {
title,
description,
totalPages,
currentPage,
isLoading,
results
} = this.state
return ( return (
<Route <Route
@ -114,7 +100,7 @@ export default class Channel extends PureComponent<ChannelProps, ChannelState> {
image={<CategoryImage header category={title} />} image={<CategoryImage header category={title} />}
> >
<Content wide> <Content wide>
{this.renderResults()} <SearchResults isLoading={isLoading} results={results} />
<Pagination <Pagination
totalPages={totalPages} totalPages={totalPages}

View File

@ -10,23 +10,3 @@
color: $brand-grey-dark; color: $brand-grey-dark;
} }
} }
.results {
display: grid;
grid-template-columns: 1fr;
grid-gap: $spacer;
@media (min-width: $break-point--small) {
grid-template-columns: repeat(2, 1fr);
}
@media (min-width: $break-point--medium) {
grid-template-columns: repeat(3, 1fr);
}
}
.empty {
text-align: center;
margin-top: $spacer * 4;
color: $brand-grey-light;
}

View File

@ -1,12 +1,12 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import { Link } from 'react-router-dom'
import queryString from 'query-string' import queryString from 'query-string'
import { History, Location } from 'history' import { History, Location } from 'history'
import { Logger } from '@oceanprotocol/squid' import { Logger } from '@oceanprotocol/squid'
import Spinner from '../components/atoms/Spinner' import SearchResults, {
SearchResultsState
} from '../components/molecules/SearchResults'
import Route from '../components/templates/Route' import Route from '../components/templates/Route'
import { User } from '../context' import { User } from '../context'
import AssetTeaser from '../components/molecules/AssetTeaser'
import Pagination from '../components/molecules/Pagination' import Pagination from '../components/molecules/Pagination'
import styles from './Search.module.scss' import styles from './Search.module.scss'
import Content from '../components/atoms/Content' import Content from '../components/atoms/Content'
@ -17,13 +17,7 @@ interface SearchProps {
history: History history: History
} }
interface SearchState { interface SearchState extends SearchResultsState {
results: any[]
totalResults: number
offset: number
totalPages: number
currentPage: number
isLoading: boolean
searchTerm: string searchTerm: string
searchCategories: string searchCategories: string
} }
@ -47,13 +41,13 @@ class Search extends PureComponent<SearchProps, SearchState> {
const { text, page, categories } = queryString.parse(search) const { text, page, categories } = queryString.parse(search)
if (text) { if (text) {
await this.setState({ this.setState({
searchTerm: decodeURIComponent(`${text}`) searchTerm: decodeURIComponent(`${text}`)
}) })
} }
if (categories) { if (categories) {
await this.setState({ this.setState({
searchCategories: decodeURIComponent(`${categories}`) searchCategories: decodeURIComponent(`${categories}`)
}) })
} }
@ -61,7 +55,7 @@ class Search extends PureComponent<SearchProps, SearchState> {
// switch to respective page if query string is present // switch to respective page if query string is present
if (page) { if (page) {
const currentPage = Number(page) const currentPage = Number(page)
await this.setState({ currentPage }) this.setState({ currentPage })
} }
this.searchAssets() this.searchAssets()
@ -116,40 +110,32 @@ class Search extends PureComponent<SearchProps, SearchState> {
await this.searchAssets() await this.searchAssets()
} }
public renderResults = () =>
this.state.isLoading ? (
<Spinner message="Searching..." />
) : this.state.results && this.state.results.length ? (
<div className={styles.results}>
{this.state.results.map((asset: any) => (
<AssetTeaser key={asset.id} asset={asset} />
))}
</div>
) : (
<div className={styles.empty}>
<p>No Data Sets Found.</p>
<Link to="/publish">+ Publish A Data Set</Link>
</div>
)
public render() { public render() {
const { totalResults, totalPages, currentPage } = this.state const {
totalResults,
totalPages,
currentPage,
isLoading,
results,
searchTerm,
searchCategories
} = this.state
return ( return (
<Route title="Search" wide> <Route title="Search" wide>
<Content wide> <Content wide>
{!this.state.isLoading && ( {!isLoading && (
<h2 className={styles.resultsTitle}> <h2 className={styles.resultsTitle}>
{totalResults} results for{' '} {totalResults} results for{' '}
<span> <span>
{decodeURIComponent( {decodeURIComponent(
this.state.searchTerm || searchTerm || searchCategories
this.state.searchCategories
)} )}
</span> </span>
</h2> </h2>
)} )}
{this.renderResults()}
<SearchResults isLoading={isLoading} results={results} />
<Pagination <Pagination
totalPages={totalPages} totalPages={totalPages}