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

[2주차 기본 과제] 웨비들의 냠냠 🍰 창업🏠 손님을 모셔오자!🌈 #2

Open
wants to merge 1 commit into
base: main
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
68 changes: 68 additions & 0 deletions dummy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
export default [
{
"menuName":"국물닭발",
"menuTag":["#칼칼","#닭발엔소주", "#국물","#냠냠"],
Copy link

Choose a reason for hiding this comment

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

요기서 # 은 before 로 붙여봐도 좋을 것 같댜!!! :)

"menuImgUrl":"./images/chicken_foot.jpeg",
"category":"안주",
},
{
"menuName":"야채곱창",
"menuTag":["#돼지곱창", "#야채듬뿍","#매콤"],
"menuImgUrl":"./images/gopchang.jpeg",
"category":"안주"
},
{
"menuName":"오돌뼈",
"menuTag":["#주먹밥","#매콤", "#오독오독"],
"menuImgUrl":"./images/odol.jpeg",
"category":"안주"
},
{
"menuName":"어묵탕",
"menuTag":["#소주친구","#뜨근국물", "#탕"],
"menuImgUrl":"./images/odang.jpeg",
"category":"탕"
},
{
"menuName":"알탕",
"menuTag":["#소주친구","#알&곤이", "#탕"],
"menuImgUrl":"./images/altang.jpeg",
"category":"탕"
},
{
"menuName":"짬뽕탕",
"menuTag":["#소주친구","#해물가득", "#탕"],
"menuImgUrl":"./images/jjam.jpeg",
"category":"탕"
},
{
"menuName":"황도",
"menuTag":["#시원함","#과일가득"],
"menuImgUrl":"./images/hwangdo.jpeg",
"category":"사이드"
},
{
"menuName":"계란찜",
"menuTag":["#포슬포슬","#닭발과함께"],
"menuImgUrl":"./images/egg.jpeg",
"category":"사이드"
},
{
"menuName":"참이슬",
"menuTag":["#소주","#참이슬누가먹음"],
"menuImgUrl":"./images/chami.jpeg",
"category":"술"
},
{
"menuName":"처음처럼",
"menuTag":["#소주","#소주1위의존엄"],
"menuImgUrl":"./images/cheo.jpeg",
"category":"술"
},
{
"menuName":"한라산",
"menuTag":["#소주","#화끈하게21도"],
"menuImgUrl":"./images/han.jpeg",
"category":"술"
}
]
97 changes: 97 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>bomipocha</title>
<link rel="stylesheet" href="style.css">
<script async type="module" src="script.js"></script>
<script src="https://kit.fontawesome.com/337bf03ec9.js" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"></link>

</head>
<body>
<header>
<h4>
보미포차
</h4>
<h4>
<input type="checkbox" id="icon">
<label for="icon">
<span></span>
<span></span>
<span></span>
</label>
<div class="menu-container">
<ul>
<li>메뉴</li>
<li>새 상품 추가</li>
<li>찜 목록</li>
</ul>
</div>
</h4>
</header>
<div id="nav-cards-wrap">
<nav>
<h1 class="nav-items">종류</h1>
<!-- <input type="checkbox" id="category">
<label for="category">
<div class="nav-container">
<div class="nav-items">전체</div>
</div>
</label> -->
<a href="#ctgr-all" class="nav-container">
Copy link
Member

Choose a reason for hiding this comment

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

오 카테고리 부분에 a태그로 href를 준게 신박해!! 혹시 이렇게 해놓으면 따로 장점이 있을까? 카테고리 클릭할 때마다 url주소가 ~.com/#ctgr-all 이런식으로 바뀔거같은데 맞나?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

맞아 !
장점이라고 하면 카테고리에 대한 링크 구분이 확실히 된다는 거??
근데 클릭이벤트 구현할 때 a태그를 쓸지 말자라는 아티클도 있더라고 ~.. 좀 더 고민해봐야할 것 같긴행

