summaryrefslogtreecommitdiff
path: root/web/public/js
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-03-30 16:36:54 -0400
committerFreya Murphy <freya@freyacat.org>2024-03-30 16:36:54 -0400
commit1f647374a8cdf3bc5c2d29ff8be277b027925c8c (patch)
tree9fdf42d250edb941de13ecd1aab9185ba2b30b00 /web/public/js
parentrename views to _views (diff)
downloadxssbook2-1f647374a8cdf3bc5c2d29ff8be277b027925c8c.tar.gz
xssbook2-1f647374a8cdf3bc5c2d29ff8be277b027925c8c.tar.bz2
xssbook2-1f647374a8cdf3bc5c2d29ff8be277b027925c8c.zip
post comments, refactor post loading, hide load more btn
Diffstat (limited to 'web/public/js')
-rw-r--r--web/public/js/shared/post.js58
1 files changed, 52 insertions, 6 deletions
diff --git a/web/public/js/shared/post.js b/web/public/js/shared/post.js
index afbeaf0..f22dd99 100644
--- a/web/public/js/shared/post.js
+++ b/web/public/js/shared/post.js
@@ -5,14 +5,28 @@ observe('#main-content', '.action-load-comments', function(me) {
page = '1';
}
let newPage = Number(page) + 1;
- let id = me.attr('postId');
me.attr('page', newPage + '');
- let url = '/home/comments?page=' + page + '&id=' + id;
+
+ let postId = me.attr('postId');
+ let loaded = Number(me.attr('loaded'));
+ let pageSize = Number(me.attr('pageSize'));
+ let commmentCount = Number(me.attr('commentCount'));
+ let commentMax = Number(me.attr('commentMax'));
+
+ let url = '/_util/post/comments?page=' + page + '&id=' + postId + '&max' + commentMax;
$.get(url, function (data) {
if (data === '') {
me.remove();
+ return;
+ }
+
+ $(data).insertBefore(me);
+
+ loaded += pageSize;
+ if (loaded >= commmentCount) {
+ me.remove();
} else {
- $(me).prepend(data);
+ me.attr('loaded', loaded + '');
}
});
});
@@ -26,13 +40,45 @@ observe('#main-content', '#action-load-posts', function(me) {
}
let newPage = Number(page) + 1;
me.attr('page', newPage + '');
- let url = '/home/posts?page=' + page;
- $.get(url, function (data) {
+
+ let loaded = Number(me.attr('loaded'));
+ let pageSize = Number(me.attr('pageSize'));
+ let postCount = Number(me.attr('postCount'));
+ let postMax = Number(me.attr('postMax'));
+
+ let url = '/_util/post/posts?page=' + page + '&max=' + postMax;
+ $.get(url, function (data) {
if (data === '') {
me.remove();
+ return;
+ }
+
+ $(data).insertBefore(me);
+
+ loaded += pageSize;
+ if (loaded >= postCount) {
+ me.remove();
} else {
- $('#post-container').append(data);
+ me.attr('loaded', loaded + '');
}
});
});
});
+
+observe('#main-content', '.action-new-comment-form', function(me) {
+ me.on('submit', function(e) {
+ e.preventDefault();
+ let input = me.find('.action-new-comment');
+ let content = input.val();
+ let post_id = input.attr('postId');
+ $.ajax({
+ url: '/api/comment',
+ method: 'POST',
+ data: JSON.stringify({ post_id, content }),
+ success: function(_data) {
+ window.location.reload();
+ },
+ error: errorToast
+ });
+ });
+});