forked from anusheel/test-clone-base-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBirthdayForm.tsx
49 lines (43 loc) · 1.8 KB
/
BirthdayForm.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
47
48
49
import React, { useEffect, useState } from 'react';
type Order = {
id: number,
name: string,
date: string
}
const BirthdayForm = () => {
const [orders, setOrders] = useState<Order[]>([]);
const [name, setName] = useState('');
const [date, setDate] = useState('');
useEffect(() => {
fetch('/api/orders')
.then(response => response.json())
.then(data => setOrders(data));
}, []);
const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
fetch('/api/orders', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name, date }),
})
.then(response => response.json())
.then(data => setOrders(prevOrders => [...prevOrders, data]));
};
return (
<div className="flex flex-col items-center justify-center w-full max-w-md mx-auto mt-5">
<h2 className="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
Birthday Form
</h2>
<div className="flex flex-col bg-white dark:bg-gray-800 shadow-md rounded-md px-8 py-6">
<label className="text-gray-700 dark:text-gray-200">Name</label>
<input className="mt-2 mb-4 bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-200 rounded-md px-3 py-2" type="text" placeholder="Enter your name" value={name} onChange={e => setName(e.target.value)} />
<label className="text-gray-700 dark:text-gray-200">Date of Birth</label>
<input className="mt-2 bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-200 rounded-md px-3 py-2" type="date" value={date} onChange={e => setDate(e.target.value)} />
<button className="mt-4 py-2 px-4 bg-blue-500 text-white rounded-md hover:bg-blue-600" onClick={handleSubmit}>Submit</button>
</div>
</div>
);
};
export default BirthdayForm;