Skip to content

Commit

Permalink
fix: request method
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Aug 25, 2024
1 parent 92a8887 commit a3c3008
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 42 deletions.
59 changes: 32 additions & 27 deletions js/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = `
<label for="form-inner-${id}" class="form-label">Edit</label>
${document.getElementById(id).getAttribute('data-parent') === 'true' ? `
<select class="form-select shadow-sm mb-2 rounded-4" id="form-inner-presence-${id}">
<option value="1" ${status.data.presence ? 'selected' : ''}>Datang</option>
<option value="2" ${status.data.presence ? '' : 'selected'}>Berhalangan</option>
</select>` : ''}
<textarea class="form-control shadow-sm rounded-4 mb-2" id="form-inner-${id}" placeholder="Type update comment"></textarea>
<div class="d-flex flex-wrap justify-content-end align-items-center mb-0">
<button style="font-size: 0.8rem;" onclick="comment.cancel('${id}')" class="btn btn-sm btn-outline-${theme.isDarkMode('light', 'dark')} rounded-4 py-0 me-1">Cancel</button>
<button style="font-size: 0.8rem;" onclick="comment.update(this)" data-uuid="${id}" class="btn btn-sm btn-outline-${theme.isDarkMode('light', 'dark')} rounded-4 py-0">Update</button>
</div>`;

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 = `
<label for="form-inner-${id}" class="form-label">Edit</label>
${document.getElementById(id).getAttribute('data-parent') === 'true' && session.get('token')?.split('.').length !== 3 ? `
<select class="form-select shadow-sm mb-2 rounded-4" id="form-inner-presence-${id}">
<option value="1" ${res.data.presence ? 'selected' : ''}>Datang</option>
<option value="2" ${res.data.presence ? '' : 'selected'}>Berhalangan</option>
</select>` : ''}
<textarea class="form-control shadow-sm rounded-4 mb-2" id="form-inner-${id}" placeholder="Type update comment"></textarea>
<div class="d-flex flex-wrap justify-content-end align-items-center mb-0">
<button style="font-size: 0.8rem;" onclick="comment.cancel('${id}')" class="btn btn-sm btn-outline-${theme.isDarkMode('light', 'dark')} rounded-4 py-0 me-1">Cancel</button>
<button style="font-size: 0.8rem;" onclick="comment.update(this)" data-uuid="${id}" class="btn btn-sm btn-outline-${theme.isDarkMode('light', 'dark')} rounded-4 py-0">Update</button>
</div>`;

document.getElementById(`button-${id}`).insertAdjacentElement('afterend', inner);
document.getElementById(`form-inner-${id}`).value = res.data.comment;
}
});

button.innerText = tmp;
};
Expand All @@ -256,8 +260,9 @@ export const comment = (() => {
const comments = document.getElementById('comments');
const onNullComment = `<div class="h6 text-center fw-bold p-4 my-3 bg-theme-${theme.isDarkMode('dark', 'light')} rounded-4 shadow">Yuk bagikan undangan ini biar banyak komentarnya</div>`;

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);

Expand Down
2 changes: 2 additions & 0 deletions js/like.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions js/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) {
Expand All @@ -45,7 +44,6 @@ export const request = (method, path) => {

return null;
})
.then(resolve, reject)
.catch((err) => {
alert(err);
throw err;
Expand Down
6 changes: 3 additions & 3 deletions js/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 11 additions & 8 deletions js/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, ".");
Expand Down Expand Up @@ -71,7 +71,7 @@ export const user = (() => {
body({
filter: Boolean(checkbox.checked)
}).
then();
send();

label.restore();
};
Expand All @@ -84,7 +84,7 @@ export const user = (() => {
body({
can_reply: Boolean(checkbox.checked)
}).
then();
send();

label.restore();
};
Expand All @@ -97,7 +97,7 @@ export const user = (() => {
body({
can_edit: Boolean(checkbox.checked)
}).
then();
send();

label.restore();
};
Expand All @@ -110,7 +110,7 @@ export const user = (() => {
body({
can_delete: Boolean(checkbox.checked)
}).
then();
send();

label.restore();
};
Expand All @@ -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();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit a3c3008

Please sign in to comment.