diff --git a/src/components/atoms/Form/Input.tsx b/src/components/atoms/Form/Input.tsx index 032a658..005b7ec 100644 --- a/src/components/atoms/Form/Input.tsx +++ b/src/components/atoms/Form/Input.tsx @@ -23,6 +23,7 @@ interface InputProps { additionalComponent?: void value?: string onChange?: any + rows?: number } interface InputState { diff --git a/src/data/form-publish.json b/src/data/form-publish.json new file mode 100644 index 0000000..7b5d5fe --- /dev/null +++ b/src/data/form-publish.json @@ -0,0 +1,114 @@ +{ + "title": "Publish a new data asset", + "description": "A cool form description", + "fields": { + "name": { + "label": "Title", + "placeholder": "i.e. My cool data set", + "type": "text", + "required": true, + "help": "Help me" + }, + "description": { + "label": "Description", + "placeholder": "i.e. My cool data set", + "type": "textarea", + "required": true, + "rows": 5 + }, + "price": { + "label": "Price", + "placeholder": "i.e. 1000", + "type": "number", + "required": true + }, + "author": { + "label": "Author", + "placeholder": "i.e. Jelly McJellyfish", + "type": "text", + "required": true + }, + "files": { + "label": "Files", + "placeholder": "i.e. https://file.com/file.json", + "type": "text", + "required": true + }, + "type": { + "label": "Type", + "type": "select", + "required": true, + "options": [ + { + "value": "", + "label": "---" + }, + { + "value": "dataset", + "label": "Data set" + }, + { + "value": "algorithm", + "label": "Algorithm" + }, + { + "value": "container", + "label": "Container" + }, + { + "value": "workflow", + "label": "Workflow" + }, + { + "value": "other", + "label": "Other" + } + ] + }, + "license": { + "label": "License", + "type": "select", + "required": true, + "options": [ + { + "value": "none", + "label": "No License Specified" + }, + { + "value": "public-domain", + "label": "Public Domain" + }, + { + "value": "cc-by", + "label": "CC BY: Attribution" + }, + { + "value": "cc-by-sa", + "label": "CC BY-SA: Attribution ShareAlike" + }, + { + "value": "cc-by-nd", + "label": "CC BY-ND: Attribution-NoDerivs" + }, + { + "value": "cc-by-nc", + "label": "CC BY-NC: Attribution-NonCommercial" + }, + { + "value": "cc-by-nc-sa", + "label": "CC BY-NC-SA: Attribution-NonCommercial-ShareAlike" + }, + { + "value": "cc-by-nc-nd", + "label": "CC BY-NC-ND: Attribution-NonCommercial-NoDerivs" + } + ] + }, + "copyrightHolder": { + "label": "Copyright Holder", + "placeholder": "i.e. fwhfiw", + "type": "text", + "required": true + } + } +} diff --git a/src/pages/Publish.tsx b/src/pages/Publish.tsx index b3b9e8d..d46e1c2 100644 --- a/src/pages/Publish.tsx +++ b/src/pages/Publish.tsx @@ -1,8 +1,12 @@ import React, { ChangeEvent, Component, FormEvent } from 'react' import Button from '../components/atoms/Button' +import Form from '../components/atoms/Form/Form' +import Input from '../components/atoms/Form/Input' import { User } from '../context/User' import AssetModel from '../models/AssetModel' +import form from '../data/form-publish.json' + type AssetType = 'dataset' | 'algorithm' | 'container' | 'workflow' | 'other' interface PublishState { @@ -33,98 +37,94 @@ class Publish extends Component<{}, PublishState> { categories: [''] } + public formFields = (entries: any[]) => + entries.map(([key, value]) => { + let onChange = this.inputChange + + if (key === 'files' || key === 'categories') { + onChange = this.inputToArrayChange + } + + return ( + + ) + }) + + private inputChange = ( + event: ChangeEvent | ChangeEvent + ) => { + this.setState({ + [event.target.name]: event.target.value + }) + } + + private inputToArrayChange = ( + event: ChangeEvent | ChangeEvent + ) => { + this.setState({ + [event.target.name]: [event.target.value] + }) + } + + private registerAsset = async (event: FormEvent) => { + event.preventDefault() + const account = await this.context.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, + contentUrls: [this.state.files], + price: this.state.price, + type: this.state.type, + size: '', + encoding: '', + compression: undefined, + contentType: '', + workExample: undefined, + inLanguage: undefined, + tags: '' + }), + curation: Object.assign(AssetModel.curation), + additionalInformation: Object.assign( + AssetModel.additionalInformation + ) + } + + await this.context.ocean.registerAsset(newAsset, account[0]) + } + public render() { + const entries = Object.entries(form.fields) + return (

Publish

-
-
- Name: - -
-
- Description: - -
-
- Price: - -
-
- Author: - -
-
- Files: - -
-
- Type: - -
-
- License: - -
+ + {this.formFields(entries)} +
Category:
-
- CopyrightHolder: - -
+ {states => (
{states.isLogged ? ( -
- -
+ ) : ( -
- -
+ )}
)}
-
+
) } - - private inputChange = ( - event: ChangeEvent | ChangeEvent - ) => { - this.setState({ - [event.target.name]: event.target.value - }) - } - - private inputToArrayChange = ( - event: ChangeEvent | ChangeEvent - ) => { - this.setState({ - [event.target.name]: [event.target.value] - }) - } - - private registerAsset = async (event: FormEvent) => { - event.preventDefault() - const account = await this.context.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, - contentUrls: [this.state.files], - price: this.state.price, - type: this.state.type, - size: '', - encoding: '', - compression: undefined, - contentType: '', - workExample: undefined, - inLanguage: undefined, - tags: '' - }), - curation: Object.assign(AssetModel.curation), - additionalInformation: Object.assign( - AssetModel.additionalInformation - ) - } - - await this.context.ocean.registerAsset(newAsset, account[0]) - } } Publish.contextType = User