-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.jsx
More file actions
42 lines (39 loc) · 1.6 KB
/
Copy pathcreate.jsx
File metadata and controls
42 lines (39 loc) · 1.6 KB
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
import { useState } from "react";
import { useHistory } from "react-router-dom";
const Create = () => {
const [ isPending, setIsPending] = useState(false);
const [title, setTitle] = useState('');
const [body, setBody] = useState('');
const [author, setAuthor] = useState('mario');
const history = useHistory();
const handleSubmit = (e) => {
e.preventDefault()
const blog = {title, body, author}
setIsPending(true);
fetch('https://ubiquitous-rotary-phone-x554744v7w6g3xq6-8000.app.github.dev/blogs', {
method: 'POST',
headers: {'content-Type': "application/json"},
body: JSON.stringify(blog)
}).then(() => {
setIsPending(false)
history.push('/')
})
}
return (
<div className="create">
<h2>Add a new blog</h2>
<form onSubmit={handleSubmit}>
<label > Blog Title</label>
<input type="text" required value={title} onChange={(e) => setTitle(e.target.value)}/>
<label > Blog body</label>
<textarea required value={body} onChange={(e) => setBody(e.target.value)} ></textarea>
<label > Blog Author</label>
<select value= {author} onChange={(e) => setAuthor(e.target.value)}> <option value="mario">Mario</option>
<option value="Yoshi">Yoshi</option></select>
{!isPending && <button>Add Blog</button>}
{isPending && <button disabled>Adding Blog</button>}
</form>
</div>
);
}
export default Create;