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,39 +59,55 @@ server.all('/comment', function get(req, res) {
let id = req.query.id let id = req.query.id
let comment = req.query.comment let comment = req.query.comment
if(posts[id]) { if(posts[id]) {
const fake = req.body.fake === 'yes'
let toRemove = null
let cid = '' let cid = ''
function recurse(post) { try {
console.log(String(cid) + ' ' + comment) function recurse(post) {
if(String(cid) === comment) { console.log(String(cid) + ' ' + comment)
if(req.body.name && req.body.title && req.body.content) { if(String(cid) === comment) {
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(req.body.name && req.body.title && req.body.content) {
res.redirect(`/post/${id}`) post.comments.push({timestamp: new Date().getTime(), author: req.body.name, title: req.body.title, content: req.body.content.replaceAll('\r\n', '\n'), comments: []})
cid = -1 if(fake) {
} res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake})
return true toRemove = post
} } else
for(let i = 0; i < post.comments.length; i++) { res.redirect(`/post/${id}`)
const pid = cid cid = -1
cid += i + '_' }
if(recurse(post.comments[i]))
return true return true
cid = pid }
for(let i = 0; i < post.comments.length; i++) {
const pid = cid
cid += i + '_'
if(recurse(post.comments[i]))
return true
cid = pid
}
return false
} }
return false const f = recurse(posts[id]);
} if(f && cid == -1)
const f = recurse(posts[id]); return
if(f && cid == -1) if(f) {
return res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake})
if(f) { }
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid}) else {
} if(req.body.name && req.body.title && req.body.content) {
else { 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(req.body.name && req.body.title && req.body.content) { if(fake) {
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: []}) res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: cid, fake: fake})
res.redirect(`/post/${id}`) 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
res.render('post.ejs', {post: posts[id], postid: id, webname: webname, email: email, comment: ''})
} }
} }
} }

View file

@ -125,8 +125,8 @@
ret += `<a href="/post/${postid}">[Nevermind]</a>` ret += `<a href="/post/${postid}">[Nevermind]</a>`
ret += ret +=
`</div></div><post><div class="content"><form method="post"><label>` + `</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>` + `<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></textarea><br/><br/><button type="submit">Submit</button>` + `<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>` `</label></form></div></post><div>`
} }
else else