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

78 lines
1.9 KiB
TypeScript
Raw Normal View History

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'
2021-03-08 02:17:00 +00:00
import settings from '../settings'
2021-03-08 01:18:51 +00:00
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
setError('Phone number needs to be a length of at least 10')
return
}
2021-03-08 02:17:00 +00:00
if (settings.env === 'jank') {
history.push(`/sessions/${phone}`)
return
}
await createClient({ name, email, phone: parseInt(phone) })
2021-03-08 01:18:51 +00:00
history.push(`/sessions/${phone}`)
}
return (
<div>
<h1>Dashboard</h1>
<form onSubmit={handleSubmit}>
<label htmlFor="name">
Name:
2021-03-08 02:17:00 +00:00
<input
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>
<button type="submit">Start Session</button>
2021-03-08 02:17:00 +00:00
<button type="button" onClick={handleReset}>
Reset
</button>
2021-03-08 03:08:18 +00:00
{error && <p className="error">{error}</p>}
2021-03-08 01:18:51 +00:00
</form>
</div>
)
}