Skip to content

Commit a101fe9

Browse files
17 - Child Element Event Emitter
1 parent 77eae0b commit a101fe9

File tree

3 files changed

+16
-38
lines changed

3 files changed

+16
-38
lines changed

src/reactify-ui/src/posts/PostCreate.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class PostCreate extends Component {
3535
return response.json()
3636
}).then(function(responseData){
3737
console.log(responseData)
38-
38+
if (thisComp.props.newPostItemCreated){
39+
thisComp.props.newPostItemCreated(responseData)
40+
}
3941
}).catch(function(error){
4042
console.log("error", error)
4143
alert("An error occured, please try again later.")

src/reactify-ui/src/posts/Posts.js

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Posts extends Component {
1010
constructor(props){
1111
super(props)
1212
this.togglePostListClass = this.togglePostListClass.bind(this)
13-
13+
this.handleNewPost = this.handleNewPost.bind(this)
1414
}
1515
state = {
1616
posts: [],
@@ -39,41 +39,17 @@ class Posts extends Component {
3939
})
4040
}
4141

42-
createPost(){
43-
const endpoint = '/api/posts/'
44-
const csrfToken = cookie.load('csrftoken')
45-
let thisComp = this
46-
let data = {
47-
"slug": "",
48-
"title": "",
49-
"content": "",
50-
"draft": false,
51-
"publish": null
52-
}
53-
if (csrfToken !== undefined) {
54-
let lookupOptions = {
55-
method: "POST",
56-
headers: {
57-
'Content-Type': 'application/json',
58-
'X-CSRFToken': csrfToken
59-
},
60-
body: JSON.stringify(data),
61-
credentials: 'include'
62-
}
63-
64-
fetch(endpoint, lookupOptions)
65-
.then(function(response){
66-
return response.json()
67-
}).then(function(responseData){
68-
console.log(responseData)
69-
70-
}).catch(function(error){
71-
console.log("error", error)
72-
})
73-
}
74-
42+
handleNewPost(postItemData){
43+
console.log(postItemData)
44+
let currentPosts = this.state.posts
45+
currentPosts.unshift(postItemData) // unshift
46+
this.setState({
47+
posts: currentPosts
48+
})
7549
}
7650

51+
52+
7753
togglePostListClass(event){
7854
event.preventDefault()
7955
let currentListClass = this.state.postsListClass
@@ -111,7 +87,7 @@ class Posts extends Component {
11187
}) : <p>No Posts Found</p>}
11288
{(csrfToken !== undefined && csrfToken !==null) ?
11389
<div className='my-5'>
114-
<PostCreate />
90+
<PostCreate newPostItemCreated={this.handleNewPost} />
11591
</div>
11692
: ""}
11793

src/staticfiles/js/reactify-django.ui.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)