commons/client/src/components/templates/Asset/AssetFile.tsx

159 lines
5.0 KiB
TypeScript
Raw Normal View History

2019-04-03 18:41:39 +02:00
import React, { PureComponent } from 'react'
2019-04-30 19:19:28 +02:00
import { Logger, DDO, File } from '@oceanprotocol/squid'
2019-04-03 18:41:39 +02:00
import filesize from 'filesize'
import Button from '../../atoms/Button'
import Spinner from '../../atoms/Spinner'
import { User, Market } from '../../../context'
2019-04-03 18:41:39 +02:00
import styles from './AssetFile.module.scss'
2019-04-08 11:35:10 +02:00
import ReactGA from 'react-ga'
import cleanupContentType from '../../../utils/cleanupContentType'
2019-04-03 18:41:39 +02:00
export const messages: any = {
99: 'Decrypting file URL...',
2019-05-10 13:03:19 +02:00
0: '1/3<br />Asking for agreement signature...',
1: '1/3<br />Agreement initialized.',
2: '2/3<br />Asking for two payment confirmations...',
3: '2/3<br />Payment confirmed. Requesting access...',
4: '3/3<br /> Access granted. Consuming file...'
}
2019-04-03 18:41:39 +02:00
interface AssetFileProps {
2019-04-30 19:19:28 +02:00
file: File
ddo: DDO
2019-04-03 18:41:39 +02:00
}
interface AssetFileState {
isLoading: boolean
error: string
step: number
2019-04-03 18:41:39 +02:00
}
export default class AssetFile extends PureComponent<
AssetFileProps,
AssetFileState
> {
public static contextType = User
2019-04-03 18:41:39 +02:00
public state = {
isLoading: false,
error: '',
step: 99
2019-04-03 18:41:39 +02:00
}
2019-04-25 11:37:15 +02:00
private resetState = () =>
2019-05-06 13:07:38 +02:00
this.setState({
isLoading: true,
error: '',
step: 99
2019-05-06 13:07:38 +02:00
})
2019-04-03 18:41:39 +02:00
2019-04-30 19:19:28 +02:00
private purchaseAsset = async (ddo: DDO, index: number) => {
2019-04-03 18:41:39 +02:00
this.resetState()
2019-04-08 11:35:10 +02:00
ReactGA.event({
category: 'Purchase',
action: 'purchaseAsset-start ' + ddo.id
})
2019-04-03 18:41:39 +02:00
2019-04-08 11:35:10 +02:00
const { ocean } = this.context
2019-04-08 19:46:53 +02:00
2019-04-03 18:41:39 +02:00
try {
2019-04-08 11:35:10 +02:00
const accounts = await ocean.accounts.list()
2019-05-31 12:56:13 +02:00
const agreements = await ocean.keeper.conditions.accessSecretStoreCondition.getGrantedDidByConsumer(
accounts[0].id
)
const agreement = agreements.find((element: any) => {
return element.did === ddo.id
})
2019-05-29 12:09:23 +02:00
let agreementId
2019-05-31 12:56:13 +02:00
2019-05-29 12:09:23 +02:00
if (agreement) {
2019-05-31 12:56:13 +02:00
;({ agreementId } = agreement)
2019-05-29 12:09:23 +02:00
} else {
agreementId = await ocean.assets
2020-02-20 16:11:28 +01:00
.order(ddo.id, accounts[0])
2019-05-31 12:56:13 +02:00
.next((step: number) => this.setState({ step }))
2019-05-29 12:09:23 +02:00
}
2019-04-03 18:41:39 +02:00
2019-05-10 11:51:36 +02:00
// manually add another step here for better UX
this.setState({ step: 4 })
2019-04-25 14:28:50 +02:00
2019-04-03 18:41:39 +02:00
const path = await ocean.assets.consume(
agreementId,
ddo.id,
accounts[0],
'',
index
)
Logger.log('path', path)
2019-04-08 11:35:10 +02:00
ReactGA.event({
category: 'Purchase',
action: 'purchaseAsset-end ' + ddo.id
})
2019-04-03 18:41:39 +02:00
this.setState({ isLoading: false })
} catch (error) {
2019-05-31 12:56:13 +02:00
Logger.error('error', error.message)
2019-05-21 12:07:27 +02:00
this.setState({
isLoading: false,
error: `${error.message}. Sorry about that, can you try again?`
})
2019-04-08 11:35:10 +02:00
ReactGA.event({
category: 'Purchase',
2019-04-08 11:41:06 +02:00
action: 'purchaseAsset-error ' + error.message
2019-04-08 11:35:10 +02:00
})
2019-04-03 18:41:39 +02:00
}
}
public render() {
const { ddo, file } = this.props
2019-05-14 11:59:18 +02:00
const { isLoading, error, step } = this.state
const { isLogged } = this.context
const { index, contentType, contentLength } = file
2019-04-03 18:41:39 +02:00
return (
<div className={styles.fileWrap}>
<ul key={index} className={styles.file}>
{contentType || contentLength ? (
<>
2019-11-12 13:27:23 +01:00
<li>{cleanupContentType(contentType)}</li>
<li>
2019-11-12 13:27:23 +01:00
{contentLength && contentLength !== '0'
? filesize(contentLength)
: ''}
</li>
{/* <li>{encoding}</li> */}
{/* <li>{compression}</li> */}
</>
) : (
<li className={styles.empty}>No file info available</li>
)}
2019-04-03 18:41:39 +02:00
</ul>
2019-04-08 19:46:53 +02:00
{isLoading ? (
<Spinner message={messages[step]} />
2019-04-03 18:41:39 +02:00
) : (
<Market.Consumer>
2020-05-19 10:36:18 +02:00
{(market) => (
<Button
primary
className={styles.buttonMain}
// weird 0 hack so TypeScript is happy
onClick={() =>
this.purchaseAsset(ddo, index || 0)
}
disabled={!isLogged || !market.networkMatch}
name="Download"
>
Get file
</Button>
)}
</Market.Consumer>
2019-04-03 18:41:39 +02:00
)}
2019-04-08 19:46:53 +02:00
{error !== '' && <div className={styles.error}>{error}</div>}
2019-04-03 18:41:39 +02:00
</div>
)
}
}