-
Notifications
You must be signed in to change notification settings - Fork 374
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 - 상태 관리로 메뉴 관리하기 #288
Open
pers0n4
wants to merge
14
commits into
blackcoffee-study:pers0n4
Choose a base branch
from
pers0n4:pers0n4
base: pers0n4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
59898af
refactor: set menu name attribute in MenuItem constructor
pers0n4 e47b4b0
refactor: change App function to class
pers0n4 b066cd0
refactor: change directory structure
pers0n4 a53d884
feat: add storage utils
pers0n4 a3da0e3
refactor: add state to App
pers0n4 14c9a99
feat: use local storage for caching
pers0n4 ea751dc
style: change `children.length` to `childElementCount`
pers0n4 b68779a
feat: declare html elements
pers0n4 2cdb20c
feat: support menu category select feature
pers0n4 9f4d93a
fix: update menu count when render menu items
pers0n4 20bb756
feat: implement menu sold out feature
pers0n4 0a7403e
fix: fix menu input placeholder
pers0n4 f44f9f7
fix: fix element selector
pers0n4 ea76efd
style: change code style
pers0n4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,199 @@ | ||
import { select } from "./dom.js"; | ||
import MenuItem from "./MenuItem.js"; | ||
import MenuItem from "./components/MenuItem.js"; | ||
import { select } from "./utils/dom.js"; | ||
import { localStore } from "./utils/storage.js"; | ||
|
||
export default function App() { | ||
/** @type {HTMLFormElement} */ | ||
const menuForm = select("#espresso-menu-form"); | ||
export default class App { | ||
/** | ||
* @type {HTMLFormElement} | ||
* @readonly | ||
*/ | ||
#menuForm = select("#menu-form"); | ||
|
||
/** | ||
* @type {HTMLUListElement} | ||
* @readonly | ||
*/ | ||
#menuList = select("#menu-list"); | ||
|
||
/** @type {HTMLUListElement} */ | ||
const menuList = select("#espresso-menu-list"); | ||
/** | ||
* @type {HTMLInputElement} | ||
* @readonly | ||
*/ | ||
#menuInput = select("#menu-name"); | ||
|
||
/** @type {HTMLInputElement} */ | ||
const menuInput = select("#espresso-menu-name"); | ||
/** | ||
* @type {HTMLSpanElement} | ||
* @readonly | ||
*/ | ||
#menuCount = select(".menu-count"); | ||
|
||
init(); | ||
/** | ||
* @type {HTMLHeadingElement} | ||
* @readonly | ||
*/ | ||
#categoryTitle = select(".category-title"); | ||
|
||
function init() { | ||
menuForm.addEventListener("submit", (event) => { | ||
/** | ||
* @typedef {"espresso" | "frappuccino" | "blended" | "teavana" | "desert"} MenuCategory | ||
* @typedef {{name: string, isSoldOut: boolean}} MenuItem | ||
* @typedef {{selectedCategory: MenuCategory, menuList: MenuItem[]}} State | ||
* | ||
* @type {State} | ||
*/ | ||
#state = { | ||
selectedCategory: "espresso", | ||
menuList: localStore.get("espresso.menuList", []), | ||
}; | ||
|
||
constructor() { | ||
this.init(); | ||
this.render(); | ||
} | ||
|
||
init() { | ||
this.#menuForm.addEventListener("submit", (event) => { | ||
event.preventDefault(); | ||
appendMenuItem(); | ||
this.appendMenuItem(); | ||
}); | ||
|
||
menuList.addEventListener("click", ({ target }) => { | ||
if (!target) return; | ||
this.#menuList.addEventListener("click", ({ target }) => { | ||
if (!target) { | ||
return; | ||
} | ||
|
||
const menuItem = target.closest("moon-menu-item"); | ||
const clickedIndex = Array.from(this.#menuList.children).indexOf( | ||
menuItem, | ||
); | ||
|
||
if (target.classList.contains("menu-edit-button")) { | ||
editMenuItem(menuItem); | ||
this.editMenuItem(clickedIndex, menuItem); | ||
} else if (target.classList.contains("menu-remove-button")) { | ||
removeMenuItem(menuItem); | ||
this.removeMenuItem(clickedIndex); | ||
} else if (target.classList.contains("menu-sold-out-button")) { | ||
this.toggleSoldOutMenuItem(clickedIndex, menuItem); | ||
} | ||
}); | ||
|
||
select("nav").addEventListener("click", ({ target }) => { | ||
if (!target) { | ||
return; | ||
} | ||
|
||
if ("categoryName" in target.dataset) { | ||
/** @type {MenuCategory} */ | ||
const selectedCategory = target.dataset.categoryName; | ||
this.setState({ | ||
selectedCategory, | ||
menuList: localStore.get(`${selectedCategory}.menuList`, []), | ||
}); | ||
this.updateCategoryTitle(target.innerText); | ||
} | ||
}); | ||
} | ||
|
||
function appendMenuItem() { | ||
if (menuInput.value.trim()) { | ||
const menuItem = new MenuItem(); | ||
menuItem.setAttribute("name", menuInput.value.trim()); | ||
menuList.appendChild(menuItem); | ||
menuInput.value = ""; | ||
updateMenuCount(); | ||
/** | ||
* @param {State} nextState | ||
*/ | ||
setState(nextState) { | ||
this.#state = { ...this.#state, ...nextState }; | ||
this.render(); | ||
localStore.set( | ||
`${this.#state.selectedCategory}.menuList`, | ||
this.#state.menuList, | ||
); | ||
} | ||
|
||
render() { | ||
this.#menuList.replaceChildren( | ||
...this.#state.menuList.map( | ||
({ name, isSoldOut }) => new MenuItem(name, isSoldOut), | ||
), | ||
); | ||
this.updateMenuCount(); | ||
} | ||
|
||
appendMenuItem() { | ||
const menuName = this.#menuInput.value.trim(); | ||
if (!menuName) { | ||
return; | ||
} | ||
|
||
this.setState({ | ||
menuList: [...this.#state.menuList, { name: menuName, isSoldOut: false }], | ||
}); | ||
this.#menuInput.value = ""; | ||
} | ||
|
||
/** | ||
* @param {number} index | ||
* @param {MenuItem} menuItem | ||
*/ | ||
function editMenuItem(menuItem) { | ||
const newMenuName = window.prompt( | ||
editMenuItem(index, menuItem) { | ||
const menuName = window.prompt( | ||
"수정할 메뉴 이름을 입력하세요", | ||
menuItem.getAttribute("name"), | ||
menuItem.name, | ||
); | ||
if (newMenuName) { | ||
menuItem.setAttribute("name", newMenuName); | ||
if (!menuName) { | ||
return; | ||
} | ||
|
||
menuItem.name = menuName; | ||
this.setState({ | ||
menuList: [ | ||
...this.#state.menuList.slice(0, index), | ||
{ | ||
name: menuItem.name, | ||
isSoldOut: menuItem.isSoldOut, | ||
}, | ||
...this.#state.menuList.slice(index + 1), | ||
], | ||
}); | ||
} | ||
|
||
/** | ||
* @param {number} index | ||
* @param {MenuItem} menuItem | ||
*/ | ||
function removeMenuItem(menuItem) { | ||
if (window.confirm("메뉴를 삭제하시겠습니까?")) { | ||
menuItem.remove(); | ||
updateMenuCount(); | ||
toggleSoldOutMenuItem(index, menuItem) { | ||
menuItem.isSoldOut = !menuItem.isSoldOut; | ||
this.setState({ | ||
menuList: [ | ||
...this.#state.menuList.slice(0, index), | ||
{ | ||
name: menuItem.name, | ||
isSoldOut: menuItem.isSoldOut, | ||
}, | ||
...this.#state.menuList.slice(index + 1), | ||
], | ||
}); | ||
} | ||
|
||
/** | ||
* @param {number} index | ||
*/ | ||
removeMenuItem(index) { | ||
if (!window.confirm("메뉴를 삭제하시겠습니까?")) { | ||
return; | ||
} | ||
|
||
this.setState({ | ||
menuList: [ | ||
...this.#state.menuList.slice(0, index), | ||
...this.#state.menuList.slice(index + 1), | ||
], | ||
}); | ||
} | ||
|
||
updateMenuCount() { | ||
const menuCount = this.#menuList.childElementCount; | ||
this.#menuCount.textContent = `총 ${menuCount}개`; | ||
} | ||
|
||
function updateMenuCount() { | ||
const menuCount = menuList.children.length; | ||
select(".menu-count").textContent = `총 ${menuCount}개`; | ||
/** | ||
* @param {string} title | ||
*/ | ||
updateCategoryTitle(title) { | ||
this.#categoryTitle.textContent = `${title} 메뉴 관리`; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
이 부분은 중복으로 사용되고 있는거 같은데 함수화 해서 사용하는건 어떨까요~?