-
Notifications
You must be signed in to change notification settings - Fork 373
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기 이하은] step2 상태 관리로 메뉴 관리하기 #287
base: Leehaeun0
Are you sure you want to change the base?
Changes from all commits
b94fb7e
aef632f
73e918a
6b7834d
edaaf5e
9990325
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,81 +1,137 @@ | ||||||||
const $input = document.getElementById('espresso-menu-name'); | ||||||||
const $form = document.getElementById('espresso-menu-form'); | ||||||||
|
||||||||
// menuList = [{name: string, category: string}] | ||||||||
let menuList = []; | ||||||||
let category = 'espresso'; | ||||||||
|
||||||||
// events | ||||||||
const editMenu = ({target}) => { | ||||||||
if (!target.classList.contains('menu-edit-button')) return; | ||||||||
const name = window.prompt('메뉴명을 수정하세요'); | ||||||||
if (!name) return; | ||||||||
|
||||||||
const menuId = Number(target.parentElement.dataset.menuId); | ||||||||
menuList = menuList.map((menu, index) => index === menuId ? {...menu, name} : menu); | ||||||||
renderAboutMenus(); | ||||||||
}; | ||||||||
|
||||||||
const removeMenu = ({target}) => { | ||||||||
if (!target.classList.contains('menu-remove-button')) return; | ||||||||
if (!window.confirm('정말 삭제하시겠습니까?')) return; | ||||||||
|
||||||||
const menuId = Number(target.parentElement.dataset.menuId); | ||||||||
menuList = menuList.filter((_, index) => index !== menuId); | ||||||||
renderAboutMenus(); | ||||||||
}; | ||||||||
|
||||||||
const addMenuList = (name) => { | ||||||||
if (!name) return; | ||||||||
menuList = [...menuList, {name, category}]; | ||||||||
renderAboutMenus(); | ||||||||
$input.value = ''; | ||||||||
}; | ||||||||
|
||||||||
// addEventListeners | ||||||||
$form.addEventListener('submit', (e) => { | ||||||||
e.preventDefault(); | ||||||||
addMenuList($input.value); | ||||||||
}); | ||||||||
|
||||||||
const addEventListenersToMenuList = () => { | ||||||||
const $menu = document.getElementById('espresso-menu-list'); | ||||||||
$menu.addEventListener('click', editMenu); | ||||||||
$menu.addEventListener('click', removeMenu); | ||||||||
import { $, $id } from './utils/dom.js'; | ||||||||
import LocalStorage from './utils/localStorage.js' | ||||||||
|
||||||||
function App () { | ||||||||
const MenuListStorage = new LocalStorage('menuList'); | ||||||||
|
||||||||
// states | ||||||||
// [{name: string; category: string; isSoldOut: boolean;}] | ||||||||
let menuList = MenuListStorage.get() ?? []; | ||||||||
let category = 'espresso'; | ||||||||
|
||||||||
window.onload = () => { | ||||||||
initEventListenes(); | ||||||||
renderAboutMenus(); | ||||||||
}; | ||||||||
|
||||||||
// utils | ||||||||
const getMenuId = (target) => { | ||||||||
return Number(target.parentElement.dataset.menuId); | ||||||||
}; | ||||||||
|
||||||||
// events | ||||||||
const addMenu = () => { | ||||||||
let name = $id('menu-name').value?.trim(); | ||||||||
if (!name) return; | ||||||||
|
||||||||
menuList = [...menuList, {name, category}]; | ||||||||
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. 여기서 |
||||||||
MenuListStorage.set(menuList); | ||||||||
renderAboutMenus(); | ||||||||
$id('menu-name').value = ''; | ||||||||
}; | ||||||||
|
||||||||
const editMenu = ({target}) => { | ||||||||
const name = window.prompt('메뉴명을 수정하세요').trim(); | ||||||||
if (!name) return; | ||||||||
|
||||||||
const menuId = getMenuId(target); | ||||||||
menuList = menuList.map((menu, index) => index === menuId ? {...menu, name} : menu); | ||||||||
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.
|
||||||||
MenuListStorage.set(menuList); | ||||||||
renderAboutMenus(); | ||||||||
}; | ||||||||
|
||||||||
const removeMenu = ({target}) => { | ||||||||
if (!window.confirm('정말 삭제하시겠습니까?')) return; | ||||||||
|
||||||||
const menuId = getMenuId(target); | ||||||||
menuList = menuList.filter((_, index) => index !== menuId); | ||||||||
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. menuList를 변경하고, 로컬스토리지에 저장하는 작업이 반복적으로 사용이 되고있는데요, 함수로 따로 관리한다면 더 좋을것 같아요! |
||||||||
MenuListStorage.set(menuList); | ||||||||
renderAboutMenus(); | ||||||||
}; | ||||||||
|
||||||||
const toggleIsSoldOut = ({target}) => { | ||||||||
const menuId = getMenuId(target); | ||||||||
menuList = menuList.map((menu, index) => | ||||||||
index === menuId ? {...menu, isSoldOut: !menu.isSoldOut} : menu, | ||||||||
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.
|
||||||||
); | ||||||||
MenuListStorage.set(menuList); | ||||||||
renderAboutMenus(); | ||||||||
}; | ||||||||
|
||||||||
const setCategory = ({target}) => { | ||||||||
const selectedCategory = target.dataset.categoryName; | ||||||||
category = selectedCategory; | ||||||||
Comment on lines
+62
to
+63
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
이렇게 선언하지 않은건 |
||||||||
$id('category-title').textContent = `${target.textContent} 메뉴 관리`; | ||||||||
renderAboutMenus(); | ||||||||
}; | ||||||||
|
||||||||
// addEventListeners | ||||||||
const initEventListenes = () => { | ||||||||
const $form = $id('menu-form'); | ||||||||
$form.addEventListener('submit', (e) => { | ||||||||
e.preventDefault(); | ||||||||
addMenu(); | ||||||||
}); | ||||||||
|
||||||||
const $menu = $id('menu-list'); | ||||||||
$menu.addEventListener('click', (e) => { | ||||||||
switch (e.target.dataset.action) { | ||||||||
case 'edit': | ||||||||
editMenu(e); | ||||||||
return; | ||||||||
case 'remove': | ||||||||
removeMenu(e); | ||||||||
return; | ||||||||
case 'sold-out': | ||||||||
toggleIsSoldOut(e); | ||||||||
return; | ||||||||
} | ||||||||
}); | ||||||||
|
||||||||
const $categoryList = $id('cafe-category-name-list'); | ||||||||
$categoryList.addEventListener('click', setCategory); | ||||||||
}; | ||||||||
|
||||||||
// renders | ||||||||
const renderAboutMenus = () => { | ||||||||
const menuListTemplate = menuList | ||||||||
.filter(menu => menu.category === category) | ||||||||
.map((menu, index) => { | ||||||||
return ` | ||||||||
<li data-menu-id="${index}" class="menu-list-item d-flex items-center py-2"> | ||||||||
<span class="w-100 pl-2 menu-name${menu.isSoldOut ? ' sold-out' : ''}">${menu.name}</span> | ||||||||
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. "sold-out" class 앞의 공백을 삼항연산자 안이 아니라
Suggested change
|
||||||||
<button | ||||||||
type="button" | ||||||||
data-action="sold-out" | ||||||||
class="bg-gray-50 text-gray-500 text-sm mr-1 menu-sold-out-button" | ||||||||
> | ||||||||
품절 | ||||||||
</button> | ||||||||
<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="remove" | ||||||||
class="bg-gray-50 text-gray-500 text-sm menu-remove-button" | ||||||||
> | ||||||||
삭제 | ||||||||
</button> | ||||||||
</li> | ||||||||
` | ||||||||
}); | ||||||||
$id('menu-list').innerHTML = menuListTemplate.join(''); | ||||||||
|
||||||||
renderMenuCount(menuListTemplate.length); | ||||||||
}; | ||||||||
|
||||||||
const renderMenuCount = (count) => { | ||||||||
$('.menu-count').textContent = `총 ${count}개`; | ||||||||
}; | ||||||||
} | ||||||||
|
||||||||
// renders | ||||||||
const renderMenuList = () => { | ||||||||
const $menuList = document.getElementById('espresso-menu-list'); | ||||||||
const menuListItemElements = menuList.map((menu, index) => { | ||||||||
return ` | ||||||||
<li data-menu-id="${index}" class="menu-list-item d-flex items-center py-2"> | ||||||||
<span class="w-100 pl-2 menu-name">${menu.name}</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> | ||||||||
` | ||||||||
}); | ||||||||
$menuList.innerHTML = menuListItemElements.join(''); | ||||||||
addEventListenersToMenuList(); | ||||||||
}; | ||||||||
|
||||||||
const renderMenuCount = () => { | ||||||||
const $menuCount = document.querySelector('.menu-count'); | ||||||||
$menuCount.textContent = `총 ${menuList.length}개`; | ||||||||
}; | ||||||||
|
||||||||
const renderAboutMenus = () => { | ||||||||
renderMenuList(); | ||||||||
renderMenuCount(); | ||||||||
}; | ||||||||
App(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const $ = (selector) => document.querySelector(selector); | ||
|
||
export const $id = (id) => document.getElementById(id); | ||
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.
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export default class LocalStorage { | ||
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. 저는 단순히 util함수로 만들어서 로컬스토리지를 관리했는데 이 방법도 좋은것 같아요!👍 |
||
|
||
constructor(key) { | ||
this.key = key; | ||
} | ||
|
||
get () { | ||
return JSON.parse(localStorage.getItem(this.key)); | ||
} | ||
|
||
set (value) { | ||
return localStorage.setItem(this.key, JSON.stringify(value)); | ||
} | ||
|
||
remove (){ | ||
localStorage.removeItem(this.key); | ||
} | ||
} |
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.
??
라는게 있었네요! 저는||
를 쓰는데 앞으로는??
를 써야겠어요! 감사합니다~!😀