Skip to content

Commit

Permalink
Integrated data into post/comment objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kq-li committed Aug 19, 2016
1 parent bd5babb commit dfbecce
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 66 deletions.
66 changes: 23 additions & 43 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,38 +327,29 @@ app.controller('PostsCtrl', [
$scope.downvotePost = posts.downvotePost;
$scope.upvoteComment = posts.upvoteComment;
$scope.downvoteComment = posts.downvoteComment;

$scope.postData = {
isAuthor: $scope.currentUser() === $scope.post.author,
isEditing: false,
body: ''
};

$scope.commentData = {};

$scope.post.isAuthor = $scope.currentUser() === $scope.post.author
$scope.post.isEditing = false;
$scope.post.newBody = '';

$scope.post.comments.forEach(function (comment, index) {
$scope.commentData[comment._id] = {
isAuthor: $scope.currentUser() === comment.author,
isEditing: false,
body: ''
};
console.log(comment);
comment.isAuthor = $scope.currentUser() === comment.author;
comment.isEditing = false;
comment.newBody = '';
});

$scope.editPost = function () {
$scope.setData($scope.postData, {
isEditing: true,
body: $scope.post.body
});
$scope.post.isEditing = true;
$scope.post.newBody = $scope.post.body
};

$scope.updatePost = function () {
posts.updatePost($scope.post, {
body: $scope.postData.body
body: $scope.post.newBody
}).then(function (res) {
$scope.setData($scope.postData, {
isEditing: false,
body: ''
});
$scope.post.isEditing = false;
$scope.post.newBody = '';
});
};

Expand All @@ -372,31 +363,26 @@ app.controller('PostsCtrl', [
posts.addComment($scope.post, {
body: $scope.body
}).then(function (res) {
$scope.commentData[res.data._id] = {
isAuthor: true,
isEditing: false,
body: ''
};

var comment = res.data;
comment.isAuthor = true;
comment.isEditing = false;
comment.newBody = '';
$scope.body = '';
});
};

$scope.editComment = function (comment) {
$scope.setData($scope.commentData[comment._id], {
isEditing: true,
body: comment.body
});
comment.isEditing = true;
comment.newBody = comment.body;
};

$scope.updateComment = function (comment) {
console.log(comment.newBody);
posts.updateComment(comment, {
body: $scope.commentData[comment._id].body
body: comment.newBody
}).then(function (res) {
$scope.setData($scope.commentData[comment._id], {
isEditing: false,
body: ''
});
comment.isEditing = false;
comment.newBody = '';
});
};

Expand All @@ -405,12 +391,6 @@ app.controller('PostsCtrl', [
$scope.post.comments.splice($scope.post.comments.indexOf(res.data));
});
};

$scope.setData = function (data, obj) {
Object.keys(obj).forEach(function (key) {
data[key] = obj[key];
});
};
}
]);

