feat: 카드 오른쪽 위 X 버튼으로 링크 삭제
- DELETE /links/<id>: .env 의 DELETE_TOKEN 과 X-Delete-Token 헤더가 맞을 때만 삭제 - 처음 삭제할 때 관리 키를 묻고 브라우저에 기억, 틀리면 다시 묻는다 - 정적 파일에 버전 쿼리를 붙여 캐시된 옛 화면이 남지 않게 한다 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -52,7 +52,16 @@ function appendLinks(links) {
|
||||
col.className = "col-12 col-md-6 col-lg-4";
|
||||
|
||||
const card = document.createElement("div");
|
||||
card.className = "card h-100 shadow-sm";
|
||||
card.className = "card h-100 shadow-sm position-relative";
|
||||
|
||||
const deleteBtn = document.createElement("button");
|
||||
deleteBtn.type = "button";
|
||||
deleteBtn.className = "btn-close card-delete-btn";
|
||||
deleteBtn.setAttribute("aria-label", "삭제");
|
||||
deleteBtn.title = "삭제";
|
||||
deleteBtn.addEventListener("click", () =>
|
||||
deleteLink(link, col, deleteBtn)
|
||||
);
|
||||
|
||||
const image = document.createElement("img");
|
||||
image.className = "card-img-top";
|
||||
@@ -90,12 +99,73 @@ function appendLinks(links) {
|
||||
linkBtn.textContent = "원문 보기";
|
||||
|
||||
body.append(title, description, linkBtn);
|
||||
card.append(image, body);
|
||||
card.append(deleteBtn, image, body);
|
||||
col.appendChild(card);
|
||||
linkList.appendChild(col);
|
||||
});
|
||||
}
|
||||
|
||||
const DELETE_TOKEN_KEY = "newsLinkDeleteToken";
|
||||
|
||||
function getStoredToken() {
|
||||
try {
|
||||
return localStorage.getItem(DELETE_TOKEN_KEY) || "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function storeToken(token) {
|
||||
try {
|
||||
if (token) localStorage.setItem(DELETE_TOKEN_KEY, token);
|
||||
else localStorage.removeItem(DELETE_TOKEN_KEY);
|
||||
} catch {
|
||||
/* 저장 불가(사생활 보호 모드 등)면 매번 묻는다 */
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteLink(link, col, button) {
|
||||
const label = link.title || link.url;
|
||||
if (!window.confirm(`이 링크를 삭제할까요?\n\n${label}`)) return;
|
||||
|
||||
let token = getStoredToken();
|
||||
for (let attempt = 0; attempt < 2; attempt += 1) {
|
||||
if (!token) {
|
||||
token = (window.prompt("관리 키를 입력하세요.") || "").trim();
|
||||
if (!token) return;
|
||||
}
|
||||
button.disabled = true;
|
||||
try {
|
||||
const res = await fetch(`/links/${encodeURIComponent(link.id)}`, {
|
||||
method: "DELETE",
|
||||
headers: { "X-Delete-Token": token },
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (res.status === 401) {
|
||||
storeToken("");
|
||||
token = "";
|
||||
window.alert(data.error || "관리 키가 맞지 않습니다.");
|
||||
continue;
|
||||
}
|
||||
if (!res.ok && res.status !== 404) {
|
||||
throw new Error(data.error || "삭제하지 못했습니다.");
|
||||
}
|
||||
storeToken(token);
|
||||
col.remove();
|
||||
nextOffset = Math.max(0, nextOffset - 1);
|
||||
if (!linkList.children.length && !hasMore) {
|
||||
setEmptyStateVisible(true, "아직 저장된 링크가 없습니다.");
|
||||
}
|
||||
return;
|
||||
} catch (err) {
|
||||
window.alert(err.message);
|
||||
return;
|
||||
} finally {
|
||||
button.disabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const PAGE_SIZE = 30;
|
||||
let nextOffset = 0;
|
||||
let hasMore = true;
|
||||
|
||||
Reference in New Issue
Block a user