Skip to content

Commit

Permalink
feat: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Sep 14, 2024
1 parent a3e104c commit 84846b9
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 40 deletions.
25 changes: 1 addition & 24 deletions js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,31 +180,8 @@ export const admin = (() => {
const download = async (button) => {
const btn = util.disableButton(button);

const res = await request(HTTP_GET, '/api/download').token(session.getToken()).download();
if (!res) {
btn.restore();
return;
}

const data = await res.blob();
if (!data) {
btn.restore();
return;
}

const filename = res.headers.get('content-disposition') ?? 'download.csv';

const link = document.createElement('a');
const href = window.URL.createObjectURL(data);

link.href = href;
link.download = (filename.match(/(?<=")(?:\\.|[^"\\])*(?=")/) ?? ['download.csv'])[0];

document.body.appendChild(link);
link.click();
document.body.removeChild(link);
await request(HTTP_GET, '/api/download').token(session.getToken()).download();

window.URL.revokeObjectURL(href);
btn.restore();
};

Expand Down
11 changes: 4 additions & 7 deletions js/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const comment = (() => {

const status = await request(HTTP_DELETE, '/api/comment/' + owns.get(id))
.token(session.getToken())
.send()
.send(dto.statusResponse)
.then((res) => res.data.status, () => false);

if (!status) {
Expand Down Expand Up @@ -104,11 +104,8 @@ export const comment = (() => {

const status = await request(HTTP_PUT, '/api/comment/' + owns.get(id))
.token(session.getToken())
.body({
presence: presence ? isPresent : null,
comment: form.value
})
.send()
.body(dto.updateCommentRequest(presence ? isPresent : null, form.value))
.send(dto.statusResponse)
.then((res) => res.data.status, () => false);

form.disabled = false;
Expand Down Expand Up @@ -303,7 +300,7 @@ export const comment = (() => {

await request(HTTP_GET, '/api/comment/' + id)
.token(session.getToken())
.send()
.send(dto.commentResponse)
.then((res) => {
if (res.code !== 200) {
return;
Expand Down
41 changes: 41 additions & 0 deletions js/dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ export const dto = (() => {
};
});

const tokenResponse = (({ token }) => {
return {
token,
};
});

const uuidResponse = (({ uuid }) => {
return {
uuid,
};
});

const commentResponse = (({ name, presence, comment, is_admin, created_at }) => {
return {
name,
presence,
comment,
is_admin,
created_at,
};
});

const commentShowMore = ((uuid, show = false) => {
return {
uuid,
Expand All @@ -62,12 +84,31 @@ export const dto = (() => {
};
});

const postSessionRequest = ((email, password) => {
return {
email: email,
password: password,
};
});

const updateCommentRequest = ((presence, comment) => {
return {
presence: presence,
comment: comment
};
});

return {
uuidResponse,
baseResponse,
tokenResponse,
statusResponse,
commentResponse,
likeCommentResponse,
postCommentResponse,
commentShowMore,
postCommentRequest,
postSessionRequest,
updateCommentRequest,
};
})();
2 changes: 1 addition & 1 deletion js/like.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const like = (() => {

await request(HTTP_POST, '/api/comment/' + id)
.token(session.getToken())
.send()
.send(dto.uuidResponse)
.then((res) => {
if (res.code == 201) {
likes.set(id, res.data.uuid);
Expand Down
32 changes: 29 additions & 3 deletions js/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,37 @@ export const request = (method, path) => {
download() {
return fetch(url + path, req)
.then((res) => {
if (res.status === 200) {
return res;
if (res.status !== 200) {
return null;
}

return null;
const existingLink = document.querySelector('a[download]');
if (existingLink) {
document.body.removeChild(existingLink);
}

const filename = res.headers.get('content-disposition')?.match(/filename="(.+)"/)?.[1] || 'download.csv';
return res.blob().then((blob) => ({ blob, filename }));
})
.then((res) => {
if (!res) {
return null;
}

const { blob, filename } = res;

const link = document.createElement('a');
const href = window.URL.createObjectURL(blob);

link.href = href;
link.download = filename;
document.body.appendChild(link);

link.click();

document.body.removeChild(link);
window.URL.revokeObjectURL(href);

})
.catch((err) => {
alert(err);
Expand Down
8 changes: 3 additions & 5 deletions js/session.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dto } from './dto.js';
import { util } from './util.js';
import { admin } from './admin.js';
import { storage } from './storage.js';
Expand All @@ -22,11 +23,8 @@ export const session = (() => {
formPassword.disabled = true;

const res = await request(HTTP_POST, '/api/session')
.body({
email: formEmail.value,
password: formPassword.value
})
.send()
.body(dto.postSessionRequest(formEmail.value, formPassword.value))
.send(dto.tokenResponse)
.then((res) => {
if (res.code === 200) {
session.set('token', res.data.token);
Expand Down

0 comments on commit 84846b9

Please sign in to comment.