3Dshock/client/src/pages/Dashboard.tsx

87 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-03-08 05:48:48 +00:00
import { Button, Divider, message, PageHeader } from 'antd'
2021-03-08 03:45:57 +00:00
import { Content } from 'antd/lib/layout/layout'
2021-03-08 02:17:00 +00:00
import React, { FormEvent } from 'react'
2021-03-08 01:18:51 +00:00
import { useState } from 'react'
import { useHistory } from 'react-router-dom'
import { createClient } from '../api'
export const Dashboard = () => {
const history = useHistory()
2021-03-08 03:08:18 +00:00
const [error, setError] = useState<string | null>(null)
2021-03-08 01:18:51 +00:00
const [name, setName] = useState('')
const [email, setEmail] = useState('')
const [phone, setPhone] = useState('')
const handleReset = () => {
//
2021-03-08 02:17:00 +00:00
setName('')
setEmail('')
setPhone('')
2021-03-08 01:18:51 +00:00
}
2021-03-08 02:17:00 +00:00
const handleSubmit = async (e: FormEvent) => {
e.preventDefault()
2021-03-08 01:18:51 +00:00
2021-03-08 03:08:18 +00:00
if (phone.length < 10) {
// helpful message
2021-03-08 03:56:20 +00:00
message.error('Check all fields!')
2021-03-08 03:08:18 +00:00
setError('Phone number needs to be a length of at least 10')
return
}
2021-03-08 04:46:40 +00:00
const client_id = await createClient({
name,
email,
phone: parseInt(phone),
})
2021-03-08 02:17:00 +00:00
2021-03-08 04:46:40 +00:00
history.push(`/sessions/${client_id}`)
2021-03-08 01:18:51 +00:00
}
return (
2021-03-08 03:45:57 +00:00
<Content>
2021-03-08 05:48:48 +00:00
<PageHeader
title="Dashboard"
subTitle="Enter the name, email and phone number of the subject"
></PageHeader>
<Divider />
2021-03-08 01:18:51 +00:00
<form onSubmit={handleSubmit}>
<label htmlFor="name">
Name:
2021-03-08 02:17:00 +00:00
<input
2021-03-08 03:56:20 +00:00
minLength={3}
2021-03-08 02:17:00 +00:00
value={name}
onChange={(e) => setName(e.target.value)}
name="name"
/>
2021-03-08 01:18:51 +00:00
</label>
<label htmlFor="email">
Email:
2021-03-08 02:17:00 +00:00
<input
value={email}
onChange={(e) => setEmail(e.target.value)}
type="email"
name="email"
/>
2021-03-08 01:18:51 +00:00
</label>
<label htmlFor="phone">
Phone:
2021-03-08 02:17:00 +00:00
<input
value={phone}
onChange={(e) => setPhone(e.target.value)}
type="tel"
name="phone"
/>
2021-03-08 01:18:51 +00:00
</label>
2021-03-08 05:48:48 +00:00
<Button danger onClick={handleReset}>
2021-03-08 02:17:00 +00:00
Reset
2021-03-08 05:48:48 +00:00
</Button>
<Button htmlType="submit" type="primary">
Start Session
</Button>
2021-03-08 03:08:18 +00:00
{error && <p className="error">{error}</p>}
2021-03-08 01:18:51 +00:00
</form>
2021-03-08 03:45:57 +00:00
</Content>
2021-03-08 01:18:51 +00:00
)
}