Skip to content

Commit 3a1ca1d

Browse files
committed
refactor: eslint config
1 parent 4d53bdf commit 3a1ca1d

File tree

6 files changed

+169
-156
lines changed

6 files changed

+169
-156
lines changed

eslint.config.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ export default [
2525
"no-unreachable": "error",
2626
"no-var": "error",
2727
"no-constant-condition": "error",
28+
"prefer-const": "error",
29+
"no-use-before-define": "error",
30+
"no-duplicate-imports": "error",
31+
"no-template-curly-in-string": "error",
32+
"require-await": "error",
33+
"no-else-return": "error",
34+
"no-floating-decimal": "error",
35+
"no-nested-ternary": "warn",
2836
},
2937
},
3038
];

js/app/component/comment.js

+145-145
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,147 @@ export const comment = (() => {
6666
bodyLike.addEventListener('touchend', () => like.tapTap(bodyLike));
6767
};
6868

69+
/**
70+
* @param {ReturnType<typeof dto.getCommentResponse>} c
71+
* @returns {void}
72+
*/
73+
const fetchTracker = (c) => {
74+
if (!session.isAdmin()) {
75+
return;
76+
}
77+
78+
if (c.comments) {
79+
c.comments.forEach(fetchTracker);
80+
}
81+
82+
if (c.ip === undefined || c.user_agent === undefined || c.is_admin || tracker.has(c.ip)) {
83+
return;
84+
}
85+
86+
/**
87+
* @param {string} uuid
88+
* @param {string} ip
89+
* @param {string} result
90+
* @returns {void}
91+
*/
92+
const setResult = (uuid, ip, result) => {
93+
document.getElementById(`ip-${uuid}`).innerHTML = `<i class="fa-solid fa-location-dot me-1"></i>${util.escapeHtml(ip)} <strong>${util.escapeHtml(result)}</strong>`;
94+
};
95+
96+
request(HTTP_GET, `https://freeipapi.com/api/json/${c.ip}`)
97+
.default()
98+
.then((res) => res.json())
99+
.then((res) => {
100+
let result = res.cityName + ' - ' + res.regionName;
101+
102+
if (res.cityName === '-' && res.regionName === '-') {
103+
result = 'localhost';
104+
}
105+
106+
tracker.set(c.ip, result);
107+
setResult(c.uuid, c.ip, result);
108+
})
109+
.catch((err) => setResult(c.uuid, c.ip, err.message));
110+
};
111+
112+
/**
113+
* @param {ReturnType<typeof dto.getCommentsResponse>} items
114+
* @param {ReturnType<typeof dto.commentShowMore>[]} hide
115+
* @returns {ReturnType<typeof dto.commentShowMore>[]}
116+
*/
117+
const traverse = (items, hide = []) => {
118+
items.forEach((item) => {
119+
if (!hide.find((i) => i.uuid === item.uuid)) {
120+
hide.push(dto.commentShowMore(item.uuid));
121+
}
122+
123+
if (item.comments && item.comments.length > 0) {
124+
traverse(item.comments, hide);
125+
}
126+
});
127+
128+
return hide;
129+
};
130+
131+
/**
132+
* @returns {Promise<ReturnType<typeof dto.getCommentsResponse>>}
133+
*/
134+
const show = () => {
135+
const comments = document.getElementById('comments');
136+
137+
if (comments.getAttribute('data-loading') === 'false') {
138+
comments.setAttribute('data-loading', 'true');
139+
comments.innerHTML = card.renderLoading().repeat(pagination.getPer());
140+
}
141+
142+
return request(HTTP_GET, `/api/comment?per=${pagination.getPer()}&next=${pagination.getNext()}`)
143+
.token(session.getToken())
144+
.send(dto.getCommentsResponse)
145+
.then(async (res) => {
146+
const commentLength = res.data.length;
147+
comments.setAttribute('data-loading', 'false');
148+
lastRender.map((i) => i.uuid).forEach((u) => gif.remove(u));
149+
150+
if (commentLength === 0) {
151+
pagination.setResultData(commentLength);
152+
comments.innerHTML = onNullComment();
153+
return res;
154+
}
155+
156+
lastRender = traverse(res.data);
157+
showHide.set('hidden', traverse(res.data, showHide.get('hidden')));
158+
159+
let data = '';
160+
for (const i of res.data) {
161+
data += await card.renderContent(i);
162+
}
163+
comments.innerHTML = data;
164+
165+
res.data.forEach(fetchTracker);
166+
res.data.forEach(addListenerLike);
167+
168+
pagination.setResultData(commentLength);
169+
comments.dispatchEvent(new Event('comment.result'));
170+
171+
return res;
172+
});
173+
};
174+
175+
/**
176+
* @param {HTMLButtonElement} button
177+
* @returns {void}
178+
*/
179+
const showOrHide = (button) => {
180+
const ids = button.getAttribute('data-uuids').split(',');
181+
const isShow = button.getAttribute('data-show') === 'true';
182+
const uuid = button.getAttribute('data-uuid');
183+
184+
if (isShow) {
185+
button.setAttribute('data-show', 'false');
186+
button.innerText = `Show replies (${ids.length})`;
187+
188+
showHide.set('show', showHide.get('show').filter((i) => i !== uuid));
189+
} else {
190+
button.setAttribute('data-show', 'true');
191+
button.innerText = 'Hide replies';
192+
193+
showHide.set('show', showHide.get('show').concat([uuid]));
194+
}
195+
196+
for (const id of ids) {
197+
showHide.set('hidden', showHide.get('hidden').map((i) => {
198+
if (i.uuid === id) {
199+
i.show = !isShow;
200+
}
201+
202+
return i;
203+
}));
204+
205+
const cls = document.getElementById(id).classList;
206+
isShow ? cls.add('d-none') : cls.remove('d-none');
207+
}
208+
};
209+
69210
/**
70211
* @param {HTMLButtonElement} button
71212
* @returns {Promise<void>}
@@ -198,18 +339,18 @@ export const comment = (() => {
198339
document.getElementById(`inner-${id}`).remove();
199340

200341
if (!gifIsOpen) {
201-
const show = document.querySelector(`[onclick="undangan.comment.showMore(this, '${id}')"]`);
342+
const showButton = document.querySelector(`[onclick="undangan.comment.showMore(this, '${id}')"]`);
202343
const original = card.convertMarkdownToHTML(util.escapeHtml(form.value));
203344
const content = document.getElementById(`content-${id}`);
204345

205346
if (original.length > card.maxCommentLength) {
206-
content.innerHTML = show?.getAttribute('data-show') === 'false' ? original.slice(0, card.maxCommentLength) + '...' : original;
347+
content.innerHTML = showButton?.getAttribute('data-show') === 'false' ? original.slice(0, card.maxCommentLength) + '...' : original;
207348
content.setAttribute('data-comment', util.base64Encode(original));
208-
show?.classList.replace('d-none', 'd-block');
349+
showButton?.classList.replace('d-none', 'd-block');
209350
} else {
210351
content.innerHTML = original;
211352
content.removeAttribute('data-comment');
212-
show?.classList.replace('d-block', 'd-none');
353+
showButton?.classList.replace('d-block', 'd-none');
213354
}
214355
}
215356

@@ -498,104 +639,6 @@ export const comment = (() => {
498639
button.disabled = true;
499640
};
500641

501-
/**
502-
* @param {ReturnType<typeof dto.getCommentsResponse>} items
503-
* @param {ReturnType<typeof dto.commentShowMore>[]} hide
504-
* @returns {ReturnType<typeof dto.commentShowMore>[]}
505-
*/
506-
const traverse = (items, hide = []) => {
507-
items.forEach((item) => {
508-
if (!hide.find((i) => i.uuid === item.uuid)) {
509-
hide.push(dto.commentShowMore(item.uuid));
510-
}
511-
512-
if (item.comments && item.comments.length > 0) {
513-
traverse(item.comments, hide);
514-
}
515-
});
516-
517-
return hide;
518-
};
519-
520-
/**
521-
* @returns {Promise<ReturnType<typeof dto.getCommentsResponse>>}
522-
*/
523-
const show = () => {
524-
const comments = document.getElementById('comments');
525-
526-
if (comments.getAttribute('data-loading') === 'false') {
527-
comments.setAttribute('data-loading', 'true');
528-
comments.innerHTML = card.renderLoading().repeat(pagination.getPer());
529-
}
530-
531-
return request(HTTP_GET, `/api/comment?per=${pagination.getPer()}&next=${pagination.getNext()}`)
532-
.token(session.getToken())
533-
.send(dto.getCommentsResponse)
534-
.then(async (res) => {
535-
const commentLength = res.data.length;
536-
comments.setAttribute('data-loading', 'false');
537-
lastRender.map((i) => i.uuid).forEach((u) => gif.remove(u));
538-
539-
if (commentLength === 0) {
540-
pagination.setResultData(commentLength);
541-
comments.innerHTML = onNullComment();
542-
return res;
543-
}
544-
545-
lastRender = traverse(res.data);
546-
showHide.set('hidden', traverse(res.data, showHide.get('hidden')));
547-
548-
let data = '';
549-
for (const i of res.data) {
550-
data += await card.renderContent(i);
551-
}
552-
comments.innerHTML = data;
553-
554-
res.data.forEach(fetchTracker);
555-
res.data.forEach(addListenerLike);
556-
557-
pagination.setResultData(commentLength);
558-
comments.dispatchEvent(new Event('comment.result'));
559-
560-
return res;
561-
});
562-
};
563-
564-
/**
565-
* @param {HTMLButtonElement} button
566-
* @returns {void}
567-
*/
568-
const showOrHide = (button) => {
569-
const ids = button.getAttribute('data-uuids').split(',');
570-
const isShow = button.getAttribute('data-show') === 'true';
571-
const uuid = button.getAttribute('data-uuid');
572-
573-
if (isShow) {
574-
button.setAttribute('data-show', 'false');
575-
button.innerText = `Show replies (${ids.length})`;
576-
577-
showHide.set('show', showHide.get('show').filter((i) => i !== uuid));
578-
} else {
579-
button.setAttribute('data-show', 'true');
580-
button.innerText = 'Hide replies';
581-
582-
showHide.set('show', showHide.get('show').concat([uuid]));
583-
}
584-
585-
for (const id of ids) {
586-
showHide.set('hidden', showHide.get('hidden').map((i) => {
587-
if (i.uuid === id) {
588-
i.show = !isShow;
589-
}
590-
591-
return i;
592-
}));
593-
594-
const cls = document.getElementById(id).classList;
595-
isShow ? cls.add('d-none') : cls.remove('d-none');
596-
}
597-
};
598-
599642
/**
600643
* @param {HTMLAnchorElement} anchor
601644
* @param {string} uuid
@@ -611,49 +654,6 @@ export const comment = (() => {
611654
anchor.setAttribute('data-show', isCollapsed ? 'true' : 'false');
612655
};
613656

614-
/**
615-
* @param {ReturnType<typeof dto.getCommentResponse>} c
616-
* @returns {void}
617-
*/
618-
const fetchTracker = (c) => {
619-
if (!session.isAdmin()) {
620-
return;
621-
}
622-
623-
if (c.comments) {
624-
c.comments.forEach(fetchTracker);
625-
}
626-
627-
if (c.ip === undefined || c.user_agent === undefined || c.is_admin || tracker.has(c.ip)) {
628-
return;
629-
}
630-
631-
/**
632-
* @param {string} uuid
633-
* @param {string} ip
634-
* @param {string} result
635-
* @returns {void}
636-
*/
637-
const setResult = (uuid, ip, result) => {
638-
document.getElementById(`ip-${uuid}`).innerHTML = `<i class="fa-solid fa-location-dot me-1"></i>${util.escapeHtml(ip)} <strong>${util.escapeHtml(result)}</strong>`;
639-
};
640-
641-
request(HTTP_GET, `https://freeipapi.com/api/json/${c.ip}`)
642-
.default()
643-
.then((res) => res.json())
644-
.then((res) => {
645-
let result = res.cityName + ' - ' + res.regionName;
646-
647-
if (res.cityName === '-' && res.regionName === '-') {
648-
result = 'localhost';
649-
}
650-
651-
tracker.set(c.ip, result);
652-
setResult(c.uuid, c.ip, result);
653-
})
654-
.catch((err) => setResult(c.uuid, c.ip, err.message));
655-
};
656-
657657
/**
658658
* @returns {void}
659659
*/

0 commit comments

Comments
 (0)