Skip to content

Commit ab3b20b

Browse files
committed
Post edit functionality works ok
1 parent 5e2ccbc commit ab3b20b

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

client/templates/posts/post_edit.jsx

+46-12
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ PostEdit = React.createClass({
2020
postData: Posts.findOne({_id:this.props._id})
2121
}
2222
},
23-
handleInputChange ( prop, val) {
24-
var stateOb = {
25-
urlValue : val
26-
};
27-
if (prop === "title") {
28-
stateOb = {
29-
titleValue : val
30-
};
31-
}
32-
this.setState(stateOb);
33-
},
3423
// This functions deletes the post and redirect the user into postsList page
3524
deletePost (event) {
3625
event.preventDefault();
@@ -39,10 +28,55 @@ PostEdit = React.createClass({
3928
FlowRouter.go('postsList');
4029
}
4130
},
31+
formSubmission (event) {
32+
event.preventDefault();
33+
// We get the values from inputs
34+
var post = {
35+
url: event.target.url.value,
36+
title: event.target.title.value
37+
};
38+
// We check if all inputs have values
39+
var fieldsErrors = validatePost(post);
40+
// If we didn't fill any of the inputs then we return a message and an error class
41+
if (!_.isEmpty(fieldsErrors)) {
42+
if (fieldsErrors.title) {
43+
this.setState({
44+
errorsTitle: fieldsErrors.title,
45+
errorsTitleClass: "has-error"
46+
});
47+
} else {
48+
this.setState({
49+
errorsTitle: "",
50+
errorsTitleClass: ""
51+
});
52+
}
53+
if (fieldsErrors.url) {
54+
this.setState({
55+
errorsUrl: fieldsErrors.url,
56+
errorsUrlClass: "has-error"
57+
});
58+
} else {
59+
this.setState({
60+
errorsUrl: "",
61+
errorsUrlClass: ""
62+
});
63+
}
64+
return;
65+
}
66+
let postId = this.props._id
67+
Posts.update(postId, {$set: post}, function (error) {
68+
if (error) {
69+
// display the error to the user
70+
throwError(error.reason);
71+
} else {
72+
FlowRouter.go(`/posts/${ postId }`);
73+
}
74+
})
75+
},
4276
render () {
4377
if (this.data.postData ) {
4478
return (
45-
<form className="main form page" onSubmit={this.formSubmition}>
79+
<form className="main form page" onSubmit={(e) => this.formSubmission(e)}>
4680
<PostInput
4781
title={"Title"}
4882
placeholder={"Name your post"}

client/templates/posts/post_submit.jsx

-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ PostInput = React.createClass({
1818
render () {
1919
const errorClassName = "form-group " + this.props.errorClassName;
2020
const inputClass = "form-control "+this.props.title.toLowerCase();
21-
const componentRef = "\""+this.props.title.toLowerCase() +"\""
2221
return (
2322
<div className={errorClassName}>
2423
<label className="control-label" htmlFor={this.props.title.toLowerCase()}>{this.props.title}</label>
@@ -110,17 +109,6 @@ PostSubmit = React.createClass({
110109
FlowRouter.go(`/posts/${ result._id }`);
111110
});
112111
},
113-
handleInputChange (prop, val) {
114-
var stateOb = {
115-
urlValue: val
116-
};
117-
if (prop === "title") {
118-
stateOb = {
119-
titleValue: val
120-
};
121-
}
122-
this.setState(stateOb);
123-
},
124112
render () {
125113
if (this.data.userIsLogged) {
126114
return (

0 commit comments

Comments
 (0)