commons/src/routes/Publish/index.tsx

299 lines
9.0 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-28 18:24:04 +01:00
import { steps } 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-02-20 16:50:27 +01:00
validationStatus?: any
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: '',
categories: '',
isPublishing: false,
2019-02-12 10:48:35 +01:00
isPublished: false,
publishedDid: '',
2019-02-20 16:50:27 +01:00
publishingError: '',
validationStatus: {
2019-02-21 14:35:25 +01:00
1: { name: false, files: false, allFieldsValid: false },
2: { description: false, categories: false, allFieldsValid: false },
3: {
author: false,
copyrightHolder: false,
license: false,
allFieldsValid: false
}
2019-02-20 16:50:27 +01:00
}
2019-01-24 17:52:11 +01:00
}
2019-01-23 14:43:41 +01:00
private inputChange = (
event: ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLSelectElement>
) => {
2019-02-20 16:50:27 +01:00
this.validateInputs(event.currentTarget.name, event.currentTarget.value)
this.setState({
[event.currentTarget.name]: event.currentTarget.value
})
}
private inputToArrayChange = (
event: ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLSelectElement>
) => {
2019-02-21 15:05:12 +01:00
this.validateInputs(event.currentTarget.name, event.currentTarget.value)
this.setState({
[event.currentTarget.name]: [event.currentTarget.value]
})
}
private next = () => {
let { currentStep } = this.state
2019-02-28 18:24:04 +01:00
const totalSteps = steps.length
2019-02-19 17:26:15 +01:00
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: '',
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
})
}
2019-02-20 16:50:27 +01:00
private validateInputs = (name: string, value: any) => {
2019-02-21 14:35:25 +01:00
let hasContent = value.length > 0
// Setting state for all fields
if (hasContent) {
2019-03-01 16:57:09 +01:00
this.setState(
prevState => ({
validationStatus: {
...prevState.validationStatus,
[this.state.currentStep]: {
...prevState.validationStatus[
this.state.currentStep
],
[name]: true
}
2019-02-21 14:35:25 +01:00
}
2019-03-01 16:57:09 +01:00
}),
this.runValidation
)
2019-02-21 14:35:25 +01:00
} else {
2019-03-01 16:57:09 +01:00
this.setState(
prevState => ({
validationStatus: {
...prevState.validationStatus,
[this.state.currentStep]: {
...prevState.validationStatus[
this.state.currentStep
],
[name]: false
}
2019-02-21 14:35:25 +01:00
}
2019-03-01 16:57:09 +01:00
}),
this.runValidation
)
2019-02-21 14:35:25 +01:00
}
}
2019-02-21 14:35:25 +01:00
private runValidation = () => {
let { validationStatus } = this.state
2019-02-21 14:35:25 +01:00
//
2019-02-20 16:50:27 +01:00
// Step 1
2019-02-21 14:35:25 +01:00
//
if (validationStatus[1].name && validationStatus[1].files) {
2019-02-21 14:35:25 +01:00
this.setState(prevState => ({
validationStatus: {
...prevState.validationStatus,
1: {
...prevState.validationStatus[1],
allFieldsValid: true
}
}
}))
2019-02-20 16:50:27 +01:00
}
2019-02-21 14:35:25 +01:00
//
2019-02-20 16:50:27 +01:00
// Step 2
2019-02-21 14:35:25 +01:00
//
2019-02-21 15:05:12 +01:00
if (validationStatus[2].description && validationStatus[2].categories) {
2019-02-21 14:35:25 +01:00
this.setState(prevState => ({
validationStatus: {
...prevState.validationStatus,
2: {
...prevState.validationStatus[2],
allFieldsValid: true
}
}
}))
2019-02-20 16:50:27 +01:00
}
2019-02-21 15:05:12 +01:00
//
2019-02-20 16:50:27 +01:00
// Step 3
2019-02-21 15:05:12 +01:00
//
if (
validationStatus[3].author &&
validationStatus[3].copyrightHolder &&
validationStatus[3].license
) {
this.setState(prevState => ({
validationStatus: {
...prevState.validationStatus,
3: {
...prevState.validationStatus[3],
allFieldsValid: true
}
}
}))
}
2019-02-20 16:50:27 +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-28 18:24:04 +01:00
<Progress steps={steps} currentStep={this.state.currentStep} />
2019-02-20 13:46:54 +01:00
<Form onSubmit={this.registerAsset}>
2019-02-28 18:24:04 +01:00
{steps.map((step: any, index: number) => (
2019-02-20 13:46:54 +01:00
<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}
2019-02-28 18:24:04 +01:00
totalSteps={steps.length}
2019-02-20 13:46:54 +01:00
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