Skip to content

Commit

Permalink
Add delete news item
Browse files Browse the repository at this point in the history
(Refs Issue larvalabs#100)
* User can delete their own news item and all child comments
* Make delete button a mixin
* Add confirmation modal to both news item and comment delete
  • Loading branch information
mreinhardt committed Feb 17, 2014
1 parent d10c632 commit 986c04f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ app.get('/news/summarize', passportConf.isAuthenticated, newsController.summariz

app.get('/news/source/:source', newsController.sourceNews);
app.get('/news/:id', newsController.comments);
app.post('/news/:id/delete', newsController.deleteNewsItemAndComments);
app.post('/news/:id/comments', passportConf.isAuthenticated, newsController.postComment);
app.post('/news/:id/comments/:comment_id/delete', passportConf.isAuthenticated, newsController.deleteComment);
app.post('/news/:id', votesController.voteFor('news', '/'));
Expand Down
34 changes: 34 additions & 0 deletions controllers/news.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,40 @@ exports.comments = function (req, res, next) {
});
};

exports.deleteNewsItemAndComments = function (req, res, next) {
var errors = req.validationErrors();

if (!req.user) {
errors.push({
param: 'user',
msg: 'User must be logged in.',
value: undefined
});
}

if (errors) {
req.flash('errors', errors);
return res.redirect('back');
}

async.parallel({
newsItem: function(cb) {
NewsItem
.findByIdAndRemove(req.params.id)
.exec(cb);
},
comments: function(cb) {
Comment
.remove({item: req.params.id}, cb);
}
}, function (err, results) {
if (err) res.redirect('back');

req.flash('success', { msg: 'News item and comments deleted.' });
res.redirect('/news');
});
};

/**
* POST /news/:id/comments
* Post a comment about a news page
Expand Down
23 changes: 23 additions & 0 deletions public/css/styles.less
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ body {
.alert { margin-bottom: 0; }
}

.page-header { position: relative; }

// Footer
// -------------------------

#footer {
height: 75px;
padding-top: 15px;
Expand Down Expand Up @@ -83,6 +86,22 @@ body {
}
}

// Global Elements
// -------------------------

.delete-form { display: inline; }
.delete {
font-size: 12px;
line-height: 0.9;
padding: 2px;
width: 16px; height: 16px;
position: absolute; top: 6px; right: 2px;
border-radius: 50%;
}

.modal { font-size: @font-size-base; }
.modal .modal-footer button { margin: 0 0 0 10px; }

// Navbar
// -------------------------

Expand Down Expand Up @@ -125,6 +144,10 @@ body {
// News Table
// -------------------------

.news-table td {
position: relative;
}

.news-table a:visited {
color: #a6bfbb;
}
Expand Down
16 changes: 16 additions & 0 deletions views/mixins/delete.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mixin delete(owner, url)
- var modalId = url.replace(/\//g, "")
if user && user.username == owner.username
button(type='button', data-toggle='modal', data-target='##{modalId}' class='delete btn btn-danger btn-xs pull-right', title='Delete') ×
div.modal.fade(id=modalId)
div.modal-dialog
div.modal-content
div.modal-header
button.close(type='button', data-dismiss='modal', aria-hidden='true') ×
h4.modal-title Confirm Delete
div.modal-body
p Are you sure you want to permanently delete this item?
div.modal-footer
button.btn.btn-default(type='button', data-dismiss='modal') Cancel
form(action=url, method='POST', class='delete-form')
button.btn.btn-danger(type='submit') Delete
6 changes: 3 additions & 3 deletions views/news/index.jade
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extends ../layout
include ../mixins/delete

block content
include ../partials/joinsite
Expand Down Expand Up @@ -73,6 +74,7 @@ block content
|  
a(href="#", class="show-summary")
| ...
+delete(item.poster, '/news/' + item._id + '/delete')
br
span
small.submit-date(title="#{item.created}")= timeago(item.created)
Expand Down Expand Up @@ -114,9 +116,7 @@ block content
td.text-muted= index + 1
td.comment
blockquote.content= comment.contents
if user && user.username == comment.poster.username
form(action="/news/" + comment.newsItem._id + "/comments/" + comment._id + "/delete", method='POST', class="delete-form")
button(type="submit", class="delete btn btn-danger btn-xs pull-right", title="Delete")='×'
+delete(comment.poster, '/news/' + comment.newsItem._id + '/comments/' + comment._id + '/delete')
small.timeago(title="#{comment.created}")= timeago(comment.created)
td.parent
a(href=comment.newsItem.url)
Expand Down
6 changes: 3 additions & 3 deletions views/news/show.jade
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extends ../layout
include ../mixins/delete

block content
div.news-item
Expand All @@ -11,6 +12,7 @@ block content
|  
a(href="#", class="show-summary")
| ...
+delete(item.poster, '/news/' + item._id + '/delete')
h4
div(class="item-summary #{item.isSelfPost() ? '' : 'hidden'}")!= item.summary
= 'submitted '
Expand All @@ -31,7 +33,5 @@ block content
a(href='/news/user/' + comment.poster.username)= comment.poster.username
span.timeago(title="#{comment.created}")= timeago(comment.created)
blockquote.content
if user && user.username == comment.poster.username
form(action="/news/" + item.id + "/comments/" + comment.id + "/delete", method='POST', class="delete-form")
button(type="submit", class="delete btn btn-danger btn-xs pull-right", title="Delete") ×
+delete(comment.poster, '/news/' + item.id + '/comments/' + comment.id + '/delete')
!= comment.contents

0 comments on commit 986c04f

Please sign in to comment.