1
1
let bloggit_data = null
2
2
3
- const current_url = window . location . href
4
- const url_parameters = window . location . search ;
5
- const post_id = new URLSearchParams ( url_parameters ) . get ( "id" ) ;
6
- const bloggit_header = document . getElementById ( "bloggit-header" )
3
+ const current_url = window . location . href
4
+
5
+ const url_parameters = window . location . search ;
6
+
7
+ const post_id = new URLSearchParams ( url_parameters ) . get ( "id" ) ;
8
+
9
+ const bloggit_header = document . getElementById ( "bloggit-header" )
7
10
const bloggit_preloader = document . getElementById ( "bloggit-preloader" )
8
11
const bloggit = document . getElementById ( "bloggit-container" )
12
+
13
+ /* Added Code */
14
+ const comment_container = document . getElementById ( "bloggit-comment-container" )
15
+ bloggit_preloader . style . cssText = `
16
+ position: fixed;
17
+ width: 100vw;
18
+ height: 100vh;
19
+ z-index: 1000;
20
+ display: flex;
21
+ align-items: center;
22
+ justify-content: center;
23
+ background-color: transparent;
24
+ ` ;
25
+ /* end*/
9
26
bloggit_header . innerHTML = `
10
27
<nav class="navbar navbar-expand-lg navbar-light bg-light">
11
- <a class="navbar-brand" href="#">Craennie </a>
28
+ <a class="navbar-brand" href="#">Crane </a>
12
29
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
13
30
<span class="navbar-toggler-icon"></span>
14
31
</button>
@@ -17,17 +34,23 @@ const bloggit = document.getElementById("bloggit-container")
17
34
18
35
19
36
<li class="nav-item">
20
- <a class="nav-link" href="/home">Home </a>
37
+ <a class="nav-link" href="b">a </a>
21
38
</li>
22
39
23
40
</ul>
24
41
</div>
25
42
</nav>`
26
43
27
-
28
- preloader = ` <div id="preloader" class="spinner-border text-primary" role="status">
44
+ preloader = ` <div class="spinner-grow text-danger" role="status">
45
+ <span class="sr-only">Loading...</span>
46
+ </div>
47
+ <div class="spinner-grow text-warning" role="status">
29
48
<span class="sr-only">Loading...</span>
30
- </div>`
49
+ </div>
50
+ <div class="spinner-grow text-info" role="status">
51
+ <span class="sr-only">Loading...</span>
52
+ </div>`
53
+
31
54
bloggit_preloader . innerHTML = preloader
32
55
33
56
function convert_datetime ( param ) {
@@ -57,6 +80,74 @@ const bloggit = document.getElementById("bloggit-container")
57
80
return param
58
81
}
59
82
83
+ }
84
+ function render_comment_form ( ) {
85
+ const comment_form_container = document . getElementById ( "bloggit-comment-rf" )
86
+
87
+ if ( comment_form_container ) {
88
+ comment_form_container . innerHTML = `
89
+ <div class="container mt-5">
90
+ <h1>Leave a Comment</h1>
91
+ <div class="form-group">
92
+ <label for="comment-name">Name:* (required)</label>
93
+ <input type="text" class="form-control" id="comment-name" placeholder="Enter your name" >
94
+ </div>
95
+ <div class="form-group">
96
+ <label for="comment-email">Email: (not compulsory, and this won't be published)</label>
97
+ <input type="email" class="form-control" id="comment-email" placeholder="Enter your email">
98
+ </div>
99
+ <div class="form-group">
100
+ <label for="comment-comment">Comment:* (required)</label>
101
+ <textarea class="form-control" id="comment-comment" rows="4" placeholder="Enter your comment"></textarea>
102
+ </div>
103
+ <p id="comment-status"></p>
104
+ <button class="btn btn-primary" id="submit-comment" onclick = "BloggitSendComment()">Submit</button>
105
+ <br /> <br/>
106
+
107
+ </div>
108
+ `
109
+ } else {
110
+ console . log ( "Comment Form Container not defined!" )
111
+ }
112
+ }
113
+
114
+ function render_comments ( data ) {
115
+ if ( comment_container ) {
116
+ comment_container . classList += "container mt4"
117
+
118
+ if ( comment_container ) {
119
+ comment_container . innerHTML = `<h2>Comments</h2>
120
+ <div id="comments" class="mt-4">
121
+
122
+ </div>
123
+ `
124
+ const c_cont_inner = document . querySelector ( "#bloggit-comment-container #comments" )
125
+ if ( data [ "comments" ] != [ ] || data [ "comments" ] ) {
126
+ data [ "comments" ] . forEach ( function ( parameter ) {
127
+ let name = parameter . name
128
+ let comment = parameter . comment
129
+ let time = convert_datetime ( parameter . time )
130
+
131
+ c_cont_inner . innerHTML += `
132
+ <!-- Comments Display -->
133
+
134
+ <!-- Comments will be displayed here -->
135
+ <div class="card mb-3">
136
+ <div class="card-body">
137
+ <h3 class="card-title">${ name } </h3>
138
+ <article class="card-text">${ comment } </article>
139
+ <time>${ time } </time>
140
+ </div>
141
+ </div>
142
+
143
+ `
144
+ } )
145
+ }
146
+ }
147
+ } else {
148
+ console . log ( 'Comment Container not defined' )
149
+ }
150
+
60
151
}
61
152
62
153
function strip_tags ( param ) {
@@ -77,7 +168,7 @@ const bloggit = document.getElementById("bloggit-container")
77
168
{
78
169
"headers" : {
79
170
80
- "Authorization" : "Token a954b21e51c809b72b5e68d5a12717ef4e2c5d3a "
171
+ "Authorization" : "Token 420a3f9dbc5eaf8dfe3b5c22ea2f630385a9f4cc "
81
172
82
173
}
83
174
}
@@ -157,11 +248,89 @@ const bloggit = document.getElementById("bloggit-container")
157
248
<div class="mt-4">
158
249
${ post . body }
159
250
</div>`
251
+
252
+ render_comments ( data )
253
+ render_comment_form ( )
160
254
}
255
+ try {
256
+ bloggit_preloader . style . display = "none"
257
+ } catch ( error ) {
258
+
259
+ }
161
260
} )
162
261
163
262
. catch ( ( error ) => {
164
263
console . log ( error )
165
264
} )
166
265
167
-
266
+
267
+
268
+ function BloggitSendComment ( ) {
269
+
270
+ let comment = document . getElementById ( "comment-comment" )
271
+ let comment_name = document . getElementById ( "comment-name" )
272
+ let comment_email = document . getElementById ( "comment-email" )
273
+
274
+ if ( ! comment . value || ! comment_name . value ) {
275
+ document . getElementById ( "comment-status" ) . innerText = 'Name or Comment fields can\'t be empty!'
276
+ setTimeout ( ( ) => {
277
+ document . getElementById ( "comment-status" ) . innerText = ''
278
+ } , 4000 ) ;
279
+
280
+ return
281
+ }
282
+ data = {
283
+ "comment" : comment . value ,
284
+ "name" : comment_name . value ,
285
+ "email" : comment_email . value
286
+ }
287
+ fetch ( "http://127.0.0.1:8000/commentv2/" + new URLSearchParams ( url_parameters ) . get ( "id" ) , {
288
+ body : JSON . stringify ( data ) ,
289
+ method : "POST" ,
290
+ headers : {
291
+ 'Content-Type' : "application/json" ,
292
+ "Authorization" : `Token 420a3f9dbc5eaf8dfe3b5c22ea2f630385a9f4cc`
293
+ }
294
+ } )
295
+ . then ( response => {
296
+ if ( response . status == '201' ) {
297
+ const datetime = new Date ( )
298
+ const date = datetime . toLocaleDateString ( ) . replaceAll ( "/" , "-" ) + " " + datetime . toLocaleTimeString ( )
299
+ document . getElementById ( "comment-status" ) . innerText = 'Your comment has been added!'
300
+ setTimeout ( ( ) => {
301
+ document . getElementById ( "comment-status" ) . innerText = ''
302
+ } , 4000 ) ;
303
+
304
+ if ( document . querySelector ( "#bloggit-comment-container #comments" ) ) {
305
+ const c_cont_inner = document . querySelector ( "#bloggit-comment-container #comments" )
306
+ c_cont_inner . innerHTML += `
307
+ <!-- Comments Display -->
308
+
309
+ <!-- Comments will be displayed here -->
310
+ <div class="card mb-3">
311
+ <div class="card-body">
312
+ <h3 class="card-title">${ comment_name . value } </h3>
313
+ <article class="card-text">${ comment . value } </article>
314
+ <time>${ date } </time>
315
+ </div>
316
+ </div>
317
+
318
+ `
319
+ }
320
+ comment . value = comment_email . value = comment_name . value = ""
321
+ }
322
+ return response . json ( ) ;
323
+
324
+ } )
325
+ . then ( data => {
326
+ if ( data [ "email" ] ) {
327
+ document . getElementById ( "comment-status" ) . innerText = data [ "email" ] [ 0 ]
328
+ setTimeout ( ( ) => {
329
+ document . getElementById ( "comment-status" ) . innerText = ''
330
+ } , 4000 ) ;
331
+ }
332
+ } )
333
+ . catch ( error => {
334
+ console . error ( error ) ;
335
+ } ) ;
336
+ }
0 commit comments