마이아고라스테이츠 만들기
console.log(agoraStatesDiscussions);
for (let i = 0; i < localStorage.length; i++) {
agoraStatesDiscussions.unshift(JSON.parse(localStorage.getItem(`${i}`)));
}
const convertToDiscussion = (obj) => {
const li = document.createElement("li");
li.className = "discussion__container";
const avatarWrapper = document.createElement("div");
avatarWrapper.className = "discussion__avatar--wrapper";
const discussionContent = document.createElement("div");
discussionContent.className = "discussion__content";
const discussionAnswered = document.createElement("div");
discussionAnswered.className = "discussion__answered";
const avatarImg = document.createElement("img");
avatarImg.className = "discussion__avatar--image";
avatarImg.src = obj.avatarUrl;
avatarImg.alt = "avatar of" + obj.author;
avatarWrapper.append(avatarImg);
const discussionTitle = document.createElement("h2");
const titleAnchor = document.createElement("a");
titleAnchor.href = obj.url;
titleAnchor.textContent = obj.title;
discussionTitle.append(titleAnchor);
discussionTitle.classList.add("discussion__title");
discussionContent.append(discussionTitle);
const discussionInfo = document.createElement("div");
discussionInfo.textContent = `${obj.author} / ${new Date(
obj.createdAt
).toLocaleTimeString()}`;
discussionInfo.classList.add("discussion__information");
discussionContent.append(discussionTitle, discussionInfo);
const checked = document.createElement("p");
checked.textContent = obj.answer ? "👍" : "🫶";
discussionAnswered.append(checked);
li.append(avatarWrapper, discussionContent, discussionAnswered);
return li;
};
const render = (element) => {
for (let i = 0; i < agoraStatesDiscussions.length; i += 1) {
element.append(convertToDiscussion(agoraStatesDiscussions[i]));
}
return;
};
const ul = document.querySelector("ul.discussions__container");
render(ul);
const form = document.querySelector("form.form");
const author = form.querySelector("input#name");
const title = form.querySelector("input#title");
const textbox = form.querySelector("textarea#story");
form.addEventListener("submit", (event) => {
event.preventDefault();
const newObj = {
id: "new id",
createdAt: new Date().toISOString(),
title: title.value,
url: "https://github.com/codestates-seb/agora-states-fe/discussions",
author: author.value,
bodyHTML: textbox.value,
avatarUrl:
"https://avatars.githubusercontent.com/u/97888923?s=64&u=12b18768cdeebcf358b70051283a3ef57be6a20f&v=4",
};
agoraStatesDiscussions.unshift(newObj);
const discussion = convertToDiscussion(newObj);
ul.prepend(discussion);
const objString = JSON.stringify(newObj);
window.localStorage.setItem(`${window.localStorage.length}`, objString);
title.value = "";
author.value = "";
textbox.value = "";
});
localStorage란?
- localStorage를 사용하면, 브라우저에 key-value 값을 Storage에 저장할 수 있다.
- 저장을 하면 새로고침이 이루어져도 저장한 데이터는 유지가 된다.
localStorage에 아이템 추가
localStorage에 아이템 읽기
localStorage에 객체, 배열 저장하기
const objString = JSON.stringify(obj);
const arrString = JSON.stringify(arr);
- ✅ localStorage 에는 문자열만 저장된다.
- 따라서, localStorage에 객체나 배열를 저장하기 위해서는 객체를 문자열로 변환해서 저장해야 한다. 여기서는 JSON.stringify() 함수를 사용하여 객체와 배열을 JSON 문자열로 변환하였다.