diff options
| author | Freya Murphy <freya@freyacat.org> | 2024-04-01 11:09:25 -0400 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2024-04-01 11:09:25 -0400 |
| commit | 3a82baec9d793edf81ac2b151b0f4d4159641375 (patch) | |
| tree | f9d50c296b078ac48c2a2391c172c3ccf37edb3f /web/public/js | |
| parent | refactor asset dir, refactor oberver in lib (diff) | |
| download | xssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.tar.gz xssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.tar.bz2 xssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.zip | |
login and register, liking on homepage
Diffstat (limited to '')
| -rw-r--r-- | src/public/js/lib.js (renamed from web/public/js/lib.js) | 31 | ||||
| -rw-r--r-- | src/public/js/modal.js (renamed from web/public/js/modal.js) | 0 | ||||
| -rw-r--r-- | src/public/js/post.js (renamed from web/public/js/post.js) | 53 | ||||
| -rw-r--r-- | src/public/js/routes/home.js (renamed from web/public/js/routes/home.js) | 0 | ||||
| -rw-r--r-- | src/public/js/thirdparty/jquery.min.js (renamed from web/public/js/thirdparty/jquery.min.js) | 0 |
5 files changed, 74 insertions, 10 deletions
diff --git a/web/public/js/lib.js b/src/public/js/lib.js index 55b8161..edb7258 100644 --- a/web/public/js/lib.js +++ b/src/public/js/lib.js @@ -58,11 +58,19 @@ var $$ = (selector) => { /// ajax error handle /// -var errorToast = (xhr) => { +var errorToastAjax = (xhr) => { let data = xhr.responseJSON; - let msg = data.message; - let detail = data.details; - let hint = data.hint; + + let msg, detail, hint; + + if (data) { + msg = data.message; + detail = data.details; + hint = data.hint; + } else { + msg = 'api_unknown'; + } + let query = '?msg=' + msg; if (detail) { @@ -77,6 +85,13 @@ var errorToast = (xhr) => { }) } +var errorToast = (msg) => { + let url = '/template/toast?msg=' + msg; + $.get(url, function (data) { + $('#toast-container').prepend(data); + }) +} + $$('.action-close-toast').on('click', function() { $(this).parent().remove(); }); @@ -94,12 +109,14 @@ $$('.action-close-toast').each(function() { $.ajaxSetup({ headers: (() => { - let ajaxHeaders = {}; - ajaxHeaders['Content-Type'] = 'application/json'; + let ajaxHeaders = { + 'Content-Type': 'application/json', + 'Prefer': 'return=representation' + }; if (jwtStr) { ajaxHeaders['Authorization'] = 'Bearer ' + jwtStr } return ajaxHeaders; })(), - error: errorToast + error: errorToastAjax }) diff --git a/web/public/js/modal.js b/src/public/js/modal.js index 2a704f3..2a704f3 100644 --- a/web/public/js/modal.js +++ b/src/public/js/modal.js diff --git a/web/public/js/post.js b/src/public/js/post.js index 7e524bb..38bbb78 100644 --- a/web/public/js/post.js +++ b/src/public/js/post.js @@ -70,12 +70,59 @@ $$('.action-new-comment-form').on('submit', function(e) { let input = me.find('.action-new-comment'); let content = input.val(); let post_id = input.attr('postId'); + + const getComment = function(data) { + if (data) { + let container = me.closest('.post').find('.comments'); + container.prepend(data); + } + input.val(''); + } + + const onComment = function(data) { + let id = data[0].id; + $.get({ + url: '/_util/post/comment?id=' + id, + success: getComment + }); + } + $.ajax({ url: '/api/comment', method: 'POST', data: JSON.stringify({ post_id, content }), - success: function(_data) { - window.location.reload(); - }, + success: onComment }); }); + +$$('.action-like').on('click', function() { + let me = $(this); + let liked = me.hasClass('btn-blue'); + let like_id = me.attr('likeId'); + let post_id = me.attr('postId'); + + const onPatch = () => { + me.toggleClass('btn-blue'); + } + + const onPost = (data) => { + me.attr('likeId', data[0].id + ''); + me.toggleClass('btn-blue'); + } + + if (like_id) { + $.ajax({ + url: '/api/like?id=eq.' + like_id, + method: 'PATCH', + data: JSON.stringify({ value: !liked }), + success: onPatch + }); + } else { + $.ajax({ + url: '/api/like', + method: 'POST', + data: JSON.stringify({ post_id, value: true }), + success: onPost, + }); + } +}); diff --git a/web/public/js/routes/home.js b/src/public/js/routes/home.js index e69de29..e69de29 100644 --- a/web/public/js/routes/home.js +++ b/src/public/js/routes/home.js diff --git a/web/public/js/thirdparty/jquery.min.js b/src/public/js/thirdparty/jquery.min.js index 7f37b5d..7f37b5d 100644 --- a/web/public/js/thirdparty/jquery.min.js +++ b/src/public/js/thirdparty/jquery.min.js |