Expand Down
4 changes: 3 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ function postFilter(post, user) {
var obj = clientFilter(post, user);

post.comments.forEach(function (comment, index) {
if (typeof comment === 'mongoose.Document')
if (typeof comment === 'mongoose.Document' || 'object') {
console.log('true');
obj.comments[index] = commentFilter(comment, user);
}
});

return obj;
Expand Down
35 changes: 29 additions & 6 deletions views/home.pug
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
form.post-form(ng-show="isLoggedIn()", ng-submit="addPost()") New Post
input(type="text", placeholder="Title", ng-model="title")
input(type="text", placeholder="Link", ng-model="link")
textarea(rows=5, placeholder="Text", ng-model="body")
form.post-form(
ng-show="isLoggedIn()",
ng-submit="addPost()"
) New Post
input(
type="text",
placeholder="Title",
ng-model="title"
)
input(
type="text",
placeholder="Link",
ng-model="link"
)
textarea(
rows=5,
placeholder="Text",
ng-model="body"
)
button(type="submit") Submit
div.alert(ng-hide="isLoggedIn()")
a(href="/login") Login
Expand All @@ -10,10 +25,18 @@ div.alert(ng-hide="isLoggedIn()")
| to post or comment.
div.post-summary(ng-repeat="post in posts | orderBy: '-rating'")
div.upvote-wrapper
div.upvote.enabled(ng-show="isLoggedIn()", ng-class="hasUpvoted(post) ? 'voted' : ''", ng-click="upvotePost(post)") +
div.upvote.enabled(
ng-show="isLoggedIn()",
ng-class="hasUpvoted(post) ? 'voted' : ''",
ng-click="upvotePost(post)"
) +
div.upvote(ng-hide="isLoggedIn()") +
div.rating {{post.rating}}
div.downvote.enabled(ng-show="isLoggedIn()", ng-class="hasDownvoted(post) ? 'voted' : ''", ng-click="downvotePost(post)") -
div.downvote.enabled(
ng-show="isLoggedIn()",
ng-class="hasDownvoted(post) ? 'voted' : ''",
ng-click="downvotePost(post)"
) -
div.downvote(ng-hide="isLoggedIn()") -
div.post-content
div.title
Expand Down
73 changes: 57 additions & 16 deletions views/posts.pug
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
div.post-header
div.upvote-wrapper
div.upvote.enabled(ng-show="isLoggedIn()", ng-class="hasUpvoted(post) ? 'voted' : ''", ng-click="upvotePost(post)") +
div.upvote.enabled(
ng-show="isLoggedIn()",
ng-class="hasUpvoted(post) ? 'voted' : ''",
ng-click="upvotePost(post)"
) +
div.upvote(ng-hide="isLoggedIn()") +
div.rating {{post.rating}}
div.downvote.enabled(ng-show="isLoggedIn()", ng-class="hasDownvoted(post) ? 'voted' : ''", ng-click="downvotePost(post)") -
div.downvote.enabled(
ng-show="isLoggedIn()",
ng-class="hasDownvoted(post) ? 'voted' : ''",
ng-click="downvotePost(post)"
) -
div.downvote(ng-hide="isLoggedIn()") -
div.post-content
div.title
a(ng-show="post.link" href="{{post.link}}") {{post.title}}
div(ng-hide="post.link") {{post.title}}
div.author posted by {{post.author}}
div.body
form.post-edit-form(ng-show="postData.isEditing", ng-submit="updatePost()")
textarea(ng-model="postData.body")
form.post-edit-form(
ng-show="post.isEditing",
ng-submit="updatePost()"
)
textarea(ng-model="post.newBody")
button(type="submit") Submit
div(ng-hide="postData.isEditing") {{post.body}}
div(ng-hide="post.isEditing") {{post.body}}
div.edit
a(ng-click="editPost()", ng-show="postData.isAuthor && !postData.isEditing") Edit
a(
ng-click="editPost()",
ng-show="post.isAuthor && !post.isEditing"
) Edit
div.delete
a(ng-click="deletePost()", ng-show="postData.isAuthor && !postData.isEditing") Delete
form.comment-form(ng-show="isLoggedIn()", ng-submit="addComment()") New Comment
textarea(rows=5, placeholder="Comment", ng-model="body")
a(
ng-click="deletePost()",
ng-show="post.isAuthor && !post.isEditing"
) Delete
form.comment-form(
ng-show="isLoggedIn()",
ng-submit="addComment()"
) New Comment
textarea(
rows=5,
placeholder="Comment",
ng-model="body"
)
button(type="submit") Submit
div.alert(ng-hide="isLoggedIn()")
a(href="/login") Login
Expand All @@ -29,19 +53,36 @@ div.alert(ng-hide="isLoggedIn()")
| to post or comment.
div.comment(ng-repeat="comment in post.comments | orderBy: '-rating'")
div.upvote-wrapper
div.upvote.enabled(ng-show="isLoggedIn()", ng-class="hasUpvoted(comment) ? 'voted' : ''", ng-click="upvoteComment(comment)") +
div.upvote.enabled(
ng-show="isLoggedIn()",
ng-class="hasUpvoted(comment) ? 'voted' : ''",
ng-click="upvoteComment(comment)"
) +
div.upvote(ng-hide="isLoggedIn()") +
div.rating {{comment.rating}}
div.downvote.enabled(ng-show="isLoggedIn()", ng-class="hasDownvoted(comment) ? 'voted' : ''", ng-click="downvoteComment(comment)") -
div.downvote.enabled(
ng-show="isLoggedIn()",
ng-class="hasDownvoted(comment) ? 'voted' : ''",
ng-click="downvoteComment(comment)"
) -
div.downvote(ng-hide="isLoggedIn()") -
div.comment-content
div.author {{comment.author}}
div.body
form.comment-edit-form(ng-show="commentData[comment._id].isEditing", ng-submit="updateComment(comment)")
textarea(ng-model="commentData[comment._id].body")
form.comment-edit-form(
ng-show="comment.isEditing",
ng-submit="updateComment(comment)"
)
textarea(ng-model="comment.newBody")
button(type="submit") Submit
div(ng-hide="commentData[comment._id].isEditing") {{comment.body}}
div(ng-hide="comment.isEditing") {{comment.body}}
div.edit
a(ng-click="editComment(comment)", ng-show="commentData[comment._id].isAuthor && !commentData[comment._id].isEditing") Edit
a(
ng-click="editComment(comment)",
ng-show="comment.isAuthor && !comment.isEditing"
) Edit
div.delete
a(ng-click="deleteComment(comment)", ng-show="commentData[comment._id].isAuthor && !commentData[comment._id].isEditing") Delete
a(
ng-click="deleteComment(comment)",
ng-show="comment.isAuthor && !comment.isEditing"
) Delete

0 comments on commit dfbecce

Please sign in to comment.