diff options
Diffstat (limited to 'web/public')
-rw-r--r-- | web/public/css/post.css | 4 | ||||
-rw-r--r-- | web/public/js/shared/post.js | 58 |
2 files changed, 56 insertions, 6 deletions
diff --git a/web/public/css/post.css b/web/public/css/post.css index 6ad14ba..4030da3 100644 --- a/web/public/css/post.css +++ b/web/public/css/post.css @@ -10,3 +10,7 @@ .action-load-comments { margin-left: 4rem; } + +#action-load-posts { + justify-content: center; +} 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 + }); + }); +}); |