1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

split up form in multiple steps, one component for all steps

This commit is contained in:
Matthias Kretschmann 2019-02-19 16:32:58 +01:00
parent cfad4a655d
commit e91de4a64d
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 268 additions and 151 deletions

View File

@ -1,90 +1,111 @@
{ {
"title": "Publish a new data asset", "title": "Publish a new data asset",
"description": "A cool form description", "description": "A cool form description",
"fields": { "steps": [
"name": { {
"label": "Title", "title": "Essentials",
"placeholder": "i.e. My cool data set", "fields": {
"type": "text", "name": {
"required": true, "label": "Title",
"help": "Help me Obiwan" "placeholder": "i.e. My cool data set",
"type": "text",
"required": true,
"help": "Help me Obiwan"
},
"files": {
"label": "Files",
"placeholder": "i.e. https://file.com/file.json",
"type": "text",
"required": true,
"help": "Provide one or multiple links to your data files."
}
}
}, },
"files": { {
"label": "Files", "title": "Information",
"placeholder": "i.e. https://file.com/file.json", "fields": {
"type": "text", "description": {
"required": true, "label": "Description",
"help": "Provide one or multiple links to your data files." "placeholder": "i.e. My cool data set",
"type": "textarea",
"required": true,
"rows": 5
},
"categories": {
"label": "Categories",
"type": "select",
"options": [
"Image Recognition",
"Dataset Of Datasets",
"Language",
"Performing Arts",
"Visual Arts & Design",
"Philosophy",
"History",
"Theology",
"Anthropology & Archeology",
"Sociology",
"Psychology",
"Politics",
"Interdisciplinary",
"Economics & Finance",
"Demography",
"Biology",
"Chemistry",
"Physics & Energy",
"Earth & Climate",
"Space & Astronomy",
"Mathematics",
"Computer Technology",
"Engineering",
"Agriculture & Bio Engineering",
"Transportation",
"Urban Planning",
"Medicine",
"Business & Management",
"Sports & Recreation",
"Communication & Journalism",
"Other"
]
}
}
}, },
"description": { {
"label": "Description", "title": "Credentials",
"placeholder": "i.e. My cool data set", "fields": {
"type": "textarea", "author": {
"required": true, "label": "Author",
"rows": 5 "placeholder": "i.e. Jelly McJellyfish",
"type": "text",
"required": true
},
"copyrightHolder": {
"label": "Copyright Holder",
"placeholder": "i.e. fwhfiw",
"type": "text",
"required": true
},
"license": {
"label": "License",
"type": "select",
"required": true,
"options": [
"Public Domain",
"CC BY: Attribution",
"CC BY-SA: Attribution ShareAlike",
"CC BY-ND: Attribution-NoDerivs",
"CC BY-NC: Attribution-NonCommercial",
"CC BY-NC-SA: Attribution-NonCommercial-ShareAlike",
"CC BY-NC-ND: Attribution-NonCommercial-NoDerivs"
]
}
}
}, },
"categories": { {
"label": "Categories", "title": "Wallet"
"type": "select",
"options": [
"Image Recognition",
"Dataset Of Datasets",
"Language",
"Performing Arts",
"Visual Arts & Design",
"Philosophy",
"History",
"Theology",
"Anthropology & Archeology",
"Sociology",
"Psychology",
"Politics",
"Interdisciplinary",
"Economics & Finance",
"Demography",
"Biology",
"Chemistry",
"Physics & Energy",
"Earth & Climate",
"Space & Astronomy",
"Mathematics",
"Computer Technology",
"Engineering",
"Agriculture & Bio Engineering",
"Transportation",
"Urban Planning",
"Medicine",
"Business & Management",
"Sports & Recreation",
"Communication & Journalism",
"Other"
]
}, },
"copyrightHolder": { {
"label": "Copyright Holder", "title": "Complete"
"placeholder": "i.e. fwhfiw",
"type": "text",
"required": true
},
"author": {
"label": "Author",
"placeholder": "i.e. Jelly McJellyfish",
"type": "text",
"required": true
},
"license": {
"label": "License",
"type": "select",
"required": true,
"options": [
"Public Domain",
"CC BY: Attribution",
"CC BY-SA: Attribution ShareAlike",
"CC BY-ND: Attribution-NoDerivs",
"CC BY-NC: Attribution-NonCommercial",
"CC BY-NC-SA: Attribution-NonCommercial-ShareAlike",
"CC BY-NC-ND: Attribution-NonCommercial-NoDerivs"
]
} }
} ]
} }

