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 돔 조작과 이벤트 핸들링으로 메뉴 관리하기 #268

Open
wants to merge 2 commits into
base: areumsheep
Choose a base branch
from

Conversation

areumsheep
Copy link

🎯 step1 요구사항 - 돔 조작과 이벤트 핸들링으로 메뉴 관리하기

  • 에스프레소 메뉴에 새로운 메뉴를 확인 버튼 또는 엔터키 입력으로 추가한다.
    • 메뉴가 추가되고 나면, input은 빈 값으로 초기화한다.
    • 사용자 입력값이 빈 값이라면 추가되지 않는다.
  • 메뉴의 수정 버튼을 눌러 메뉴 이름 수정할 수 있다.
    • 메뉴 수정시 브라우저에서 제공하는 prompt 인터페이스를 활용한다.
  • 메뉴 삭제 버튼을 이용하여 메뉴 삭제할 수 있다.
    • 메뉴 삭제시 브라우저에서 제공하는 confirm 인터페이스를 활용한다.
  • 총 메뉴 갯수를 count하여 상단에 보여준다.
  • 추가되는 메뉴의 아래 마크업은 <ul id="espresso-menu-list" class="mt-3 pl-0"></ul> 안에 삽입해야 한다.

✨ 상세 설명

dom.js

  • querySelector 함수를 통해 요소를 가져오고 만약 없는 요소를 가져올 시 에러를 반환한다.

index.js

  • form에 onSubmit이벤트를 추가하여 input에 메뉴 이름이 입력되어 있을 시 메뉴 리스트에 메뉴를 추가한다.
    • 메뉴 추가 후 폼의 reset()을 활용하여 input을 빈 값으로 초기화한다.
    • 총 메뉴 개수를 추가하기 위해 updateEspressoMenuCount()함수를 호출한다.
  • 부모(#espresso-menu-list)에 이벤트를 추가하여 이벤트 위임을 통해 자식에게 전달되도록 한다.
    • data-action으로 어떤 버튼이 클릭되었는지 확인한다.
    • 삭제할 경우 총 메뉴 개수를 수정하기 위해 updateEspressoMenuCount()함수를 호출한다.

🤔 더 고민해보고 싶은 것

  • 늦어질까봐 일단 index.js에 모든 걸 다 맡기는 식으로 개발을 하였습니다...🥲 이후에 메뉴별로 상태를 관리해야 할 경우를 대비하여 남은 시간동안 더 고민해볼게요!

Copy link

@dhrod0325 dhrod0325 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요 조이!

리뷰 남겨주셔서 정말 감사해욧! ㅎㅎ

저도 다른 팀이지만 리뷰 조금 남겨 보았어요!

남은 기간 같이 잘해봐요~! 👍

Comment on lines +54 to +58
if (deleteFlag) {
const deleteMenu = currentTarget.closest('.menu-list-item');
deleteMenu.remove();
}
updateEspressoMenuCount(-1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (deleteFlag) {
const deleteMenu = currentTarget.closest('.menu-list-item');
deleteMenu.remove();
}
updateEspressoMenuCount(-1);
if (!deleteFlag) {
return;
}
const deleteMenu = currentTarget.closest('.menu-list-item');
deleteMenu.remove();
updateEspressoMenuCount(-1);

지금은 취소를 눌러도 updateEspressoMenuCount가 호출되네요 early return 이 이런 오류를 예방하기 좋은 것 같아요! 😀

@@ -0,0 +1,5 @@
export function $(selector) {
const element = document.querySelector(selector);
if (element === null) throw new Error('element is null');
Copy link

@dhrod0325 dhrod0325 Jul 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

selector 이름을 만약에 잘못 입력 했을 때 예외처리를 안 해주게 되면 다음 스크립트가 동작을 안하게 되겠네요.!
엄격하게 체크하려고 의도 하신 건지 궁금해요~!😮


espressoMenuList.addEventListener('click', (event) => {
const currentTarget = event.target;
switch (currentTarget?.dataset.action) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

옵셔널체이닝 좋네요! 👍 data로 이벤트를 분기 하는 것 도 좋은 것 같아요!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dataset 을 사용해서 switch 쓰도록 한 방식 좋네요! 저도 사용해봐야겠네용 ㅎㅎ

event.preventDefault();
const menuName = espressoMenuInput.value;

if (menuName) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

early return 해주면 뒤 코드의 흐름을 보기 더 쉬울 것 같아요~!

Comment on lines +3 to +7
let orderedMenuCount = 0;
const espressoMenuCount = $('.menu-count');
const espressoMenuForm = $('#espresso-menu-form');
const espressoMenuInput = $('#espresso-menu-name');
const espressoMenuList = $('#espresso-menu-list');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용되는 변수들을 모아두니 보기 편하네요! 😀

const currentTarget = event.target;
switch (currentTarget?.dataset.action) {
case 'edit':
const newMenuName = prompt('메뉴명을 수정하세요.');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 메뉴 이름이 비어 있을 때도 체크해주면 좋을 것 같아요~!

</button>
<button
type="button"
data-action="delete"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 name attribute 를 추가하여 이벤트를 분기했는데 data-action을 사용하셨네요 !👍

const espressoMenuInput = $('#espresso-menu-name');
const espressoMenuList = $('#espresso-menu-list');

const updateEspressoMenuCount = (number) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, -1 를 인자로 넘겨서 처리 하셨군요!
menu list의 length를 활용하는 방법도 고려해보면 좋을것 같아요 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants