You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.1 KiB

3 years ago
import React from 'react'
3 years ago
import { Button, Divider, Form, Input, message, Row, Typography } from 'antd'
3 years ago
import FormItem from 'antd/lib/form/FormItem'
3 years ago
import { Content } from 'antd/lib/layout/layout'
3 years ago
import { useState } from 'react'
import { useHistory } from 'react-router-dom'
import { createClient } from '../api'
3 years ago
type FormData = {
name: string
email: string
phone: string
}
3 years ago
export const Dashboard = () => {
const history = useHistory()
3 years ago
const [error, setError] = useState<string | null>(null)
3 years ago
const [form] = Form.useForm<FormData>()
3 years ago
3 years ago
const handleReset = () => {
form.resetFields()
}
3 years ago
3 years ago
const handleSubmit = async (values: FormData) => {
3 years ago
if (values.phone.length < 10) {
3 years ago
// helpful message
3 years ago
message.error('Check all fields!')
3 years ago
setError('Phone number needs to be a length of at least 10')
return
}
3 years ago
const client_id = await createClient({
3 years ago
name: values.name,
email: values.email,
phone: parseInt(values.phone.replace(/\D/g, '')),
3 years ago
})
3 years ago
3 years ago
history.push(`/sessions/${client_id}`)
3 years ago
}
return (
3 years ago
<Content>
3 years ago
<Typography.Title className="page-head" level={3}>
Dashboard
</Typography.Title>
3 years ago
<Divider />
3 years ago
<Form
3 years ago
form={form}
3 years ago
className="dashboard-form"
onFinish={handleSubmit}
labelCol={{ span: 8 }}
wrapperCol={{ span: 16 }}
>
3 years ago
<Typography.Paragraph style={{ textAlign: 'center' }}>
Enter the name, email and phone number of the subject
</Typography.Paragraph>
3 years ago
<FormItem label="name" name="name">
<Input minLength={3} />
</FormItem>
<FormItem label="email" name="email">
<Input type="email" />
</FormItem>
<FormItem label="phone" name="phone">
<Input type="tel" minLength={10} />
</FormItem>
<Row justify="space-between">
<Button danger onClick={handleReset}>
Reset
</Button>
<Button htmlType="submit" type="primary">
Start Session
</Button>
</Row>
3 years ago
{error && <p className="error">{error}</p>}
3 years ago
</Form>
3 years ago
</Content>
3 years ago
)
}