<div class="nav-items">전체</div>
</a>
<a href="#ctgr-dish" class="nav-container">
<div class="nav-items">안주</div>
</a>
<a href="#ctgr-tang" class="nav-container">
<div class="nav-items">탕</div>
</a>
<a href="#ctgr-side" class="nav-container">
<div class="nav-items">사이드</div>
</a>
<a href="#ctgr-alcohol" class="nav-container">
<div class="nav-items">술</div>
</a>
Comment on lines +46 to +57
Copy link

Choose a reason for hiding this comment

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

요 부분은 구조가 전체적으로 반복되니까 map 을 사용해 봐도 좋을 것 같아!! :)

</nav>
<div id="ctg-cards-wrap">
<div class="selected-ctgr">
<div id="ctgr-all" style="display:none;">
Copy link
Member

Choose a reason for hiding this comment

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

default로 display:none을 주어야해서 인라인 속성을 준거같은데 그래두 인라인 속성은 지양하는게 좋으니 css파일에서 지정해놨어도 좋았겠다 !!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오키오키 인라인 지양 확인!

<div class="category-container">
<h2> 전체</h2>
<h3 class="cancel-btn">X</h3>
</div>
</div>
<div id="ctgr-dish" style="display:none;">
<div class="category-container">
<h2> 안주</h2>
<h3 class="cancel-btn">X</h3>
</div>
</div>
<div id="ctgr-tang" style="display:none;">
<div class="category-container">
<h2> 탕</h2>
<h3 class="cancel-btn">X</h3>
</div>
</div>
<div id="ctgr-side" style="display:none;">
<div class="category-container">
<h2> 사이드</h2>
<h3 class="cancel-btn">X</h3>
</div>
</div>
<div id="ctgr-alcohol" style="display:none;">
<div class="category-container">
<h2> 술</h2>
<h3 class="cancel-btn">X</h3>
</div>
Comment on lines +68 to +89
Copy link

Choose a reason for hiding this comment

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

요 부분도 map 으로 처리해 주면 좋을 것 같아요잉 :)

</div>
</div>
<div id="card-wrap">
</div>
</div>
</div>
</body>
</html>
144 changes: 144 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import data from './dummy.js';
/**
* nav에서 선택된 카테고리에 따라 카드, 태그 생성/삭제함
*/
var target=document.querySelectorAll('.nav-container');

Choose a reason for hiding this comment

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

보니까 전체적으로 var 을 이용했는데 const 를 지향해보는 것도 좋을 것 같아!! 혹시 var 를 쓴 특별한 이유가 있을까???? :)

var btnClose=document.querySelectorAll('.selected-ctgr .cancel-btn')
Copy link

Choose a reason for hiding this comment

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

요기서는 dom 요소를 저장하는 변수 네임이니까, 동사보다는 명사형으로 해 보면 어떨까??? 예를 들어 closeButton 같은??? :)

var targetID, targetClass;
const selectedCtgr=[];
var ctgrAllList=[".ctgr-dish",".ctgr-tang",".ctgr-side",".ctgr-alcohol"]
Copy link
Member

Choose a reason for hiding this comment

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

이런 카테고리 목록 리스트 같은거, 먼가 이왕 더미데이터 만든김에 하드코딩보다는 데이터 만들어놓은 거에서 가져와도 좋을듯!! ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오오 좋은 생각 !!

var nonSelectedCtgr=[];
// 생성
for(var i=0;i<target.length;i++){
Copy link

Choose a reason for hiding this comment

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

리턴 값이 없다면 forEach 를 써봐도 좋을 것 같아!! 그런데 여기서는 생성이니까 map 을 써서 새 배열을 만들어봐두 좋을 것 같다!!!! :)

