Skip to content

Commit

Permalink
feat: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Aug 29, 2024
1 parent bf5ba6c commit 818b28b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion js/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const card = (() => {
</div>
${renderTracker(comment)}
${renderButton(comment)}
${comment.comments.map((c) => renderInnerContent(c)).join('')}
<div id="reply-content-${comment.uuid}">${comment.comments.map((c) => renderInnerContent(c)).join('')}</div>
</div>`;
};

Expand Down
13 changes: 5 additions & 8 deletions js/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export const comment = (() => {
const response = await request(HTTP_POST, '/api/comment')
.token(session.get('token'))
.body(dto.postCommentRequest(id, nameValue, presence ? presence.value === "1" : true, form.value))
.send()
.send(dto.postCommentResponse)
.then((res) => res, () => null);

if (name) {
Expand Down Expand Up @@ -212,12 +212,10 @@ export const comment = (() => {
return;
}

const dtoPostComment = dto.postCommentResponse(response.data);
dtoPostComment.is_admin = session.get('token')?.split('.').length === 3;
const newComment = card.renderContent(dtoPostComment);
response.data.is_admin = session.get('token')?.split('.').length === 3;

document.getElementById('comments').lastElementChild.remove();
document.getElementById('comments').innerHTML = newComment + document.getElementById('comments').innerHTML;
document.getElementById('comments').innerHTML = card.renderContent(response.data) + document.getElementById('comments').innerHTML;
document.getElementById('comments').scrollIntoView({ behavior: 'smooth' });
}

Expand All @@ -228,9 +226,8 @@ export const comment = (() => {
changeButton(id, false);
document.getElementById(`inner-${id}`).remove();

const dtoPostComment = dto.postCommentResponse(response.data);
dtoPostComment.is_admin = session.get('token')?.split('.').length === 3;
document.getElementById(`${id}`).insertAdjacentHTML('beforeend', card.renderInnerContent(dtoPostComment));
response.data.is_admin = session.get('token')?.split('.').length === 3;
document.getElementById(`reply-content-${id}`).insertAdjacentHTML('beforeend', card.renderInnerContent(response.data));

const containerDiv = document.getElementById(`button-${id}`);
const anchorTag = containerDiv.querySelector('a');
Expand Down
19 changes: 18 additions & 1 deletion js/dto.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
export const dto = (() => {
/**
* Generates a base response object containing code, data, and error properties.
*
* @template T
* @param {number} code
* @param {T} data
* @param {string[]=} error
* @returns {{code: number, data: T, error?: string[]}} The response object containing the code, data, and error.
*/
const baseResponse = (code, data, error) => {
return {
code,
data,
error,
};
};

const likeCommentResponse = ((love = 0) => {
return {
love,
};
});


const postCommentResponse = (({ uuid, own, name, presence, comment, created_at }) => {
let is_admin;
let comments = [];
Expand Down Expand Up @@ -41,6 +57,7 @@ export const dto = (() => {
});

return {
baseResponse,
likeCommentResponse,
postCommentResponse,
commentShowMore,
Expand Down
15 changes: 13 additions & 2 deletions js/request.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { dto } from "./dto.js";

export const HTTP_GET = 'GET';
export const HTTP_POST = 'POST';
export const HTTP_PUT = 'PUT';
Expand All @@ -20,15 +22,24 @@ export const request = (method, path) => {
}

return {
send() {
/**
* @template T
* @param {((data: any) => T)=} transform
* @returns {Promise<ReturnType<typeof dto.baseResponse<T>>>}
*/
send(transform = null) {
return fetch(url + path, req)
.then((res) => res.json())
.then((res) => {
if (res.error) {
throw res.error[0];
}

return res;
if (transform) {
res.data = transform(res.data);
}

return dto.baseResponse(res.code, res.data, res.error);
})
.catch((err) => {
alert(err);
Expand Down

0 comments on commit 818b28b

Please sign in to comment.