Add preview function

This commit is contained in:
Daniella / Tove 2022-01-23 06:18:51 +01:00
parent 59aa77238f
commit 9df50a426d
2 changed files with 47 additions and 31 deletions

View file

@ -59,12 +59,19 @@ server.all('/comment', function get(req, res) {
let id = req.query.id
let comment = req.query.comment
if(posts[id]) {
const fake = req.body.fake === 'yes'
let toRemove = null
let cid = ''
try {
function recurse(post) {
console.log(String(cid) + ' ' + comment)
if(String(cid) === comment) {
if(req.body.name && req.body.title && req.body.content) {
post.comments.push({timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []})
if(fake) {
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake})
toRemove = post
} else
res.redirect(`/post/${id}`)
cid = -1
}
@ -83,16 +90,25 @@ server.all('/comment', function get(req, res) {
if(f && cid == -1)
return
if(f) {
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid})
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake})
}
else {
if(req.body.name && req.body.title && req.body.content) {
posts[id].comments.push({timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []})
if(fake) {
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake})
toRemove = posts[id]
} else
res.redirect(`/post/${id}`)
}
else
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: ''})
}
} finally {
if(fake) {
toRemove.comments.pop()
}
}
}
}
else

View file

@ -125,8 +125,8 @@
ret += `<a href="/post/${postid}">[Nevermind]</a>`
ret +=
`</div></div><post><div class="content"><form method="post"><label>` +
`<h3><input type="username" name="name" value="Anonymous" required>: <input type="text" name="title" placeholder="Title" required></h3>` +
`<textarea name="content" placeholder="So basically, ..." required></textarea><br/><br/><button type="submit">Submit</button>` +
`<h3><input type="username" name="name" value="Anonymous" ${fake ? `value="${post.comments[post.comments.length - 1].author.replaceAll("\"", "&quot;")}"` : ''} required>: <input type="text" name="title" placeholder="Title" ${fake ? `value="${post.comments[post.comments.length - 1].title.replaceAll("\"", "&quot;")}"` : ''} required></h3>` +
`<textarea name="content" placeholder="So basically, ..." required>${fake ? post.comments[post.comments.length - 1].content.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;") : ''}</textarea><br/><br/><button type="submit" name="fake" value="no">Submit</button><button type="submit" name="fake" value="yes">Preview</button>` +
`</label></form></div></post><div>`
}
else