-
Notifications
You must be signed in to change notification settings - Fork 377
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 돔 조작과 이벤트 핸들링으로 메뉴 관리하기 #268
base: areumsheep
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,89 @@ | ||
<!DOCTYPE html> | ||
<html lang="kr"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>문벅스 메뉴</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="shortcut icon" href="src/images/favicon.ico" /> | ||
<link rel="icon" href="src/images/favicon.png" /> | ||
<link rel="stylesheet" href="src/css/index.css" /> | ||
</head> | ||
<body> | ||
<div id="app" class="px-4"> | ||
<div class="d-flex justify-center mt-5 w-100"> | ||
<div class="w-100"> | ||
<header class="my-4"> | ||
<a href="/" class="text-black"> | ||
<h1 class="text-center font-bold">🌝 문벅스 메뉴 관리</h1> | ||
</a> | ||
<nav class="d-flex justify-center flex-wrap"> | ||
<button | ||
data-category-name="espresso" | ||
class="cafe-category-name btn bg-white shadow mx-1" | ||
> | ||
☕ 에스프레소 | ||
</button> | ||
<button | ||
data-category-name="frappuccino" | ||
class="cafe-category-name btn bg-white shadow mx-1" | ||
> | ||
🥤 프라푸치노 | ||
</button> | ||
<button | ||
data-category-name="blended" | ||
class="cafe-category-name btn bg-white shadow mx-1" | ||
> | ||
🍹 블렌디드 | ||
</button> | ||
<button | ||
data-category-name="teavana" | ||
class="cafe-category-name btn bg-white shadow mx-1" | ||
> | ||
🫖 티바나 | ||
</button> | ||
<button | ||
data-category-name="desert" | ||
class="cafe-category-name btn bg-white shadow mx-1" | ||
> | ||
🍰 디저트 | ||
</button> | ||
</nav> | ||
</header> | ||
<main class="mt-10 d-flex justify-center"> | ||
<div class="wrapper bg-white p-10"> | ||
<div class="heading d-flex justify-between"> | ||
<h2 class="mt-1">☕ 에스프레소 메뉴 관리</h2> | ||
<span class="mr-2 mt-4 menu-count">총 0개</span> | ||
</div> | ||
<form id="espresso-menu-form"> | ||
<div class="d-flex w-100"> | ||
<label for="espresso-menu-name" class="input-label" hidden> | ||
에스프레소 메뉴 이름 | ||
</label> | ||
<input | ||
type="text" | ||
id="espresso-menu-name" | ||
name="espressoMenuName" | ||
class="input-field" | ||
placeholder="에스프레소 메뉴 이름" | ||
autocomplete="off" | ||
/> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>문벅스 메뉴</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="shortcut icon" href="src/images/favicon.ico" /> | ||
<link rel="icon" href="src/images/favicon.png" /> | ||
<link rel="stylesheet" href="src/css/index.css" /> | ||
</head> | ||
<body> | ||
<div id="app" class="px-4"> | ||
<div class="d-flex justify-center mt-5 w-100"> | ||
<div class="w-100"> | ||
<header class="my-4"> | ||
<a href="/" class="text-black"> | ||
<h1 class="text-center font-bold">🌝 문벅스 메뉴 관리</h1> | ||
</a> | ||
<nav class="d-flex justify-center flex-wrap"> | ||
<button | ||
type="button" | ||
name="submit" | ||
id="espresso-menu-submit-button" | ||
class="input-submit bg-green-600 ml-2" | ||
data-category-name="espresso" | ||
class="cafe-category-name btn bg-white shadow mx-1" | ||
> | ||
확인 | ||
☕ 에스프레소 | ||
</button> | ||
<button | ||
data-category-name="frappuccino" | ||
class="cafe-category-name btn bg-white shadow mx-1" | ||
> | ||
🥤 프라푸치노 | ||
</button> | ||
<button | ||
data-category-name="blended" | ||
class="cafe-category-name btn bg-white shadow mx-1" | ||
> | ||
🍹 블렌디드 | ||
</button> | ||
<button | ||
data-category-name="teavana" | ||
class="cafe-category-name btn bg-white shadow mx-1" | ||
> | ||
🫖 티바나 | ||
</button> | ||
<button | ||
data-category-name="desert" | ||
class="cafe-category-name btn bg-white shadow mx-1" | ||
> | ||
🍰 디저트 | ||
</button> | ||
</nav> | ||
</header> | ||
<main class="mt-10 d-flex justify-center"> | ||
<div class="wrapper bg-white p-10"> | ||
<div class="heading d-flex justify-between"> | ||
<h2 class="mt-1">☕ 에스프레소 메뉴 관리</h2> | ||
<span class="mr-2 mt-4 menu-count">총 0개</span> | ||
</div> | ||
<form id="espresso-menu-form"> | ||
<div class="d-flex w-100"> | ||
<label for="espresso-menu-name" class="input-label" hidden> | ||
에스프레소 메뉴 이름 | ||
</label> | ||
<input | ||
type="text" | ||
id="espresso-menu-name" | ||
name="espressoMenuName" | ||
class="input-field" | ||
placeholder="에스프레소 메뉴 이름" | ||
autocomplete="off" | ||
/> | ||
<button | ||
type="submit" | ||
name="submit" | ||
id="espresso-menu-submit-button" | ||
class="input-submit bg-green-600 ml-2" | ||
> | ||
확인 | ||
</button> | ||
</div> | ||
</form> | ||
<ul id="espresso-menu-list" class="mt-3 pl-0"></ul> | ||
</div> | ||
</form> | ||
<ul id="espresso-menu-list" class="mt-3 pl-0"></ul> | ||
</main> | ||
</div> | ||
</main> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<script type="module" src="./src/js/index.js"></script> | ||
</body> | ||
<script type="module" src="./src/js/index.js" defer></script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,61 @@ | ||||||||||||||||||||||||||||
import { $ } from './utils/dom.js'; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
let orderedMenuCount = 0; | ||||||||||||||||||||||||||||
const espressoMenuCount = $('.menu-count'); | ||||||||||||||||||||||||||||
const espressoMenuForm = $('#espresso-menu-form'); | ||||||||||||||||||||||||||||
const espressoMenuInput = $('#espresso-menu-name'); | ||||||||||||||||||||||||||||
const espressoMenuList = $('#espresso-menu-list'); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
const updateEspressoMenuCount = (number) => { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, -1 를 인자로 넘겨서 처리 하셨군요! |
||||||||||||||||||||||||||||
orderedMenuCount += number; | ||||||||||||||||||||||||||||
espressoMenuCount.innerText = `총 ${orderedMenuCount}개`; | ||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
espressoMenuForm.addEventListener('submit', (event) => { | ||||||||||||||||||||||||||||
event.preventDefault(); | ||||||||||||||||||||||||||||
const menuName = espressoMenuInput.value; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (menuName) { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||
const content = ` | ||||||||||||||||||||||||||||
<li class="menu-list-item d-flex items-center py-2"> | ||||||||||||||||||||||||||||
<span class="w-100 pl-2 menu-name">${menuName}</span> | ||||||||||||||||||||||||||||
<button | ||||||||||||||||||||||||||||
type="button" | ||||||||||||||||||||||||||||
data-action="edit" | ||||||||||||||||||||||||||||
class="bg-gray-50 text-gray-500 text-sm mr-1 menu-edit-button" | ||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||
수정 | ||||||||||||||||||||||||||||
</button> | ||||||||||||||||||||||||||||
<button | ||||||||||||||||||||||||||||
type="button" | ||||||||||||||||||||||||||||
data-action="delete" | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 name attribute 를 추가하여 이벤트를 분기했는데 data-action을 사용하셨네요 !👍 |
||||||||||||||||||||||||||||
class="bg-gray-50 text-gray-500 text-sm menu-remove-button" | ||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||
삭제 | ||||||||||||||||||||||||||||
</button> | ||||||||||||||||||||||||||||
</li> | ||||||||||||||||||||||||||||
`; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
espressoMenuList.insertAdjacentHTML('beforeend', content); | ||||||||||||||||||||||||||||
espressoMenuForm.reset(); | ||||||||||||||||||||||||||||
updateEspressoMenuCount(+1); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
espressoMenuList.addEventListener('click', (event) => { | ||||||||||||||||||||||||||||
const currentTarget = event.target; | ||||||||||||||||||||||||||||
switch (currentTarget?.dataset.action) { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dataset 을 사용해서 switch 쓰도록 한 방식 좋네요! 저도 사용해봐야겠네용 ㅎㅎ |
||||||||||||||||||||||||||||
case 'edit': | ||||||||||||||||||||||||||||
const newMenuName = prompt('메뉴명을 수정하세요.'); | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기 메뉴 이름이 비어 있을 때도 체크해주면 좋을 것 같아요~! |
||||||||||||||||||||||||||||
currentTarget.previousElementSibling.innerText = newMenuName; | ||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||
case 'delete': | ||||||||||||||||||||||||||||
const deleteFlag = confirm('정말 삭제하시겠습니까?'); | ||||||||||||||||||||||||||||
if (deleteFlag) { | ||||||||||||||||||||||||||||
const deleteMenu = currentTarget.closest('.menu-list-item'); | ||||||||||||||||||||||||||||
deleteMenu.remove(); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
updateEspressoMenuCount(-1); | ||||||||||||||||||||||||||||
Comment on lines
+54
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
지금은 취소를 눌러도 |
||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function $(selector) { | ||
const element = document.querySelector(selector); | ||
if (element === null) throw new Error('element is null'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. selector 이름을 만약에 잘못 입력 했을 때 예외처리를 안 해주게 되면 다음 스크립트가 동작을 안하게 되겠네요.! |
||
return element; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
사용되는 변수들을 모아두니 보기 편하네요! 😀