Browse Source

Commit 8-1: Added edit post form.

Frederic G. MARAND 9 years ago
parent
commit
bbba9bc51e

+ 1 - 0
.meteor/packages

@@ -12,3 +12,4 @@ sacha:spin
 ian:accounts-ui-bootstrap-3
 accounts-password
 audit-argument-checks
+insecure

+ 1 - 0
.meteor/versions

@@ -21,6 +21,7 @@ htmljs@1.0.4
 http@1.1.0
 ian:accounts-ui-bootstrap-3@1.2.79
 id-map@1.0.3
+insecure@1.0.3
 iron:controller@1.0.8
 iron:core@1.0.8
 iron:dynamic-template@1.0.8

+ 17 - 0
client/templates/posts/post_edit.html

@@ -0,0 +1,17 @@
+<template name="postEdit">
+  <form class="main form page">
+    <div class="form-group">
+      <label class="control-label" for="url">URL</label>
+      <input type="text" name="url" id="url" value="{{url}}" placeholder="Your URL" class="form-control" />
+    </div>
+
+    <div class="form-group">
+      <label class="control-label" for="title">Title</label>
+      <input type="text" name="title" id="title" value="{{title}}" placeholder="Name yout post" class="form-control" />
+    </div>
+
+    <input type="submit" value="Submit" class="btn btn-primary submit" />
+    <hr />
+    <a class="btn btn-danger delete" href="#">Delete post</a>
+  </form>
+</template>

+ 43 - 0
client/templates/posts/post_edit.js

@@ -0,0 +1,43 @@
+/**
+ * @file
+ *
+ *
+ * User: marand
+ * Date: 02/09/15
+ * Time: 08:43
+ */
+
+Template.postEdit.events({
+  'submit form': function (e) {
+    e.preventDefault();
+
+    var currentPostId = this._id;
+
+    var postProperties = {
+      url: $(e.target).find('[name=url]').val(),
+      title: $(e.target).find('[name=title]').val()
+    };
+
+
+    Posts.update(currentPostId, { $set: postProperties },function (error, result) {
+      // Display the error to the user and abort.
+      if (error) {
+        return alert(error.reason);
+      }
+      else {
+        Router.go('postPage', { _id: currentPostId });
+      }
+    });
+  },
+
+  'click .delete': function (e) {
+    e.preventDefault();
+
+    if (confirm("Delete this post ?")) {
+      var currentPostId = this._id;
+      Posts.remove(currentPostId);
+      Router.go('postsList');
+    }
+
+  }
+});

+ 4 - 0
client/templates/posts/post_item.html

@@ -2,6 +2,10 @@
   <div class="post">
     <div class="post-content">
       <h3><a href="{{url}}">{{title}}</a><span>{{domain}}</span></h3>
+      <p>Submitted by {{author}}</p>
+      {{#if ownPost}}
+        <a href="{{pathFor 'postEdit' }}">Edit</a>
+      {{/if}}
     </div>
     {{! La route postPage nécessite un _id, donc IR va le chercher dans le
       data context, qui se trouve à ce stade être le document concerné, qui

+ 5 - 0
client/templates/posts/post_item.js

@@ -8,6 +8,11 @@
  */
 
 Template.postItem.helpers({
+  ownPost: function ()  {
+    // Template is invoked in the context of a Post, so 'this' is a Post.
+    return this.userId = Meteor.userId();
+  },
+
   domain: function () {
     var a = document.createElement('a');
     a.href = this.url;

+ 2 - 1
lib/collections/posts.js

@@ -15,6 +15,7 @@ Posts = new Mongo.Collection('posts');
 // This is in lib/ instead of server/ for latency compensation (?).
 Meteor.methods({
   postInsert: function(postAttributes) {
+    "use strict";
     check(Meteor.userId(), String);
     check(postAttributes, {
       title: String,
@@ -27,7 +28,7 @@ Meteor.methods({
       return {
         postExists: true,
         _id: postWithSameLink._id
-      }
+      };
     }
 
     var user = Meteor.user();

+ 8 - 0
lib/router.js

@@ -29,6 +29,14 @@ Router.route('/posts/:_id', {
   }
 });
 
+Router.route('/posts/:_id/edit', {
+  name: 'postEdit',
+  data: function () {
+    // "this" is the matched route.
+    return Posts.findOne(this.params._id);
+  }
+});
+
 Router.route('/submit', {
   name: 'postSubmit'
 });