1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

remove unused methods/hooks/components

This commit is contained in:
Matthias Kretschmann 2020-09-24 12:23:40 +02:00
parent 897bfb6e54
commit 3a72d670fd
Signed by: m
GPG Key ID: 606EEEF3C479A91F
8 changed files with 6 additions and 298 deletions

View File

@ -1,20 +0,0 @@
.layout {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.label {
margin-bottom: calc(var(--spacer) / 2);
}
@media (max-width: 55rem) {
.layout {
flex-direction: row;
}
.label {
flex-basis: auto;
width: calc(50% - var(--spacer) / 2);
}
}

View File

@ -1,54 +0,0 @@
import React, { ChangeEvent, ReactElement } from 'react'
import SearchFilterSection from '../atoms/SearchFilterSection'
import usePriceQueryParams from '../../hooks/usePriceQueryParams'
import styles from './SearchPriceFilter.module.css'
import Input from '../atoms/Input'
export declare type PriceInputProps = {
label: string
value: string
onChange: (ev: ChangeEvent<HTMLInputElement>) => void
text: string
}
export const PriceInput = ({
label,
value,
onChange,
text
}: PriceInputProps): ReactElement => {
return (
<Input
name={label}
label={text}
type="number"
min="0"
value={value}
onChange={onChange}
/>
)
}
export const SearchPriceFilter = () => {
const { min, setMin, max, setMax } = usePriceQueryParams()
return (
<SearchFilterSection title="Filter by price in Ocean Tokens">
<div className={styles.layout}>
<PriceInput
label="minPrice"
value={min}
onChange={(ev) => setMin(ev.target.value)}
text="Min price"
/>
<PriceInput
label="maxPrice"
value={max}
onChange={(ev) => setMax(ev.target.value)}
text="Max price"
/>
</div>
</SearchFilterSection>
)
}

View File

@ -2,7 +2,6 @@ import React, { ReactElement, useState, useEffect } from 'react'
import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore'
import SearchBar from '../../molecules/SearchBar'
import AssetList from '../../organisms/AssetList'
import { SearchPriceFilter } from '../../molecules/SearchPriceFilter'
import styles from './index.module.css'
import queryString from 'query-string'
import { getResults } from './utils'
@ -42,10 +41,6 @@ export default function SearchPage({
{text && <SearchBar initialValue={text as string} />}
</div>
{/* <aside className={styles.side}>
<SearchPriceFilter />
</aside> */}
<div className={styles.results}>
{loading ? <Loader /> : <AssetList queryResult={queryResult} />}
</div>

View File

@ -2,23 +2,20 @@ import {
SearchQuery,
QueryResult
} from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore'
import { priceQueryParamToWei } from '../../../utils'
import { MetadataStore, Logger } from '@oceanprotocol/lib'
export function getSearchQuery(
page?: string | string[],
offset?: string | string[],
text?: string | string[],
tag?: string | string[],
priceQuery?: [string | undefined, string | undefined]
tag?: string | string[]
): SearchQuery {
return {
page: Number(page) || 1,
offset: Number(offset) || 20,
query: {
text,
tags: tag ? [tag] : undefined,
price: priceQuery
tags: tag ? [tag] : undefined
},
sort: {
created: -1
@ -32,31 +29,14 @@ export function getSearchQuery(
}
export async function getResults(
params: any,
params: { text?: string; tag?: string; page?: string; offset?: string },
metadataStoreUri: string
): Promise<QueryResult> {
const { text, tag, page, offset, minPrice, maxPrice } = params
const minPriceParsed = priceQueryParamToWei(
minPrice as string,
'Error parsing context.query.minPrice'
)
const maxPriceParsed = priceQueryParamToWei(
maxPrice as string,
'Error parsing context.query.maxPrice'
)
const priceQuery =
minPriceParsed || maxPriceParsed
? // sometimes TS gets a bit silly
([minPriceParsed, maxPriceParsed] as [
string | undefined,
string | undefined
])
: undefined
const { text, tag, page, offset } = params
const metadataStore = new MetadataStore(metadataStoreUri, Logger)
const queryResult = await metadataStore.queryMetadata(
getSearchQuery(page, offset, text, tag, priceQuery)
getSearchQuery(page, offset, text, tag)
)
return queryResult

View File

@ -1,54 +0,0 @@
import { useState, useEffect } from 'react'
import { setProperty, JSONparse } from '../utils'
const CATEGORIES_QUERY_PARAM = 'categories'
// Set selected categories if they are in the query param
const useGetCategoriesFromQueryParam = (
allCategories: string[],
setSelectedCategories: (categories: string[]) => void
) => {
useEffect(() => {
if (router.query && router.query.categories) {
const parsedCategories = JSONparse<string[]>(
router.query.categories as string,
'Error parsing router.query.categories and setting parsedCategories'
)
// Only set/accept elements that are actually in allCcategories
const categoriesFromUrl = (parsedCategories || []).filter((pc: string) =>
allCategories.includes(pc)
)
setSelectedCategories(categoriesFromUrl)
}
}, [])
}
export default function useCategoriesQueryParam(allCategories: string[]) {
const [selectedCategories, setSelectedCategories] = useState<string[]>([])
useGetCategoriesFromQueryParam(allCategories, setSelectedCategories)
// Update url and the state with the selected categories
const toggleCategory = (category: string) => {
const newSelectedCategories = selectedCategories.includes(category)
? selectedCategories.filter((c) => c !== category)
: [...selectedCategories, category]
setSelectedCategories(newSelectedCategories)
// If newSelectedCategories remove the search query param, else set it
const queryParamValue =
newSelectedCategories.length === 0
? undefined
: JSON.stringify(newSelectedCategories)
const query = Object.assign({}, router.query)
setProperty(query, CATEGORIES_QUERY_PARAM, queryParamValue)
router.push({
pathname: router.pathname,
query: query
})
}
return {
selectedCategories,
toggleCategory
}
}

View File

@ -1,28 +0,0 @@
import { useEffect, useState } from 'react'
import { useLocation } from '@reach/router'
import queryString from 'query-string'
export default function usePriceQueryParams() {
const location = useLocation()
const [min, setMin] = useState(
(queryString.parse(location.search).minPrice as string) || '0'
)
const [max, setMax] = useState(
(queryString.parse(location.search).maxPrice as string) || '0'
)
useEffect(() => {
if (parseFloat(max) < parseFloat(min)) {
setMax(min)
}
}, [min])
useEffect(() => {
if (parseFloat(min) > parseFloat(max)) {
setMin(max)
}
}, [max])
return { min, setMin, max, setMax }
}

View File

@ -1,7 +1,6 @@
import axios, { AxiosResponse } from 'axios'
import { toast } from 'react-toastify'
import { File as FileMetadata } from '@oceanprotocol/lib'
import web3Utils from 'web3-utils'
export function updateQueryStringParameter(
uri: string,
@ -83,54 +82,6 @@ export function isDid(did: string | undefined): boolean {
return !!didMatch
}
export function JSONparse<T>(
input: string | undefined,
errorMessage: string
): T | undefined {
if (input === undefined) {
return undefined
}
try {
return JSON.parse(input) as T
} catch (err) {
console.error(errorMessage)
}
}
export function priceQueryParamToWei(
priceQueryParam: string | undefined,
errorMessage?: string
): string | undefined {
if (priceQueryParam === undefined) {
return undefined
}
try {
return web3Utils.toWei(priceQueryParam as string)
} catch (err) {
console.error(
errorMessage || 'Error in priceQueryParamToWei',
priceQueryParam
)
}
}
// The types of this function are deceivingly cryptic. This is just a helper
// that sets or removes a property on any object based on if the value passed is
// truthy or falsy
// e.g: setProperty({foo: 'bar'}, 'foo', false) -> {}
// setProperty({foo: 'bar'}, 'foo', 'baaz') -> { foo: 'baaz' }
export function setProperty<T extends Record<string, unknown>>(
objectToBeUpdated: T,
propertyName: keyof T,
value?: T[keyof T]
): void {
if (value) {
objectToBeUpdated[propertyName] = value
} else {
delete objectToBeUpdated[propertyName]
}
}
export function formatBytes(a: number, b: number): string {
if (a === 0) return '0 Bytes'
const c = 1024

View File

@ -4,10 +4,7 @@ import {
toStringNoMS,
updateQueryStringParameter,
isDid,
getFileInfo,
JSONparse,
priceQueryParamToWei,
setProperty
getFileInfo
} from '../../../src/utils'
jest.mock('axios')
@ -58,62 +55,3 @@ describe('isDid()', () => {
expect(isDid('hello')).toBe(false)
})
})
describe('JSONparse()', () => {
it('parse valid JSON and returns it', () => {
expect(
JSONparse<{ [key: string]: boolean }>('{"valid":true}', 'should not fail')
).toEqual({
valid: true
})
})
it('returns undefined when invalid JSON test is passed', () => {
expect(
JSONparse<unknown>(
'hello',
'Console.error part of test: JSONparse should fail'
)
).toBe(undefined)
})
it('returns undefined when receives undefined', () => {
expect(JSONparse<unknown>(undefined, 'Should not be logged')).toBe(
undefined
)
})
})
describe('priceQueryParamToWei()', () => {
it('converts a valid (eth) string amount and returns it', () => {
expect(priceQueryParamToWei('12')).toEqual('12000000000000000000')
})
it('returns undefined when (eth) string amount is passed', () => {
expect(priceQueryParamToWei('12.12.12')).toEqual(undefined)
})
it('returns undefined when (eth) string amount is undefined', () => {
expect(priceQueryParamToWei(undefined)).toEqual(undefined)
})
})
describe('setProperty()', () => {
let testObject: { foo: string }
beforeEach(() => {
testObject = {
foo: 'bar'
}
})
it('changes the value of a property in an object', () => {
setProperty(testObject, 'foo', 'wunderbar')
expect(testObject.foo).toEqual('wunderbar')
})
it('removes a property from an object if a falsy value is passed', () => {
setProperty(testObject, 'foo')
expect(testObject.foo).toBeUndefined()
})
})