-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathArticleForm.js
78 lines (70 loc) · 2.44 KB
/
ArticleForm.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React, { useEffect, useState } from 'react'
import PT from 'prop-types'
const initialFormValues = { title: '', text: '', topic: '' }
export default function ArticleForm(props) {
const [values, setValues] = useState(initialFormValues)
// ✨ where are my props? Destructure them here
useEffect(() => {
// ✨ implement
// Every time the `currentArticle` prop changes, we should check it for truthiness:
// if it's truthy, we should set its title, text and topic into the corresponding
// values of the form. If it's not, we should reset the form back to initial values.
})
const onChange = evt => {
const { id, value } = evt.target
setValues({ ...values, [id]: value })
}
const onSubmit = evt => {
evt.preventDefault()
// ✨ implement
// We must submit a new post or update an existing one,
// depending on the truthyness of the `currentArticle` prop.
}
const isDisabled = () => {
// ✨ implement
// Make sure the inputs have some values
}
return (
// ✨ fix the JSX: make the heading display either "Edit" or "Create"
// and replace Function.prototype with the correct function
<form id="form" onSubmit={onSubmit}>
<h2>Create Article</h2>
<input
maxLength={50}
onChange={onChange}
value={values.title}
placeholder="Enter title"
id="title"
/>
<textarea
maxLength={200}
onChange={onChange}
value={values.text}
placeholder="Enter text"
id="text"
/>
<select onChange={onChange} id="topic" value={values.topic}>
<option value="">-- Select topic --</option>
<option value="JavaScript">JavaScript</option>
<option value="React">React</option>
<option value="Node">Node</option>
</select>
<div className="button-group">
<button disabled={isDisabled()} id="submitArticle">Submit</button>
<button onClick={Function.prototype}>Cancel edit</button>
</div>
</form>
)
}
// 🔥 No touchy: ArticleForm expects the following props exactly:
ArticleForm.propTypes = {
postArticle: PT.func.isRequired,
updateArticle: PT.func.isRequired,
setCurrentArticleId: PT.func.isRequired,
currentArticle: PT.shape({ // can be null or undefined, meaning "create" mode (as opposed to "update")
article_id: PT.number.isRequired,
title: PT.string.isRequired,
text: PT.string.isRequired,
topic: PT.string.isRequired,
})
}