View File

@ -0,0 +1,5 @@
@import '../../styles/variables';
.title {
font-size: $font-size-h2;
}

121
src/routes/Publish/Step.tsx Normal file
View File

@ -0,0 +1,121 @@
import React, { PureComponent } from 'react'
import Input from '../../components/atoms/Form/Input'
import Label from '../../components/atoms/Form/Label'
import Row from '../../components/atoms/Form/Row'
import Button from '../../components/atoms/Button'
import { User } from '../../context/User'
import Files from './Files/'
import styles from './Step.module.scss'
interface StepProps {
currentStep: number
index: number
inputChange: any
inputToArrayChange: any
fields?: any[]
files?: any
state: any
title: string
next: any
prev: any
}
export default class Step extends PureComponent<StepProps, {}> {
public previousButton() {
let { currentStep, prev } = this.props
if (currentStep !== 1) {
return <Button onClick={prev}>Previous</Button>
}
return null
}
public nextButton() {
let { currentStep, next } = this.props
if (currentStep < 3) {
return <Button onClick={next}>Next</Button>
}
return null
}
public render() {
const {
currentStep,
index,
title,
fields,
inputChange,
inputToArrayChange,
files,
state
} = this.props
if (currentStep !== index + 1) {
return null
}
return (
<>
<h3 className={styles.title}>{title}</h3>
{fields &&
Object.entries(fields).map(([key, value]) => {
let onChange = inputChange
if (key === 'files' || key === 'categories') {
onChange = inputToArrayChange
}
if (key === 'files') {
return (
<Row key={key}>
<Label htmlFor={key} required>
{value.label}
</Label>
<Files
placeholder={value.placeholder}
name={value.name}
help={value.help}
files={files}
onChange={onChange}
/>
</Row>
)
}
return (
<Input
key={key}
name={key}
label={value.label}
placeholder={value.placeholder}
required={value.required}
type={value.type}
help={value.help}
options={value.options}
onChange={onChange}
rows={value.rows}
value={(state as any)[key]}
/>
)
})}
{this.previousButton()}
{this.nextButton()}
<User.Consumer>
{states =>
states.isLogged ? (
<Button primary>Register asset</Button>
) : (
<Button onClick={states.startLogin}>
Register asset (login first)
</Button>
)
}
</User.Consumer>
</>
)
}
}
Step.contextType = User

View File

