commons/src/routes/Publish/index.tsx

198 lines
5.9 KiB
TypeScript
Raw Normal View History

2019-01-23 14:43:41 +01:00
import React, { ChangeEvent, Component, FormEvent } from 'react'
import { Logger } from '@oceanprotocol/squid'
2019-02-12 14:07:22 +01:00
import Route from '../../components/templates/Route'
import Form from '../../components/atoms/Form/Form'
import AssetModel from '../../models/AssetModel'
import { User } from '../../context/User'
import Step from './Step'
2019-02-19 17:39:53 +01:00
import Progress from './Progress'
2019-01-24 17:52:11 +01:00
2019-02-12 14:07:22 +01:00
import form from '../../data/form-publish.json'
2019-01-24 17:52:11 +01:00
type AssetType = 'dataset' | 'algorithm' | 'container' | 'workflow' | 'other'
2019-01-23 12:15:16 +01:00
2019-02-05 17:05:28 +01:00
interface PublishState {
2019-02-05 16:00:22 +01:00
name?: string
dateCreated?: Date
description?: string
files?: string[]
2019-02-05 16:00:22 +01:00
price?: number
author?: string
type?: AssetType
license?: string
copyrightHolder?: string
categories?: string[]
2019-02-12 10:48:35 +01:00
tags?: string[]
isPublishing?: boolean
isPublished?: boolean
publishedDid?: string
publishingError?: string
currentStep?: number
2019-01-23 12:15:16 +01:00
}
2019-02-05 17:05:28 +01:00
class Publish extends Component<{}, PublishState> {
2019-01-24 17:52:11 +01:00
public state = {
currentStep: 1,
2019-01-24 17:52:11 +01:00
name: '',
dateCreated: new Date(),
description: '',
2019-02-13 12:19:45 +01:00
files: [],
2019-01-24 17:52:11 +01:00
price: 0,
author: '',
type: 'dataset' as AssetType,
license: '',
copyrightHolder: '',
2019-02-13 14:40:54 +01:00
categories: [],
isPublishing: false,
2019-02-12 10:48:35 +01:00
isPublished: false,
publishedDid: '',
publishingError: ''
2019-01-24 17:52:11 +01:00
}
2019-01-23 14:43:41 +01:00
private inputChange = (
event: ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLSelectElement>
) => {
this.setState({
[event.currentTarget.name]: event.currentTarget.value
})
}
private inputToArrayChange = (
event: ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLSelectElement>
) => {
this.setState({
[event.currentTarget.name]: [event.currentTarget.value]
})
}
private next = () => {
let { currentStep } = this.state
2019-02-19 17:26:15 +01:00
const totalSteps = form.steps.length
currentStep =
currentStep >= totalSteps - 1 ? totalSteps : currentStep + 1
this.setState({ currentStep })
}
private prev = () => {
let { currentStep } = this.state
currentStep = currentStep <= 1 ? 1 : currentStep - 1
2019-02-19 17:26:15 +01:00
this.setState({ currentStep })
}
2019-02-20 13:46:54 +01:00
private tryAgain = () => {
this.setState({ publishingError: '' })
}
2019-02-12 10:48:35 +01:00
private toStart = () => {
this.setState({
name: '',
dateCreated: new Date(),
description: '',
2019-02-13 20:41:59 +01:00
files: [],
2019-02-12 10:48:35 +01:00
price: 0,
author: '',
type: 'dataset' as AssetType,
license: '',
copyrightHolder: '',
2019-02-13 20:41:59 +01:00
categories: [],
2019-02-12 10:48:35 +01:00
isPublishing: false,
2019-02-20 14:07:29 +01:00
isPublished: false,
currentStep: 1
2019-02-12 10:48:35 +01:00
})
}
private registerAsset = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault()
this.setState({
publishingError: '',
isPublishing: true
})
const { ocean } = this.context
const account = await ocean.getAccounts()
const newAsset = {
// OEP-08 Attributes
// https://github.com/oceanprotocol/OEPs/tree/master/8
base: Object.assign(AssetModel.base, {
name: this.state.name,
description: this.state.description,
dateCreated: new Date().toString(),
author: this.state.author,
license: this.state.license,
copyrightHolder: this.state.copyrightHolder,
2019-02-14 21:27:18 +01:00
files: this.state.files,
price: this.state.price,
type: this.state.type,
2019-02-13 20:41:59 +01:00
categories: [this.state.categories],
size: '',
encoding: '',
compression: undefined,
contentType: '',
workExample: undefined,
inLanguage: undefined,
tags: ''
}),
curation: Object.assign(AssetModel.curation),
additionalInformation: Object.assign(
AssetModel.additionalInformation
)
}
2019-02-12 14:07:22 +01:00
try {
const asset = await ocean.registerAsset(newAsset, account[0])
2019-02-12 10:48:35 +01:00
this.setState({
publishedDid: asset.id,
isPublished: true
})
} catch (e) {
// make readable errors
Logger.log('error:', e)
this.setState({
publishingError: e
})
}
this.setState({
isPublishing: false
})
}
2019-01-23 14:43:41 +01:00
public render() {
return (
<Route
title="Publish"
description="Publish a new data set into the Ocean Protocol Network."
>
2019-02-20 13:46:54 +01:00
<Progress
steps={form.steps}
currentStep={this.state.currentStep}
/>
<Form onSubmit={this.registerAsset}>
{form.steps.map((step: any, index: number) => (
<Step
key={index}
index={index}
title={step.title}
description={step.description}
2019-02-19 17:39:53 +01:00
currentStep={this.state.currentStep}
2019-02-20 13:46:54 +01:00
fields={step.fields}
inputChange={this.inputChange}
inputToArrayChange={this.inputToArrayChange}
state={this.state}
next={this.next}
prev={this.prev}
totalSteps={form.steps.length}
tryAgain={this.tryAgain}
toStart={this.toStart}
content={step.content}
2019-02-19 17:39:53 +01:00
/>
2019-02-20 13:46:54 +01:00
))}
</Form>
2019-02-08 14:22:40 +01:00
</Route>
2019-01-23 14:43:41 +01:00
)
}
2019-01-23 12:15:16 +01:00
}
Publish.contextType = User
2019-01-23 14:43:41 +01:00
export default Publish