mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
Add service filters download and compute to the existing filters (#817)
* filter tags added, filter functionality linked * filter tag selection fix * deselect filter fix, logs deleted * use single filter function * delete unused functions and logs *   replaced
This commit is contained in:
parent
421d5981e6
commit
969ac96417
@ -2,6 +2,13 @@
|
|||||||
div.filterList {
|
div.filterList {
|
||||||
white-space: normal;
|
white-space: normal;
|
||||||
margin-bottom: 0;
|
margin-bottom: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
align-items: baseline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.separator {
|
||||||
|
width: calc(var(--spacer) / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
.filter {
|
.filter {
|
||||||
|
@ -2,7 +2,11 @@ import React, { ReactElement, useState } from 'react'
|
|||||||
import { useNavigate } from '@reach/router'
|
import { useNavigate } from '@reach/router'
|
||||||
import styles from './filterService.module.css'
|
import styles from './filterService.module.css'
|
||||||
import classNames from 'classnames/bind'
|
import classNames from 'classnames/bind'
|
||||||
import { addExistingParamsToUrl, FilterByTypeOptions } from './utils'
|
import {
|
||||||
|
addExistingParamsToUrl,
|
||||||
|
FilterByAccessOptions,
|
||||||
|
FilterByTypeOptions
|
||||||
|
} from './utils'
|
||||||
import Button from '../../atoms/Button'
|
import Button from '../../atoms/Button'
|
||||||
|
|
||||||
const cx = classNames.bind(styles)
|
const cx = classNames.bind(styles)
|
||||||
@ -14,57 +18,110 @@ const serviceFilterItems = [
|
|||||||
{ display: 'algorithms', value: FilterByTypeOptions.Algorithm }
|
{ display: 'algorithms', value: FilterByTypeOptions.Algorithm }
|
||||||
]
|
]
|
||||||
|
|
||||||
|
const accessFilterItems = [
|
||||||
|
{ display: 'download ', value: FilterByAccessOptions.Download },
|
||||||
|
{ display: 'compute ', value: FilterByAccessOptions.Compute }
|
||||||
|
]
|
||||||
|
|
||||||
export default function FilterPrice({
|
export default function FilterPrice({
|
||||||
serviceType,
|
serviceType,
|
||||||
setServiceType
|
accessType,
|
||||||
|
setServiceType,
|
||||||
|
setAccessType
|
||||||
}: {
|
}: {
|
||||||
serviceType: string
|
serviceType: string
|
||||||
|
accessType: string
|
||||||
setServiceType: React.Dispatch<React.SetStateAction<string>>
|
setServiceType: React.Dispatch<React.SetStateAction<string>>
|
||||||
|
setAccessType: React.Dispatch<React.SetStateAction<string>>
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const [serviceSelections, setServiceSelections] = useState<string[]>([])
|
const [serviceSelections, setServiceSelections] = useState<string[]>([])
|
||||||
|
const [accessSelections, setAccessSelections] = useState<string[]>([])
|
||||||
|
|
||||||
async function applyServiceFilter(filterBy: string) {
|
async function applyFilter(filter: string, filterType: string) {
|
||||||
let urlLocation = await addExistingParamsToUrl(location, ['serviceType'])
|
let urlLocation = ''
|
||||||
if (filterBy && location.search.indexOf('&serviceType') === -1) {
|
if (filterType.localeCompare('accessType') === 0) {
|
||||||
urlLocation = `${urlLocation}&serviceType=${filterBy}`
|
urlLocation = await addExistingParamsToUrl(location, ['accessType'])
|
||||||
|
} else {
|
||||||
|
urlLocation = await addExistingParamsToUrl(location, ['serviceType'])
|
||||||
}
|
}
|
||||||
setServiceType(filterBy)
|
|
||||||
|
if (filter && location.search.indexOf(filterType) === -1) {
|
||||||
|
filterType === 'accessType'
|
||||||
|
? (urlLocation = `${urlLocation}&accessType=${filter}`)
|
||||||
|
: (urlLocation = `${urlLocation}&serviceType=${filter}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
filterType === 'accessType' ? setAccessType(filter) : setServiceType(filter)
|
||||||
navigate(urlLocation)
|
navigate(urlLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleSelectedFilter(isSelected: boolean, value: string) {
|
async function handleSelectedFilter(isSelected: boolean, value: string) {
|
||||||
if (isSelected) {
|
if (
|
||||||
if (serviceSelections.length > 1) {
|
value === FilterByAccessOptions.Download ||
|
||||||
const otherValue = serviceFilterItems.find(
|
value === FilterByAccessOptions.Compute
|
||||||
(p) => p.value !== value
|
) {
|
||||||
).value
|
if (isSelected) {
|
||||||
await applyServiceFilter(otherValue)
|
if (accessSelections.length > 1) {
|
||||||
setServiceSelections([otherValue])
|
// both selected -> select the other one
|
||||||
|
const otherValue = accessFilterItems.find(
|
||||||
|
(p) => p.value !== value
|
||||||
|
).value
|
||||||
|
await applyFilter(otherValue, 'accessType')
|
||||||
|
setAccessSelections([otherValue])
|
||||||
|
} else {
|
||||||
|
// only the current one selected -> deselect it
|
||||||
|
await applyFilter(undefined, 'accessType')
|
||||||
|
setAccessSelections([])
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
await applyServiceFilter(undefined)
|
if (accessSelections.length) {
|
||||||
if (serviceSelections.includes(value)) {
|
// one already selected -> both selected
|
||||||
serviceSelections.pop()
|
await applyFilter(undefined, 'accessType')
|
||||||
|
setAccessSelections(accessFilterItems.map((p) => p.value))
|
||||||
|
} else {
|
||||||
|
// none selected -> select
|
||||||
|
await applyFilter(value, 'accessType')
|
||||||
|
setAccessSelections([value])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (serviceSelections.length) {
|
if (isSelected) {
|
||||||
await applyServiceFilter(undefined)
|
if (serviceSelections.length > 1) {
|
||||||
setServiceSelections(serviceFilterItems.map((p) => p.value))
|
const otherValue = serviceFilterItems.find(
|
||||||
|
(p) => p.value !== value
|
||||||
|
).value
|
||||||
|
await applyFilter(otherValue, 'serviceType')
|
||||||
|
setServiceSelections([otherValue])
|
||||||
|
} else {
|
||||||
|
await applyFilter(undefined, 'serviceType')
|
||||||
|
setServiceSelections([])
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
await applyServiceFilter(value)
|
if (serviceSelections.length) {
|
||||||
setServiceSelections([value])
|
await applyFilter(undefined, 'serviceType')
|
||||||
|
setServiceSelections(serviceFilterItems.map((p) => p.value))
|
||||||
|
} else {
|
||||||
|
await applyFilter(value, 'serviceType')
|
||||||
|
setServiceSelections([value])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function applyClearFilter() {
|
async function applyClearFilter() {
|
||||||
let urlLocation = await addExistingParamsToUrl(location, ['serviceType'])
|
let urlLocation = await addExistingParamsToUrl(location, [
|
||||||
|
'accessType',
|
||||||
|
'serviceType'
|
||||||
|
])
|
||||||
|
|
||||||
urlLocation = `${urlLocation}`
|
urlLocation = `${urlLocation}`
|
||||||
|
|
||||||
setServiceSelections([])
|
setServiceSelections([])
|
||||||
|
setAccessSelections([])
|
||||||
|
|
||||||
setServiceType(undefined)
|
setServiceType(undefined)
|
||||||
|
setAccessType(undefined)
|
||||||
navigate(urlLocation)
|
navigate(urlLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,8 +148,31 @@ export default function FilterPrice({
|
|||||||
</Button>
|
</Button>
|
||||||
)
|
)
|
||||||
})}
|
})}
|
||||||
|
<div className={styles.separator} />
|
||||||
|
{accessFilterItems.map((e, index) => {
|
||||||
|
const isAccessSelected =
|
||||||
|
e.value === accessType || accessSelections.includes(e.value)
|
||||||
|
const selectFilter = cx({
|
||||||
|
[styles.selected]: isAccessSelected,
|
||||||
|
[styles.filter]: true
|
||||||
|
})
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
style="text"
|
||||||
|
key={index}
|
||||||
|
className={selectFilter}
|
||||||
|
onClick={async () => {
|
||||||
|
handleSelectedFilter(isAccessSelected, e.value)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{e.display}
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
{clearFilters.map((e, index) => {
|
{clearFilters.map((e, index) => {
|
||||||
const showClear = serviceSelections.length > 0
|
const showClear =
|
||||||
|
accessSelections.length > 0 || serviceSelections.length > 0
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
size="small"
|
size="small"
|
||||||
|
@ -21,11 +21,13 @@ export default function SearchPage({
|
|||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { appConfig } = useSiteMetadata()
|
const { appConfig } = useSiteMetadata()
|
||||||
const parsed = queryString.parse(location.search)
|
const parsed = queryString.parse(location.search)
|
||||||
const { text, owner, tags, page, sort, sortOrder, serviceType } = parsed
|
const { text, owner, tags, page, sort, sortOrder, serviceType, accessType } =
|
||||||
|
parsed
|
||||||
const { chainIds } = useUserPreferences()
|
const { chainIds } = useUserPreferences()
|
||||||
const [queryResult, setQueryResult] = useState<QueryResult>()
|
const [queryResult, setQueryResult] = useState<QueryResult>()
|
||||||
const [loading, setLoading] = useState<boolean>()
|
const [loading, setLoading] = useState<boolean>()
|
||||||
const [service, setServiceType] = useState<string>(serviceType as string)
|
const [service, setServiceType] = useState<string>(serviceType as string)
|
||||||
|
const [access, setAccessType] = useState<string>(accessType as string)
|
||||||
const [sortType, setSortType] = useState<string>(sort as string)
|
const [sortType, setSortType] = useState<string>(sort as string)
|
||||||
const [sortDirection, setSortDirection] = useState<string>(
|
const [sortDirection, setSortDirection] = useState<string>(
|
||||||
sortOrder as string
|
sortOrder as string
|
||||||
@ -53,6 +55,7 @@ export default function SearchPage({
|
|||||||
sort,
|
sort,
|
||||||
page,
|
page,
|
||||||
serviceType,
|
serviceType,
|
||||||
|
accessType,
|
||||||
sortOrder,
|
sortOrder,
|
||||||
appConfig.metadataCacheUri,
|
appConfig.metadataCacheUri,
|
||||||
chainIds
|
chainIds
|
||||||
@ -74,7 +77,9 @@ export default function SearchPage({
|
|||||||
<div className={styles.row}>
|
<div className={styles.row}>
|
||||||
<ServiceFilter
|
<ServiceFilter
|
||||||
serviceType={service}
|
serviceType={service}
|
||||||
|
accessType={access}
|
||||||
setServiceType={setServiceType}
|
setServiceType={setServiceType}
|
||||||
|
setAccessType={setAccessType}
|
||||||
/>
|
/>
|
||||||
<Sort
|
<Sort
|
||||||
sortType={sortType}
|
sortType={sortType}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
|
import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
|
||||||
import { MetadataCache, Logger } from '@oceanprotocol/lib'
|
import { Logger } from '@oceanprotocol/lib'
|
||||||
import {
|
import {
|
||||||
queryMetadata,
|
queryMetadata,
|
||||||
transformChainIdsListToQuery
|
transformChainIdsListToQuery
|
||||||
@ -33,6 +33,13 @@ export const FilterByTypeOptions = {
|
|||||||
type FilterByTypeOptions =
|
type FilterByTypeOptions =
|
||||||
typeof FilterByTypeOptions[keyof typeof FilterByTypeOptions]
|
typeof FilterByTypeOptions[keyof typeof FilterByTypeOptions]
|
||||||
|
|
||||||
|
export const FilterByAccessOptions = {
|
||||||
|
Download: 'access',
|
||||||
|
Compute: 'compute'
|
||||||
|
}
|
||||||
|
type FilterByAccessOptions =
|
||||||
|
typeof FilterByAccessOptions[keyof typeof FilterByAccessOptions]
|
||||||
|
|
||||||
function getSortType(sortParam: string): string {
|
function getSortType(sortParam: string): string {
|
||||||
const sortTerm =
|
const sortTerm =
|
||||||
sortParam === SortTermOptions.Created
|
sortParam === SortTermOptions.Created
|
||||||
@ -51,7 +58,8 @@ export function getSearchQuery(
|
|||||||
offset?: string,
|
offset?: string,
|
||||||
sort?: string,
|
sort?: string,
|
||||||
sortOrder?: string,
|
sortOrder?: string,
|
||||||
serviceType?: string
|
serviceType?: string,
|
||||||
|
accessType?: string
|
||||||
): any {
|
): any {
|
||||||
const sortTerm = getSortType(sort)
|
const sortTerm = getSortType(sort)
|
||||||
const sortValue = sortOrder === SortValueOptions.Ascending ? 1 : -1
|
const sortValue = sortOrder === SortValueOptions.Ascending ? 1 : -1
|
||||||
@ -139,6 +147,12 @@ export function getSearchQuery(
|
|||||||
: `${serviceType}`
|
: `${serviceType}`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
match: {
|
||||||
|
'service.type':
|
||||||
|
accessType === undefined ? 'access OR compute' : `${accessType}`
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
query_string: {
|
query_string: {
|
||||||
query: `${transformChainIdsListToQuery(chainIds)}`
|
query: `${transformChainIdsListToQuery(chainIds)}`
|
||||||
@ -169,6 +183,7 @@ export async function getResults(
|
|||||||
sort?: string
|
sort?: string
|
||||||
sortOrder?: string
|
sortOrder?: string
|
||||||
serviceType?: string
|
serviceType?: string
|
||||||
|
accessType?: string
|
||||||
},
|
},
|
||||||
metadataCacheUri: string,
|
metadataCacheUri: string,
|
||||||
chainIds: number[]
|
chainIds: number[]
|
||||||
@ -182,7 +197,8 @@ export async function getResults(
|
|||||||
offset,
|
offset,
|
||||||
sort,
|
sort,
|
||||||
sortOrder,
|
sortOrder,
|
||||||
serviceType
|
serviceType,
|
||||||
|
accessType
|
||||||
} = params
|
} = params
|
||||||
|
|
||||||
const searchQuery = getSearchQuery(
|
const searchQuery = getSearchQuery(
|
||||||
@ -195,7 +211,8 @@ export async function getResults(
|
|||||||
offset,
|
offset,
|
||||||
sort,
|
sort,
|
||||||
sortOrder,
|
sortOrder,
|
||||||
serviceType
|
serviceType,
|
||||||
|
accessType
|
||||||
)
|
)
|
||||||
const source = axios.CancelToken.source()
|
const source = axios.CancelToken.source()
|
||||||
// const queryResult = await metadataCache.queryMetadata(searchQuery)
|
// const queryResult = await metadataCache.queryMetadata(searchQuery)
|
||||||
|
Loading…
Reference in New Issue
Block a user