@ -1,15 +1,10 @@
import React, { ChangeEvent, Component, FormEvent } from 'react' import React, { ChangeEvent, Component, FormEvent } from 'react'
import { Logger } from '@oceanprotocol/squid' import { Logger } from '@oceanprotocol/squid'
import Route from '../../components/templates/Route' import Route from '../../components/templates/Route'
import Button from '../../components/atoms/Button'
import Form from '../../components/atoms/Form/Form' import Form from '../../components/atoms/Form/Form'
import Input from '../../components/atoms/Form/Input'
import Label from '../../components/atoms/Form/Label'
import Row from '../../components/atoms/Form/Row'
import { User } from '../../context/User'
import AssetModel from '../../models/AssetModel' import AssetModel from '../../models/AssetModel'
import Web3message from '../../components/Web3message' import Web3message from '../../components/Web3message'
import Files from './Files/' import Step from './Step'
import form from '../../data/form-publish.json' import form from '../../data/form-publish.json'
@ -19,7 +14,7 @@ interface PublishState {
name?: string name?: string
dateCreated?: Date dateCreated?: Date
description?: string description?: string
files?: any[] files?: string[]
price?: number price?: number
author?: string author?: string
type?: AssetType type?: AssetType
@ -31,10 +26,12 @@ interface PublishState {
isPublished?: boolean isPublished?: boolean
publishedDid?: string publishedDid?: string
publishingError?: string publishingError?: string
currentStep?: number
} }
class Publish extends Component<{}, PublishState> { class Publish extends Component<{}, PublishState> {
public state = { public state = {
currentStep: 1,
name: '', name: '',
dateCreated: new Date(), dateCreated: new Date(),
description: '', description: '',
@ -51,48 +48,6 @@ class Publish extends Component<{}, PublishState> {
publishingError: '' publishingError: ''
} }
public formFields = (entries: any[]) =>
entries.map(([key, value]) => {
let onChange = this.inputChange
if (key === 'files' || key === 'categories') {
onChange = this.inputToArrayChange
}
if (key === 'files') {
return (
<Row key={key}>
<Label htmlFor={key} required>
{value.label}
</Label>
<Files
placeholder={value.placeholder}
name={value.name}
help={value.help}
files={this.state.files}
onChange={onChange}
/>
</Row>
)
}
return (
<Input
key={key}
name={key}
label={value.label}
placeholder={value.placeholder}
required={value.required}
type={value.type}
help={value.help}
options={value.options}
onChange={onChange}
rows={value.rows}
value={(this.state as any)[key]}
/>
)
})
private inputChange = ( private inputChange = (
event: ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLSelectElement> event: ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLSelectElement>
) => { ) => {
@ -113,6 +68,22 @@ class Publish extends Component<{}, PublishState> {
this.setState({ publishingError: '' }) this.setState({ publishingError: '' })
} }
private next = () => {
let { currentStep } = this.state
currentStep = currentStep >= 2 ? 3 : currentStep + 1
this.setState({
currentStep: currentStep
})
}
private prev = () => {
let { currentStep } = this.state
currentStep = currentStep <= 1 ? 1 : currentStep - 1
this.setState({
currentStep: currentStep
})
}
private toStart = () => { private toStart = () => {
this.setState({ this.setState({
name: '', name: '',
@ -187,8 +158,6 @@ class Publish extends Component<{}, PublishState> {
} }
public render() { public render() {
const entries = Object.entries(form.fields)
return ( return (
<Route title="Publish"> <Route title="Publish">
<Web3message /> <Web3message />
@ -200,25 +169,27 @@ class Publish extends Component<{}, PublishState> {
) : this.state.isPublished ? ( ) : this.state.isPublished ? (
this.publishedState() this.publishedState()
) : ( ) : (
<Form <>
title={form.title} <p>Step {this.state.currentStep} </p>
description={form.description}
onSubmit={this.registerAsset}
>
{this.formFields(entries)}
<User.Consumer> <Form onSubmit={this.registerAsset}>
{states => {form.steps.map((step: any, index: number) => (
states.isLogged ? ( <Step
<Button primary>Register asset</Button> key={index}
) : ( index={index}
<Button onClick={states.startLogin}> title={step.title}
Register asset (login first) currentStep={this.state.currentStep}
</Button> fields={step.fields}
) inputChange={this.inputChange}
} inputToArrayChange={this.inputToArrayChange}
</User.Consumer> files={this.state.files}
</Form> state={this.state}
next={this.next}
prev={this.prev}
/>
))}
</Form>
</>
)} )}
</Route> </Route>
) )
@ -249,5 +220,4 @@ class Publish extends Component<{}, PublishState> {
} }
} }
Publish.contextType = User
export default Publish export default Publish