target[i].addEventListener('click',function(){
Copy link

Choose a reason for hiding this comment

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

요기서 function 대신에 화살표 함수 써 보는 것두 좋을 것 같댜!! :)

targetID=this.getAttribute('href');
document.querySelector(targetID).style.display='block';
if (targetID==="#ctgr-all"){
for (var t=0;t<ctgrAllList.length;t++){
for (var j=0;j<document.querySelectorAll(ctgrAllList[t]).length;j++){
document.querySelectorAll(ctgrAllList[t])[j].style.display='block';
Copy link
Member

Choose a reason for hiding this comment

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

오!! 카테고리 선택에 따라 해당하는 목록의 style을 block/none으로 바꿔서 필터링을 구현했구나!!!! 나는 왜 이 생각을 못한걸까...

}
}
}
else{
targetClass=targetID.replace('#','.');
Copy link
Member

Choose a reason for hiding this comment

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

옹 요거 신박하군 ㅋㅋㅋ 같은 명칭을 class로도 쓰고 id로도 쓴건가? querySelectorAll() 말고 selectElementsByClassName('명칭')이랑 selectElementById('명칭')를 써도 될거같다는 생각?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

아무래도 그게 좀 더 가독성 있는 코드긴 하겠다!
피드백 고마워 ㅎㅎ

for (var j=0;j<document.querySelectorAll(targetClass).length;j++){
document.querySelectorAll(targetClass)[j].style.display='block';
Copy link

Choose a reason for hiding this comment

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

클래스명으로 dom 요소를 선택하구 싶은 거라면 getElementsByClassName 요거 사용해봐도 좋을 것 같아용!!

}
selectedCtgr.push(targetClass);
}
});
}

for(var j=0;j<target.length;j++){
btnClose[j].addEventListener('click',function(){
this.parentNode.parentNode.style.display='none'
Copy link

Choose a reason for hiding this comment

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

요기서는 parentNode 로 접근하는 것도 좋지만, 버튼의 parent parent 에 eventListener 를 붙이고, 버튼 이벤트를 감지하면 어떨까??? :)

targetClass="."+this.parentNode.parentNode.getAttribute('id');
if (targetClass===".ctgr-all"){
nonSelectedCtgr = ctgrAllList.filter(x=> !selectedCtgr.includes(x));
Copy link
Member

Choose a reason for hiding this comment

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

나 이부분 구현하는거 진짜 애먹었는데 ~~ 현재 선택되어 있는 카테고리 목록을 selectedCtgr에 항상 저장해주고 모든 카테고리 리스트(ctgrAllList)에서 이를 빼줌으로써 구현한거구나? 엄청 심플해..
근데 궁금한게, 지금 이 길다란 for문이 아마 카테고리 해제하는 부분을 담당하는 것 같은데, 그러면 해제해준 뒤에 selectedCtgr 변수에서 해당 카테고리를 제거해야하지 않아? 근데 그부분이 안보이는 것 같아서 궁금해 내가 못찾는건가

Copy link
Contributor Author

Choose a reason for hiding this comment

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

어머 맞아 .

지금 확인해보니까
전체, 다른카테고리 있을 때 다른카테고리를 해제하면 바로 없어지네.(원래 전체에 의해 있어야하는데)
그 부분이 말해준 부분인 것 같아 !! 으악

다시 기능 수정해야겠다 고마워 ㅎㅎ

for (var t=0;t<nonSelectedCtgr.length;t++){
for (var j=0;j<document.querySelectorAll(nonSelectedCtgr[t]).length;j++){
var clsList=document.querySelectorAll(nonSelectedCtgr[t])[0].classList;
document.querySelectorAll(nonSelectedCtgr[t])[j].style.display='none';
}
}
}
else{
for (var j=0;j<document.querySelectorAll(targetClass).length;j++){
var clsList=document.querySelectorAll(targetClass)[0].classList;
document.querySelectorAll(targetClass)[j].style.display='none';
}
}
});
}


/**
* 메뉴를 더미데이터에서 가져옴
*/
function ctgrMatch(post){
if (post.category==="안주"){
return "ctgr-dish";
}
else if (post.category==="탕"){
return "ctgr-tang";
}
else if (post.category==="사이드"){
return "ctgr-side";
}
else if (post.category==="술"){
return "ctgr-alcohol";
}
}
Comment on lines +59 to +72

Choose a reason for hiding this comment

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

이 부분 switch case 문으로 작성해보는 건 어떨까??? :)

