Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14기 정새미] step1 돔 조작과 이벤트 핸들링으로 메뉴 관리하기 #274

Open
wants to merge 7 commits into
base: eddington524
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,11 @@ live-server 폴더명
## 📝 License

This project is [MIT](https://github.com/blackcoffee-study/moonbucks-menu/blob/main/LICENSE) licensed.

# 팀이름: N^6
- 김성중
- 김동욱
- 정요한
- 정새미
- 전상혁
- 서진규
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h2 class="mt-1">☕ 에스프레소 메뉴 관리</h2>
autocomplete="off"
/>
<button
type="button"
type="submit"
name="submit"
id="espresso-menu-submit-button"
class="input-submit bg-green-600 ml-2"
Expand Down
4 changes: 4 additions & 0 deletions src/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ hr {
background-color: lightgray;
}

ul{
list-style: none;
}

ul li button {
width: 60px;
line-height: inherit;
Expand Down
84 changes: 84 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const $ = (selector) => document.querySelector(selector);

function App() {

const createMenuName = () => {

if($("#espresso-menu-name").value === ""){
alert("값을 입력해주세요");
return;
}

const espressoMenuName = $("#espresso-menu-name").value;

// 로컬스토리지에 메뉴 저장
localStorage.setItem("espresso", espressoMenuName);

$("#espresso-menu-list").insertAdjacentHTML("beforeend", getMenuItemTemplate(espressoMenuName));
updateMenuCount();
$("#espresso-menu-form").reset();
};

const updateMenuCount = () => {
const menuCount = $("#espresso-menu-list").querySelectorAll('li').length;
$(".menu-count").textContent = `총 ${menuCount} 개`
};

const getMenuItemTemplate = (espressoMenuName) => {
return `<li class="menu-list-item d-flex items-center py-2">
<span class="w-100 pl-2 menu-name">${espressoMenuName}</span>
<button
type="button"
class="bg-gray-50 text-gray-500 text-sm mr-1 menu-edit-button"
>
수정
</button>
<button
type="button"
class="bg-gray-50 text-gray-500 text-sm menu-remove-button"
>
삭제
</button>
</li>`
};

const removeMenuName = (e) => {
e.target.closest("li").remove();
updateMenuCount();
};

const editMenuName = (e) =>{
const $menuName = e.target.closest("li").querySelector(".menu-name");
const editedName = prompt("메뉴명을 수정하세요", $menuName.textContent);
if(!editedName) return;
$menuName.textContent = editedName;
};

$("#espresso-menu-list").addEventListener("click", (e) => {
if (e.target.classList.contains("menu-edit-button")){
editMenuName(e);
}

if(e.target.classList.contains("menu-remove-button")){
if(confirm("정말 삭제하시겠습니까?")){
removeMenuName(e);
}
}
});

// form 태그가 자동으로 전송되는 걸 막기
$("#espresso-menu-form").addEventListener("submit", (e) => {
e.preventDefault();
createMenuName();
})

// $("#espresso-menu-name").addEventListener("keydown", (e) => {
// if(e.key !== "Enter") return;
// createMenuName();
// });

// $("#espresso-menu-submit-button").addEventListener("click", createMenuName);

}

App();