Skip to content

Commit 5d45327

Browse files
author
corey-mitchell
committed
saves comment, now to figure out how to get them to link up to the article.
1 parent 6b8f780 commit 5d45327

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

models/Comment.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ const Schema = mongoose.Schema;
66

77
// Create a new Comment Schema
88
const CommentSchema = new Schema({
9-
title: String,
10-
body: String
9+
body: {
10+
type: String,
11+
required: true
12+
}
1113
});
1214

1315
// Creates model from the above Schema

public/assets/javascript/app.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ $(document).ready(()=>{
7272
$("<h4>").text(`Notes For Article: ${currentArticle}`),
7373
$("<hr>"),
7474
$("<ul class='list-group comment-container'>"),
75-
$("<textarea placeholder='New Note' rows='4' cols='60'>"),
76-
$("<button class='btn btn-success saveComment'>Save Comment</button>")
75+
$("<textarea id='text' placeholder='New Note' rows='4' cols='60'>"),
76+
$(`<button class='btn btn-success saveComment' data-id=${currentArticle}>Save Comment</button>`)
7777
);
7878

7979
// Opens modal with above html
@@ -90,17 +90,31 @@ $(document).ready(()=>{
9090

9191
// Adding some information about the article and article notes to the save button for easy access
9292
// When trying to add a new note
93-
// $(".btn.save").data("article", noteData);
93+
$(".btn.save").data("article", commentData);
9494
// renderNotesList will populate the actual note HTML inside of the modal we just created/opened
95-
// renderNotesList(noteData);
95+
renderNotesList(commentData);
9696
});
9797

9898
});
9999

100100
// Handles save comment button
101-
$(document).on('click', '.saveComment', ()=>{
102-
console.log('button clicked');
103-
})
101+
$(document).on('click', '.saveComment', function() {
102+
// Targets article id
103+
const currentArticle = $(this)
104+
.data('id');
105+
106+
// Targets textarea text
107+
const body = $("#text").val().trim();
108+
109+
// console.log(currentArticle, text);
110+
111+
$.ajax(`/articles/${currentArticle}`, {
112+
type: "POST",
113+
data: {body}
114+
}).then((data)=>{
115+
// console.log(data);
116+
});
117+
});
104118

105119

106120

@@ -110,7 +124,7 @@ $(document).ready(()=>{
110124
// Setting up an array of notes to render after finished
111125
// Also setting up a currentNote constiable to temporarily store each note
112126
let notesToRender = [];
113-
let currentNote;
127+
let currentNote;
114128
if (!data.notes.length) {
115129
// If we have no notes, just display a message explaining this
116130
currentNote = $("<li class='list-group-item'>No notes for this article yet.</li>");

server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ app.get('/articles/:id', (req, res)=>{
206206

207207
// Route for saving/updating Article's associated comments
208208
app.post('/articles/:id', (req, res)=>{
209+
// console.log(req.body);
209210
db.Comment.create(req.body)
210211
.then((dbComment)=>{
211212
// If a Note was created successfully, find one Article with an `_id` equal to `req.params.id`.

views/saved.handlebars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
{{!-- Modal --}}
6969
<div class="modal fade" id="commentModal" tabindex="-1" role="dialog" aria-labelledby="commentModal" aria-hidden="true">
70-
<div class='container-fluid text-center'></div>
70+
<div class='container-fluid text-center' data-id="{{id}}"></div>
7171
{{!-- <div class="modal-dialog" role="document">
7272
<div class="modal-content">
7373
<div class="modal-header">

0 commit comments

Comments
 (0)