function postTitle(post){
Copy link
Member

Choose a reason for hiding this comment

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

element만드는 부분을 각 구성요소별로 함수화한게 무척 깔끔해서 가독성이 좋아!! b

const h2=document.createElement('h2')
h2.setAttribute('class','card-title')
h2.textContent=post.menuName
return h2
}
function postTag(post){

Choose a reason for hiding this comment

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

함수명도 잘 지었구 기능별로 함수를 잘 분리한 것 같아!! 최고다!! :)

const div=document.createElement('div')
div.setAttribute('class','card-tag')
const h3=document.createElement('p')
h3.innerText=post.menuTag
const icon=document.createElement('img')
icon.setAttribute('src','./images/square-caret-down-solid.svg')
icon.setAttribute('class','modal-open-icon')
icon.setAttribute('alt','더보기아이콘')
div.append(h3)
div.append(icon)
Comment on lines +80 to +89
Copy link

Choose a reason for hiding this comment

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

요 부분 mapa으로 새로운 배열 만들어서 innerHTML 로 넣어주면 좀 더 직관적일 수 잇을 것 같댜!!!! :)

Choose a reason for hiding this comment

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

지금 코드 보니까 보미 언니는 post 로 data[i]를 받아와서,
컨테이너인 div 를 createElement로 만들고, 그 div 에 class 속성으로 card_tag 를 만들고 있자나??
근데 이렇게 하면, html 태그 요소가 한 눈에 보이지 않아서 가독성이 조금 떨어지는 것 같다고 생각햇어!!

그리고 나는 태그가 복수개일 것이라고 생각해서, 그 태그를 분리하는 작업을 map 을 이용해서 해주면 좋겠다는 뜻이어써!! 만약 태그가 복수개가 아니라면 그냥 innerHTML만 사용해주면 돼!! :)

return div
}
function postImg(post){
const img=document.createElement('img')
img.setAttribute('src',post.menuImgUrl)
img.setAttribute('class','menuImg')
img.setAttribute('alt','메뉴이미지')
return img
}
for (var i=0;i<data.length;i++){

Choose a reason for hiding this comment

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

여기서 var 는 let 으로 변경해주면 좋을 것 같아!!! :)

const cardContent= document.querySelector("#card-wrap")
const section=document.createElement('section')
section.setAttribute('class','card')
// 더미데이터의 카테고리를 태그의 속성과 매치해주는 부분
section.classList.add(ctgrMatch(data[i]))
cardContent.append(section)
const div=document.createElement('div')
div.setAttribute('id','content')
section.append(div)
div.append(postTitle(data[i]));
div.append(postTag(data[i]));
// 팝업되는 태그 모달
const modal=document.createElement('div')
modal.setAttribute('class','tag-modal')
modal.innerText=data[i].menuTag
const icon=document.createElement('img')
icon.setAttribute('src','./images/square-caret-down-solid.svg')
icon.setAttribute('class','modal-close-icon')
icon.setAttribute('alt','더보기아이콘')
modal.append(icon)
modal.style.display='none';
div.append(modal)

div.append(postImg(data[i]));
const img=document.createElement('img')
img.setAttribute('src','./images/glass-water-solid.svg')
img.setAttribute('class','icon')
img.setAttribute('alt','찜아이콘')
div.append(img)
}


var modalOpenTarget=document.querySelectorAll('.card-tag .modal-open-icon');
// var modalCloseTarget=document.querySelectorAll('.modal-close-icon');
for(var i=0;i<modalOpenTarget.length;i++){
modalOpenTarget[i].addEventListener('click',function(){
this.parentNode.parentNode.querySelector('.tag-modal').style.display='block';
});
}
var modalCloseTarget=document.querySelectorAll('.modal-close-icon');
for(var i=0;i<modalCloseTarget.length;i++){
modalCloseTarget[i].addEventListener('click',function(){
this.parentNode.style.display='none';
});
}
Comment on lines +132 to +144

Choose a reason for hiding this comment

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

요 부분 함수로 분리해주면 더 가독성 좋아지겠따!!!!! :)

Loading