Skip to content

Commit 5f45f1c

Browse files
author
James Wilson
committed
New Article view now gets topics from the database
1 parent 8b26068 commit 5f45f1c

File tree

1 file changed

+92
-38
lines changed

1 file changed

+92
-38
lines changed

src/NewArticle.js

Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,99 @@ import {BASE_URL} from './constants';
33

44
class NewArticle extends React.Component {
55

6-
state = {
7-
writingAs: '',
8-
title: '',
9-
body:'',
10-
authors: [],
11-
}
12-
13-
componentDidMount () {
14-
fetch(`${BASE_URL}/users`)
15-
.then(response => response.json())
16-
.then(({users}) => this.setState({authors:users}));
17-
}
18-
19-
render () {
20-
const {body, authors} = this.state;
21-
return (
22-
<div>
23-
Writing as:
24-
<select>
25-
{authors.map(author=> (
26-
<option
27-
value={author._id}
28-
> {author.name}</option>
29-
))}
30-
</select>
31-
32-
Title: <input
33-
type="text"
34-
onChange={({target:{value}}) => this.setState({title:value})}/>
35-
36-
Article: <textarea
37-
value={body}
38-
placeholder={'Write your article here.'}
39-
onChange={({target:{value}}) => this.setState({body:value})}/>
40-
41-
6+
state = {
7+
writingAs: '',
8+
title: '',
9+
topic: '',
10+
body:'',
11+
authors: [],
12+
availableTopics: [],
13+
}
14+
15+
componentDidMount () {
16+
fetch(`${BASE_URL}/users`)
17+
.then(response => response.json())
18+
.then(({users}) => this.setState({authors:users}));
19+
20+
fetch(`${BASE_URL}/topics`)
21+
.then(response => response.json())
22+
.then(({topics}) => this.setState({availableTopics: topics}))
23+
}
24+
25+
render () {
26+
const {body, authors, availableTopics} = this.state;
27+
return (
28+
<div className="section">
29+
<form>
30+
<div className="field">
31+
<label className="label">
32+
Writing as
33+
</label>
34+
<select className="select">
35+
{authors.map(({_id, name}) => (
36+
<option
37+
value={_id}
38+
key={_id}
39+
>
40+
{name}
41+
</option>
42+
))}
43+
</select>
44+
</div>
45+
46+
<div className="field">
47+
<label className="label">
48+
Topic
49+
</label>
50+
<select className="select">
51+
{availableTopics.map(({_id, title}) => (
52+
<option
53+
value={_id}
54+
key={_id}
55+
>
56+
{title}
57+
</option>
58+
))}
59+
</select>
60+
</div>
61+
62+
63+
<div className="field">
64+
<label className="label">
65+
Title
66+
</label>
67+
<div className="control">
68+
<input
69+
type="text"
70+
className="input"
71+
onChange={({target:{value}}) => this.setState({title:value})}
72+
placeholder={"What is it about?"}
73+
/>
74+
</div>
75+
</div>
76+
77+
<div className="field">
78+
<label className="label">
79+
Article
80+
</label>
81+
82+
<textarea
83+
className="textarea"
84+
value={body}
85+
placeholder={'Write your article here.'}
86+
onChange={({target:{value}}) => this.setState({body:value})}/>
87+
</div>
88+
89+
<div className="control">
90+
<button className="button">
91+
Submit Article
92+
</button>
4293
</div>
43-
);
44-
}
94+
95+
</form>
96+
</div>
97+
);
98+
}
4599
}
46100

47101
export default NewArticle;

0 commit comments

Comments
 (0)