-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestEcho.tsx
46 lines (40 loc) · 1.53 KB
/
RestEcho.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2020 Gonzalo Müller Bravo.
import * as React from 'react'
import { Link } from 'react-router-dom'
import { LabeledImage } from '../gadgets/LabeledImage'
import styles from './RestEcho.css'
import { useEcho } from './useEcho'
const enum FieldsNames {
from = '@from'
}
function extractData(form: HTMLFormElement): string {
return (form[FieldsNames.from] as HTMLInputElement).value
}
function processForm(event: React.FormEvent<HTMLFormElement>): HTMLFormElement {
event.preventDefault()
event.stopPropagation()
return event.currentTarget
}
export function RestEcho(): React.ReactElement {
const { echoText, disabled, handleSubmit } = useEcho()
return (
<React.Fragment>
<form className='box' onSubmit={async event => handleSubmit(extractData(processForm(event)))}>
<div className='field'>
<label className='label'>Text to send:</label>
<input name={FieldsNames.from} type='text' className='input is-primary' disabled={disabled}/>
</div>
<button data-x='rest-echo-button' className='button is-primary' disabled={disabled}>
<LabeledImage image='imgs/go.svg' label='Send' imageClassName='contained-vertical-image'/>
</button>
<div className='field'>
<label className='label'>Result:</label>
<div data-x='rest-echo-result' className={`box ${styles.result}`}>
{echoText}
</div>
</div>
</form>
<Link to='/' className='button is-link is-small is-outlined'>Home</Link>
</React.Fragment>
)
}