commons/client/src/routes/Publish/Step.tsx

180 lines
5.5 KiB
TypeScript
Raw Normal View History

2019-03-29 19:44:58 +01:00
import React, { PureComponent, FormEvent, ChangeEvent } 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, Market } from '../../context'
import Files from './Files/'
2019-02-20 13:46:54 +01:00
import StepRegisterContent from './StepRegisterContent'
import styles from './Step.module.scss'
import Web3message from '../../components/organisms/Web3message'
2019-03-29 19:44:58 +01:00
interface Fields {
label: string
placeholder?: string
help?: string
type: string
required?: boolean
options?: string
rows?: number
}
interface StepProps {
currentStep: number
index: number
2019-03-29 19:44:58 +01:00
inputChange(
event:
| FormEvent<HTMLInputElement>
| ChangeEvent<HTMLInputElement>
| ChangeEvent<HTMLSelectElement>
| ChangeEvent<HTMLTextAreaElement>
): void
fields?: Fields
state: any
title: string
2019-02-20 11:12:10 +01:00
description: string
2019-03-29 19:44:58 +01:00
next(): void
prev(): void
2019-02-19 17:26:15 +01:00
totalSteps: number
2019-03-29 19:44:58 +01:00
tryAgain(): void
toStart(): void
2019-02-20 13:46:54 +01:00
publishedDid?: string
content?: string
}
2019-02-20 16:50:27 +01:00
export default class Step extends PureComponent<StepProps, {}> {
public static contextType = User
public previousButton() {
2019-02-20 16:50:27 +01:00
const { currentStep, prev } = this.props
if (currentStep !== 1) {
2019-02-20 11:12:10 +01:00
return (
<Button link onClick={prev}>
Previous
</Button>
)
}
return null
}
public nextButton() {
2019-02-20 16:50:27 +01:00
const { currentStep, next, totalSteps, state } = this.props
2019-02-19 17:26:15 +01:00
if (currentStep < totalSteps) {
return (
2019-02-20 16:50:27 +01:00
<Button
2019-02-21 14:35:25 +01:00
disabled={
!state.validationStatus[currentStep].allFieldsValid
}
2019-02-20 16:50:27 +01:00
onClick={next}
>
Next
</Button>
)
}
return null
}
public render() {
const {
currentStep,
index,
title,
2019-02-20 11:12:10 +01:00
description,
fields,
inputChange,
2019-02-19 17:26:15 +01:00
state,
totalSteps,
2019-02-20 13:46:54 +01:00
tryAgain,
toStart,
content
} = this.props
if (currentStep !== index + 1) {
return null
}
const lastStep = currentStep === totalSteps
return (
<>
2019-02-20 11:12:10 +01:00
<header className={styles.header}>
<h2 className={styles.title}>{title}</h2>
<p className={styles.description}>{description}</p>
</header>
{fields &&
Object.entries(fields).map(([key, value]) => {
if (key === 'files') {
return (
<Row key={key}>
<Label htmlFor={key} required>
{value.label}
</Label>
<Files
placeholder={value.placeholder}
2019-02-21 14:35:25 +01:00
name={key}
help={value.help}
2019-02-20 13:46:54 +01:00
files={state.files}
2019-03-26 17:42:24 +01:00
onChange={inputChange}
/>
</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}
2019-03-26 17:42:24 +01:00
onChange={inputChange}
rows={value.rows}
value={(state as any)[key]}
/>
)
})}
2019-02-20 13:46:54 +01:00
{lastStep && (
<StepRegisterContent
tryAgain={tryAgain}
toStart={toStart}
state={state}
content={content}
2019-02-20 13:46:54 +01:00
/>
)}
2019-02-20 11:12:10 +01:00
<div className={styles.actions}>
{this.previousButton()}
{this.nextButton()}
{lastStep && (
<Market.Consumer>
2020-05-19 10:36:18 +02:00
{(market) => (
<Button
disabled={
!this.context.isLogged ||
!market.networkMatch ||
this.props.state.isPublishing
}
primary
>
Register asset
</Button>
)}
</Market.Consumer>
2019-02-20 11:12:10 +01:00
)}
</div>
<div className={styles.account}>
{!lastStep && <Web3message />}
</div>
</>
)
}
}