mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
Update file metadata from provider (#631)
* get file metadata from provider * get file metadata without a wallet connected * unused import deleted * get file metadata from provider get file metadata without a wallet connected unused import deleted * loader added * lint fix * loaded added on file icon * fileMetadata empty object init * lint error fixed, deleted unused imports Co-authored-by: claudia.holhos <claudia.holhos@hpm.ro>
This commit is contained in:
parent
f5e01d7c0c
commit
b1d8a77895
@ -25,3 +25,7 @@
|
|||||||
width: 4.5rem;
|
width: 4.5rem;
|
||||||
padding: calc(var(--spacer) / 2) calc(var(--spacer) / 4);
|
padding: calc(var(--spacer) / 2) calc(var(--spacer) / 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.loaderWrap {
|
||||||
|
margin-right: calc(var(--spacer)/6);
|
||||||
|
}
|
@ -4,17 +4,28 @@ import filesize from 'filesize'
|
|||||||
import classNames from 'classnames/bind'
|
import classNames from 'classnames/bind'
|
||||||
import cleanupContentType from '../../utils/cleanupContentType'
|
import cleanupContentType from '../../utils/cleanupContentType'
|
||||||
import styles from './File.module.css'
|
import styles from './File.module.css'
|
||||||
|
import Loader from '../atoms/Loader'
|
||||||
|
|
||||||
const cx = classNames.bind(styles)
|
const cx = classNames.bind(styles)
|
||||||
|
|
||||||
|
function LoaderArea() {
|
||||||
|
return (
|
||||||
|
<div className={styles.loaderWrap}>
|
||||||
|
<Loader />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export default function File({
|
export default function File({
|
||||||
file,
|
file,
|
||||||
className,
|
className,
|
||||||
small
|
small,
|
||||||
|
isLoading
|
||||||
}: {
|
}: {
|
||||||
file: FileMetadata
|
file: FileMetadata
|
||||||
className?: string
|
className?: string
|
||||||
small?: boolean
|
small?: boolean
|
||||||
|
isLoading?: boolean
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
if (!file) return null
|
if (!file) return null
|
||||||
|
|
||||||
@ -26,6 +37,8 @@ export default function File({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<ul className={styleClasses}>
|
<ul className={styleClasses}>
|
||||||
|
{isLoading === false || isLoading === undefined ? (
|
||||||
|
<>
|
||||||
{file.contentType || file.contentLength ? (
|
{file.contentType || file.contentLength ? (
|
||||||
<>
|
<>
|
||||||
<li>{cleanupContentType(file.contentType)}</li>
|
<li>{cleanupContentType(file.contentType)}</li>
|
||||||
@ -38,6 +51,10 @@ export default function File({
|
|||||||
) : (
|
) : (
|
||||||
<li className={styles.empty}>No file info available</li>
|
<li className={styles.empty}>No file info available</li>
|
||||||
)}
|
)}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<LoaderArea />
|
||||||
|
)}
|
||||||
</ul>
|
</ul>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -48,11 +48,13 @@ const SuccessAction = () => (
|
|||||||
export default function Compute({
|
export default function Compute({
|
||||||
isBalanceSufficient,
|
isBalanceSufficient,
|
||||||
dtBalance,
|
dtBalance,
|
||||||
file
|
file,
|
||||||
|
fileIsLoading
|
||||||
}: {
|
}: {
|
||||||
isBalanceSufficient: boolean
|
isBalanceSufficient: boolean
|
||||||
dtBalance: string
|
dtBalance: string
|
||||||
file: FileMetadata
|
file: FileMetadata
|
||||||
|
fileIsLoading?: boolean
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { appConfig } = useSiteMetadata()
|
const { appConfig } = useSiteMetadata()
|
||||||
const { accountId } = useWeb3()
|
const { accountId } = useWeb3()
|
||||||
@ -370,7 +372,7 @@ export default function Compute({
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className={styles.info}>
|
<div className={styles.info}>
|
||||||
<File file={file} small />
|
<File file={file} isLoading={fileIsLoading} small />
|
||||||
<Price price={price} conversion />
|
<Price price={price} conversion />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -35,12 +35,14 @@ export default function Consume({
|
|||||||
ddo,
|
ddo,
|
||||||
file,
|
file,
|
||||||
isBalanceSufficient,
|
isBalanceSufficient,
|
||||||
dtBalance
|
dtBalance,
|
||||||
|
fileIsLoading
|
||||||
}: {
|
}: {
|
||||||
ddo: DDO
|
ddo: DDO
|
||||||
file: FileMetadata
|
file: FileMetadata
|
||||||
isBalanceSufficient: boolean
|
isBalanceSufficient: boolean
|
||||||
dtBalance: string
|
dtBalance: string
|
||||||
|
fileIsLoading?: boolean
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const { accountId } = useWeb3()
|
const { accountId } = useWeb3()
|
||||||
const { ocean } = useOcean()
|
const { ocean } = useOcean()
|
||||||
@ -159,7 +161,7 @@ export default function Consume({
|
|||||||
<aside className={styles.consume}>
|
<aside className={styles.consume}>
|
||||||
<div className={styles.info}>
|
<div className={styles.info}>
|
||||||
<div className={styles.filewrapper}>
|
<div className={styles.filewrapper}>
|
||||||
<File file={file} />
|
<File file={file} isLoading={fileIsLoading} />
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.pricewrapper}>
|
<div className={styles.pricewrapper}>
|
||||||
<Price price={price} conversion />
|
<Price price={price} conversion />
|
||||||
|
@ -2,7 +2,7 @@ import React, { ReactElement, useState, useEffect } from 'react'
|
|||||||
import styles from './index.module.css'
|
import styles from './index.module.css'
|
||||||
import Compute from './Compute'
|
import Compute from './Compute'
|
||||||
import Consume from './Consume'
|
import Consume from './Consume'
|
||||||
import { Logger } from '@oceanprotocol/lib'
|
import { Logger, File as FileMetadata, DID } from '@oceanprotocol/lib'
|
||||||
import Tabs from '../../atoms/Tabs'
|
import Tabs from '../../atoms/Tabs'
|
||||||
import compareAsBN from '../../../utils/compareAsBN'
|
import compareAsBN from '../../../utils/compareAsBN'
|
||||||
import Pool from './Pool'
|
import Pool from './Pool'
|
||||||
@ -10,20 +10,44 @@ import Trade from './Trade'
|
|||||||
import { useAsset } from '../../../providers/Asset'
|
import { useAsset } from '../../../providers/Asset'
|
||||||
import { useOcean } from '../../../providers/Ocean'
|
import { useOcean } from '../../../providers/Ocean'
|
||||||
import { useWeb3 } from '../../../providers/Web3'
|
import { useWeb3 } from '../../../providers/Web3'
|
||||||
|
import { getFileInfo } from '../../../utils/provider'
|
||||||
|
import axios from 'axios'
|
||||||
|
|
||||||
export default function AssetActions(): ReactElement {
|
export default function AssetActions(): ReactElement {
|
||||||
const { accountId } = useWeb3()
|
const { accountId } = useWeb3()
|
||||||
const { ocean, balance, account } = useOcean()
|
const { config, ocean, balance, account } = useOcean()
|
||||||
const { price, ddo, metadata } = useAsset()
|
const { price, ddo } = useAsset()
|
||||||
|
|
||||||
const [isBalanceSufficient, setIsBalanceSufficient] = useState<boolean>()
|
const [isBalanceSufficient, setIsBalanceSufficient] = useState<boolean>()
|
||||||
const [dtBalance, setDtBalance] = useState<string>()
|
const [dtBalance, setDtBalance] = useState<string>()
|
||||||
|
const [fileMetadata, setFileMetadata] = useState<FileMetadata>(Object)
|
||||||
|
const [fileIsLoading, setFileIsLoading] = useState<boolean>(false)
|
||||||
const isCompute = Boolean(ddo?.findServiceByType('compute'))
|
const isCompute = Boolean(ddo?.findServiceByType('compute'))
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!config) return
|
||||||
|
const source = axios.CancelToken.source()
|
||||||
|
async function initFileInfo() {
|
||||||
|
setFileIsLoading(true)
|
||||||
|
try {
|
||||||
|
const fileInfo = await getFileInfo(
|
||||||
|
DID.parse(`${ddo.id}`),
|
||||||
|
config.providerUri,
|
||||||
|
source.token
|
||||||
|
)
|
||||||
|
setFileMetadata(fileInfo.data[0])
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(error.message)
|
||||||
|
} finally {
|
||||||
|
setFileIsLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
initFileInfo()
|
||||||
|
}, [config, ddo.id])
|
||||||
|
|
||||||
// Get and set user DT balance
|
// Get and set user DT balance
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!ocean || !accountId) return
|
if (!ocean || !accountId) return
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
try {
|
try {
|
||||||
const dtBalance = await ocean.datatokens.balance(
|
const dtBalance = await ocean.datatokens.balance(
|
||||||
@ -55,14 +79,16 @@ export default function AssetActions(): ReactElement {
|
|||||||
<Compute
|
<Compute
|
||||||
dtBalance={dtBalance}
|
dtBalance={dtBalance}
|
||||||
isBalanceSufficient={isBalanceSufficient}
|
isBalanceSufficient={isBalanceSufficient}
|
||||||
file={metadata?.main.files[0]}
|
file={fileMetadata}
|
||||||
|
fileIsLoading={fileIsLoading}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Consume
|
<Consume
|
||||||
ddo={ddo}
|
ddo={ddo}
|
||||||
dtBalance={dtBalance}
|
dtBalance={dtBalance}
|
||||||
isBalanceSufficient={isBalanceSufficient}
|
isBalanceSufficient={isBalanceSufficient}
|
||||||
file={metadata?.main.files[0]}
|
file={fileMetadata}
|
||||||
|
fileIsLoading={fileIsLoading}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import axios, { CancelToken, AxiosResponse } from 'axios'
|
import axios, { CancelToken, AxiosResponse } from 'axios'
|
||||||
import { toast } from 'react-toastify'
|
import { toast } from 'react-toastify'
|
||||||
import { File as FileMetadata, Logger } from '@oceanprotocol/lib'
|
import { DID, File as FileMetadata, Logger } from '@oceanprotocol/lib'
|
||||||
|
|
||||||
export async function fileinfo(
|
export async function fileinfo(
|
||||||
url: string,
|
url: string,
|
||||||
@ -57,3 +57,26 @@ export async function fileinfo(
|
|||||||
Logger.error(error.message)
|
Logger.error(error.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getFileInfo(
|
||||||
|
url: string | DID,
|
||||||
|
providerUri: string,
|
||||||
|
cancelToken: CancelToken
|
||||||
|
): Promise<AxiosResponse> {
|
||||||
|
let postBody
|
||||||
|
try {
|
||||||
|
if (url instanceof DID)
|
||||||
|
postBody = {
|
||||||
|
did: url.getDid(),
|
||||||
|
cancelToken
|
||||||
|
}
|
||||||
|
else
|
||||||
|
postBody = {
|
||||||
|
url,
|
||||||
|
cancelToken
|
||||||
|
}
|
||||||
|
return await axios.post(`${providerUri}/api/v1/services/fileinfo`, postBody)
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(error.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user