
๋๋ ๋ ธ์ ํด๋ก๋ ํ๋ก์ ํธ์์ ์๋ํฐ ๋ถ๋ถ์ ๋งก์๋ค. ํ์๋ถ์ด API ๋ชจ๋์ ๋ง๋ค์ด ์ฃผ์ ์ ๊ทธ๊ฑธ ์ด์ฉํด ๋ดค๋๋ฐ ์ฝ๋๊ฐ ํจ์ฌ ๊น๋ํด์ก๋ค! ๋ชจ๋์ด ์๋ฐ ๊ฑฐ๊ตฌ๋ ๋ฐฐ์ ์
import { request } from "../../api/api.js";
const deleteBtn = document.querySelector(".deleteBtn");
const deletePost = (e) => {
e.preventDefault();
// documentId ๋ฐ์์ค๊ธฐ
const currentUrl = window.location.href;
const documentId = currentUrl.split("/").pop();
// ํด๋น id ์ญ์
request({ method: "DELETE" }, documentId);
};
deleteBtn.addEventListener("click", deletePost);
import { request } from "../../api/api.js";
const breadCrumbCon = document.querySelector(".breadCrumb");
// api ๋ฐ์ดํฐ
const postData = await request({ method: "GET" });
// console.log(postData);
//data ๊ฒฝ๋ก ํ์
function findId(postData, targetId, path = []) {
for (const post of postData) {
// ํ์ถ ์กฐ๊ฑด: ํ์ฌ ๋ฐ์ดํฐ์ id๊ฐ ๋ชฉํ id์ ์ผ์นํ๋ฉด ํ์ดํ ๋ฐํ
if (post.id === targetId) return [...path, post.title];
// documents ๋ฐฐ์ด ํ์ํ์ฌ ์ฌ๊ท ํธ์ถ
if (post.documents) {
const result = findId(post.documents, targetId, [...path, post.title]);
if (result) return result; // ๋ชฉํ ID๋ฅผ ์ฐพ์ผ๋ฉด ๊ฒฐ๊ณผ ๋ฐํ
}
}
return null; // ๋ชฉํ๋ฅผ ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ
}
//๋ฐฐ์ด๊ฐ 3๊ฐ๋ง ๊ฐ์ ธ์์ ํ์
const pathTitle = () => {
const path = findId(postData, 140046);
if (path.length === 1) { return path; }
else {
return path
.slice(-3)
.map(v => {
return v.length > 7 ? `${v.slice(0, 7)}...` : v;
});
}
};
//dom์ ํ์
let breadCrumbInner = `<ul>`;
pathTitle().forEach(title => {
breadCrumbInner += `<li><a>${title}</a></li>`;
});
breadCrumbInner += `</ul>`;
breadCrumbCon.innerHTML = breadCrumbInner;
๊ฒฝ๋ก / ํ์๋ ์ด๋ป๊ฒ ํ ๊น ํ๋ค๊ฐ css ์ ํ์๋ฅผ ํ์ฉํ๋ค
.breadCrumb > li:not(:last-child)::after {
content: " /";
margin-left: 9px;
}
์ข ์ผ ํ๋ก์ ํธ ์งํํ ํ๋ฃจ^.^ ๊นํ๋ธ๋ฅผ ์ฌ์ฉํ์ง ์์์ zip์ผ๋ก ์ฃผ๊ณ ๋ฐ์๋๋ฐ ์ด๊ฒ ๋ง๋...? ์ถ์๋ค. ๋ฐฉ๊ธ ์ ์ ๋ด๊ฐ ๋ญ ์์ ํ๊ณ ๋๊ธด ๊ฑด์ง๋ ๊ธฐ์ต์ด ์ ๋๋ ํ์... ๊นํ๋ธ์ ์์คํจ์ ์๊ฒ ๋๋ค. ๋งค๋ฒ ํท๊ฐ๋ ธ๋ ์ฌ๊ทํจ์๋ฅผ ํ๋ก์ ํธ์ ์ฌ์ฉํด ๋ณผ ์ ์์ด์ ์ข์๋ค.