Hi, I m trying to create a " post page" and im really stuck for some time, I’m trying to make a page where you can add/edit/remove a post, but I can’t make the post remove/edit to work. What am I doing wrong?
'create_a_standard_post': function () {
return Posts.insert({type: 'standard'})
},
'create_a_post': function (type) {
return Posts.insert({type: type});
},
'post.find': function (id) {
return Posts.findOne({_id: id});
},
'post.remove': function (post) {
if (!this.userId) {
throw new Meteor.Error('not-authorized');
}
Posts.remove(post._id);
},
'posts.list': function () {
return Posts.find().fetch();
},
'post.add': function (data) {
if (!this.userId) {
throw new Meteor.Error('not-authorized');
}
data.userId = this.userId;
console.log(data);
Posts.insert ({
text: data.name ,
createdAt: new Date(),
userId: this.userId
});
}
,
'post.edit': function(id, data) {
if(!this.userId){
throw new Meteor.Error('not-authorized');
}
Posts.update(id, {$set: {name: data.name}});
}
});
class PostsList extends React.Component {
constructor() {
super();
}
removePost() {
Meteor.call('post.remove', this.props.post)
}
render() {
const {loading, posts} = this.props;
if (loading) {
return (
<div>Loading...</div>
)
} else {
return (
<div>
{
posts.map((post) => {
return (
<div key={post._id}>
<div>{post.name}</div>
<a href={"/post/edit/" + post._id}>Edit</a>
<button onClick={this.removePost.bind(this)}>Remove</button>
</div>
)
})
}
<a href="/posts/create">Create a post</a>
</div>
)
}
}
}
export default createContainer(() => {
const handle = Meteor.subscribe('posts');
return {
loading: !handle.ready(),
posts: Posts.find().fetch(),
}
}, PostsList);
export default class PostsCreate extends Component {
constructor() {
super();
}
addPost(post) {
Meteor.call('post.add', post, (err, result) => {
if(!err) {
alert('post added!');
}
});
}
render() {
return (
<div>
<Formsy.Form onValidSubmit={this.addPost.bind(this)}>
<Input type="text" name="name" placeholder="" value=""/>
<button type="submit" className="btn btn-default">Add post</button>
</Formsy.Form>
<a href="/posts">posts</a>
</div>
);
}
}
export default class PostEdit extends Component {
constructor() {
super();
this.state = {loading: true, post: null}
}
componentDidMount() {
Meteor.call('post.find', route.current().params.id, (err, res) => {
this.setState({
loading: false,
post: res
})
})
}
editPost (data) {
Meteor.call('post.edit', this.state.post._id, data, (err, result) => {
if(!err) {
alert('post modified!');
}
});
}
render() {
if (this.state.loading) {
return (
<div>Loading...</div>
)
} else {
return (
<div>
<Formsy.Form onValidSubmit={this.editPost().bind(this)}>
<Input type="text" name="name" placeholder="post" value={this.state.post.name}/>
<button type="submit" className="btn btn-default">Edit</button>
</Formsy.Form>
<a href="/posts">Back to list</a>
</div>
);
}
}
}