diff --git a/js/comment.js b/js/comment.js index 47c886e..7690f96 100644 --- a/js/comment.js +++ b/js/comment.js @@ -26,7 +26,8 @@ export const comment = (() => { const status = await request(HTTP_DELETE, '/api/comment/' + owns.get(id)) .token(session.get('token')) - .then((res) => res.data.status); + .send() + .then((res) => res.data.status, () => false); if (!status) { btn.restore(); @@ -85,7 +86,8 @@ export const comment = (() => { presence: presence ? presence.value === "1" : null, comment: form.value }) - .then((res) => res.data.status); + .send() + .then((res) => res.data.status, () => false); form.disabled = false; if (cancel) { @@ -144,7 +146,8 @@ export const comment = (() => { presence: presence ? presence.value === "1" : true, comment: form.value }) - .then(); + .send() + .then((res) => res, () => null); if (name) { name.disabled = false; @@ -223,30 +226,31 @@ export const comment = (() => { const tmp = button.innerText; button.innerText = 'Loading..'; - const status = await request(HTTP_GET, '/api/comment/' + id) + await request(HTTP_GET, '/api/comment/' + id) .token(session.get('token')) - .then((res) => res); - - if (status?.code === 200) { - const inner = document.createElement('div'); - inner.classList.add('my-2'); - inner.id = `inner-${id}`; - inner.innerHTML = ` - - ${document.getElementById(id).getAttribute('data-parent') === 'true' ? ` - ` : ''} - -
- - -
`; - - document.getElementById(`button-${id}`).insertAdjacentElement('afterend', inner); - document.getElementById(`form-inner-${id}`).value = status.data.comment; - } + .send() + .then((res) => { + if (res.code === 200) { + const inner = document.createElement('div'); + inner.classList.add('my-2'); + inner.id = `inner-${id}`; + inner.innerHTML = ` + + ${document.getElementById(id).getAttribute('data-parent') === 'true' && session.get('token')?.split('.').length !== 3 ? ` + ` : ''} + +
+ + +
`; + + document.getElementById(`button-${id}`).insertAdjacentElement('afterend', inner); + document.getElementById(`form-inner-${id}`).value = res.data.comment; + } + }); button.innerText = tmp; }; @@ -256,8 +260,9 @@ export const comment = (() => { const comments = document.getElementById('comments'); const onNullComment = `
Yuk bagikan undangan ini biar banyak komentarnya
`; - await request(HTTP_GET, `/api/comment?per=${pagination.getPer()}&next=${pagination.getNext()}`) + return await request(HTTP_GET, `/api/comment?per=${pagination.getPer()}&next=${pagination.getNext()}`) .token(session.get('token')) + .send() .then((res) => { pagination.setResultData(res.data.length); diff --git a/js/like.js b/js/like.js index 9c609e8..640f0c9 100644 --- a/js/like.js +++ b/js/like.js @@ -18,6 +18,7 @@ export const like = (() => { if (likes.has(id)) { await request(HTTP_PATCH, '/api/comment/' + likes.get(id)) .token(session.get('token')) + .send() .then((res) => { if (res.data.status) { likes.unset(id); @@ -37,6 +38,7 @@ export const like = (() => { await request(HTTP_POST, '/api/comment/' + id) .token(session.get('token')) + .send() .then((res) => { if (res.code == 201) { likes.set(id, res.data.uuid); diff --git a/js/request.js b/js/request.js index 8829d6e..36ec340 100644 --- a/js/request.js +++ b/js/request.js @@ -20,7 +20,7 @@ export const request = (method, path) => { } return { - then(resolve = null, reject = null) { + send() { return fetch(url + path, req) .then((res) => res.json()) .then((res) => { @@ -30,13 +30,12 @@ export const request = (method, path) => { return res; }) - .then(resolve, reject) .catch((err) => { alert(err); throw err; }); }, - download(resolve = null, reject = null) { + download() { return fetch(url + path, req) .then((res) => { if (res.status === 200) { @@ -45,7 +44,6 @@ export const request = (method, path) => { return null; }) - .then(resolve, reject) .catch((err) => { alert(err); throw err; diff --git a/js/session.js b/js/session.js index a17b166..6ad40b8 100644 --- a/js/session.js +++ b/js/session.js @@ -23,13 +23,13 @@ export const session = (() => { email: formEmail.value, password: formPassword.value }) + .send() .then((res) => { if (res.code === 200) { session.set('token', res.data.token); } - - return res.code === 200; - }); + }) + .then((res) => res.code === 200, () => false); if (res) { bootstrap.Modal.getOrCreateInstance('#loginModal').hide(); diff --git a/js/user.js b/js/user.js index 7db1a13..ea483c5 100644 --- a/js/user.js +++ b/js/user.js @@ -8,7 +8,7 @@ export const user = (() => { const token = storage('session'); const getUserDetail = () => { - request(HTTP_GET, '/api/user').token(token.get('token')).then((res) => { + request(HTTP_GET, '/api/user').token(token.get('token')).send().then((res) => { for (let [key, value] of Object.entries(res.data)) { user.set(key, value); @@ -27,7 +27,7 @@ export const user = (() => { }; const getStatUser = () => { - request(HTTP_GET, '/api/stats').token(token.get('token')).then((res) => { + request(HTTP_GET, '/api/stats').token(token.get('token')).send().then((res) => { document.getElementById('count-comment').innerHTML = res.data.comments.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."); document.getElementById('count-like').innerHTML = res.data.likes.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."); document.getElementById('count-present').innerHTML = res.data.present.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."); @@ -71,7 +71,7 @@ export const user = (() => { body({ filter: Boolean(checkbox.checked) }). - then(); + send(); label.restore(); }; @@ -84,7 +84,7 @@ export const user = (() => { body({ can_reply: Boolean(checkbox.checked) }). - then(); + send(); label.restore(); }; @@ -97,7 +97,7 @@ export const user = (() => { body({ can_edit: Boolean(checkbox.checked) }). - then(); + send(); label.restore(); }; @@ -110,7 +110,7 @@ export const user = (() => { body({ can_delete: Boolean(checkbox.checked) }). - then(); + send(); label.restore(); }; @@ -124,6 +124,7 @@ export const user = (() => { await request(HTTP_PUT, '/api/key'). token(token.get('token')). + send(). then((res) => { if (res.data.status) { getUserDetail(); @@ -153,7 +154,8 @@ export const user = (() => { old_password: old.value, new_password: newest.value, }). - then((res) => res.data.status); + send(). + then((res) => res.data.status, () => false); btn.restore(); @@ -185,7 +187,8 @@ export const user = (() => { body({ name: name.value, }). - then((res) => res.data.status); + send(). + then((res) => res.data.status, () => false); name.disabled = false; diff --git a/js/util.js b/js/util.js index 404cc11..815d4ba 100644 --- a/js/util.js +++ b/js/util.js @@ -227,6 +227,7 @@ export const util = (() => { progress.add(); request(HTTP_GET, '/api/config') .token(token) + .send() .then((res) => { session.set('token', token); progress.complete('request');