Skip to content

Commit

Permalink
Add delete function
Browse files Browse the repository at this point in the history
  • Loading branch information
rikster-r committed Jan 21, 2023
1 parent 520bcb1 commit 402b2b9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
18 changes: 17 additions & 1 deletion public/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ header .secondary-action {
}

.messages-list {
max-width: 500px;
display: flex;
flex-direction: column;
gap: 1rem;
Expand All @@ -212,10 +213,25 @@ header .secondary-action {

.message-block {
border-left: 10px solid #c084fc;
padding: 0.1rem 1.5rem;
padding: 0 1.5rem;
height: max-content;
}

.message-block .title {
font-weight: 700;
display: flex;
}

.message-block .title .delete {
margin-left: 0.5rem;
}

.message-block .title .delete:hover {
cursor: pointer;
}

.delete-icon {
width: 24px;
height: 24px;
color: #7e22ce;
}
8 changes: 8 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,12 @@ router.post('/new-message', body('message').trim().not().isEmpty().escape(), (re
res.redirect('/');
});

router.get('/:id/delete', (req, res, next) => {
Message.findByIdAndDelete(req.params.id, err => {
if (err) return next(err);

res.redirect('/');
});
});

module.exports = router;
12 changes: 10 additions & 2 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
<% messages.forEach(message => { %>
<div class="message-block">
<p><%= message.text %> </p>
<p class="title">
<div class="title">
<% if (user?.isMember) { %>
<%= message.author.username %> -
<% } else { %>
Anonymous -
<% } %>
<%= message.date %> </p>
<%= message.date %>
<% if (user?.username === message.author.username) { %>
<a href="/<%= `${message._id}/delete` %>" class="delete">
<svg class="delete-icon" viewBox="0 0 24 24">
<path fill="currentColor" d="M9,3V4H4V6H5V19A2,2 0 0,0 7,21H17A2,2 0 0,0 19,19V6H20V4H15V3H9M7,6H17V19H7V6M9,8V17H11V8H9M13,8V17H15V8H13Z" />
</svg>
</a>
<% } %>
</div>
</div>
<% }) %>
</div>
Expand Down

0 comments on commit 402b2b9

Please sign in to comment.