-
Notifications
You must be signed in to change notification settings - Fork 2
[손호민] TS Todo Refactor 과제 #1
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
Open
HoberMin
wants to merge
5
commits into
sonhomin
Choose a base branch
from
sonhomin_working
base: sonhomin
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules/ |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta | ||
| name="viewport" | ||
| content="width=device-width, initial-scale=1.0" /> | ||
| <title>h0ber Ts</title> | ||
| </head> | ||
| <body> | ||
| <main id="app"></main> | ||
| <script | ||
| type="module" | ||
| src="./src/main.js"></script> | ||
| </body> | ||
| </html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "name": "fedc5_learningts_study", | ||
| "version": "1.0.0", | ||
| "description": "[러닝 타입스크립트](https://www.yes24.com/Product/Goods/116585556)로 타입스크립트 뿌시기 !", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "devDependencies": { | ||
| "typescript": "^5.3.3" | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import Header from './components/Header.js'; | ||
| import createTodo from './components/createTodoInput.js'; | ||
| import TodoList from './components/TodoList.js'; | ||
| import TodoCounter from './components/TodoCounter.js'; | ||
| const HEADER_TITLE = 'TODO LIST'; | ||
| export default class App { | ||
| constructor({ $app, initialState }) { | ||
| this.$app = $app; | ||
| this.state = initialState; | ||
| new Header({ | ||
| $app: this.$app, | ||
| title: HEADER_TITLE, | ||
| }); | ||
| new createTodo({ | ||
| $app: this.$app, | ||
| onSubmit: (text) => { | ||
| const nextState = this.makeNextState(text); | ||
| this.state = nextState; | ||
| todoList.setState(nextState); | ||
| }, | ||
| }); | ||
| const todoList = new TodoList({ | ||
| $app: this.$app, | ||
| todoInitialState: this.state, | ||
| updateTodoCounter: (nextState) => { | ||
| todoCounter.setState(nextState); | ||
| }, | ||
| }); | ||
| const todoCounter = new TodoCounter({ | ||
| $app: this.$app, | ||
| initialState: this.state, | ||
| }); | ||
| } | ||
| makeNextState(text) { | ||
| const nextState = [ | ||
| ...this.state, | ||
| { | ||
| isCompleted: false, | ||
| title: text, | ||
| id: new Date().getTime().toString(), | ||
| }, | ||
| ]; | ||
| return nextState; | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { Todo, AppProps } from './util/types.js'; | ||
|
|
||
| import Header from './components/Header.js'; | ||
| import createTodo from './components/createTodoInput.js'; | ||
| import TodoList from './components/TodoList.js'; | ||
| import TodoCounter from './components/TodoCounter.js'; | ||
|
|
||
| const HEADER_TITLE = 'TODO LIST'; | ||
|
|
||
| export default class App { | ||
| $app: HTMLDivElement; | ||
| state: Todo[]; | ||
|
|
||
| constructor({ $app, initialState }: AppProps) { | ||
| this.$app = $app; | ||
| this.state = initialState; | ||
|
|
||
| new Header({ | ||
| $app: this.$app, | ||
| title: HEADER_TITLE, | ||
| }); | ||
|
|
||
| new createTodo({ | ||
| $app: this.$app, | ||
| onSubmit: (text: string) => { | ||
| const nextState = this.makeNextState(text); | ||
| this.state = nextState; | ||
| todoList.setState(nextState); | ||
| }, | ||
| }); | ||
|
|
||
| const todoList = new TodoList({ | ||
| $app: this.$app, | ||
| todoInitialState: this.state, | ||
| updateTodoCounter: (nextState: Todo[]) => { | ||
| todoCounter.setState(nextState); | ||
| }, | ||
| }); | ||
|
|
||
| const todoCounter = new TodoCounter({ | ||
| $app: this.$app, | ||
| initialState: this.state, | ||
| }); | ||
| } | ||
|
|
||
| makeNextState(text: string): Todo[] { | ||
| const nextState = [ | ||
| ...this.state, | ||
| { | ||
| isCompleted: false, | ||
| title: text, | ||
| id: new Date().getTime().toString(), | ||
| }, | ||
| ]; | ||
| return nextState; | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| export default class Header { | ||
| constructor({ $app, title }) { | ||
| this.$header = document.createElement('h1'); | ||
| this.$app = $app; | ||
| this.title = title; | ||
| this.render(); | ||
| } | ||
| render() { | ||
| this.$header.textContent = this.title; | ||
| this.$app.appendChild(this.$header); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { HeaderProps } from '../util/types.js'; | ||
|
|
||
| export default class Header { | ||
| $app: HTMLDivElement; | ||
| title: string; | ||
| $header: HTMLHeadElement; | ||
|
|
||
| constructor({ $app, title }: HeaderProps) { | ||
| this.$header = document.createElement('h1'); | ||
| this.$app = $app; | ||
| this.title = title; | ||
| this.render(); | ||
| } | ||
|
|
||
| render(): void { | ||
| this.$header.textContent = this.title; | ||
| this.$app.appendChild(this.$header); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| export default class TodoCounter { | ||
| constructor({ $app, initialState }) { | ||
| this.$counter = document.createElement('div'); | ||
| $app.appendChild(this.$counter); | ||
| this.state = initialState; | ||
| this.render(); | ||
| } | ||
| setState(nextState) { | ||
| this.state = nextState; | ||
| this.render(); | ||
| } | ||
| render() { | ||
| this.$counter.innerHTML = ` | ||
| <div class="todo-counter">Total: ${this.state.length}</div> | ||
| <div class="done-counter">Done: ${this.state.filter((e) => e.isCompleted).length}</div> | ||
| `; | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { Todo, TodoCounterProps } from '../util/types.js'; | ||
|
|
||
| export default class TodoCounter { | ||
| state: Todo[]; | ||
| $counter: HTMLDivElement; | ||
|
|
||
| constructor({ $app, initialState }: TodoCounterProps) { | ||
| this.$counter = document.createElement('div'); | ||
| $app.appendChild(this.$counter); | ||
| this.state = initialState; | ||
| this.render(); | ||
| } | ||
|
|
||
| setState(nextState: Todo[]): void { | ||
| this.state = nextState; | ||
| this.render(); | ||
| } | ||
|
|
||
| render(): void { | ||
| this.$counter.innerHTML = ` | ||
| <div class="todo-counter">Total: ${this.state.length}</div> | ||
| <div class="done-counter">Done: ${ | ||
| this.state.filter((e) => e.isCompleted).length | ||
| }</div> | ||
| `; | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { setItem } from '../util/storage.js'; | ||
| const STORAGE_KEY = 'todo'; | ||
| export default class TodoList { | ||
| constructor({ $app, todoInitialState, updateTodoCounter }) { | ||
| this.$todoList = document.createElement('div'); | ||
| $app.appendChild(this.$todoList); | ||
| this.updateTodoCounter = updateTodoCounter; | ||
| this.state = todoInitialState; | ||
| this.setEvent(); | ||
| this.render(); | ||
| } | ||
| completedTodo(id) { | ||
| const nextState = this.state.map((todo) => { | ||
| if (todo.id === id) { | ||
| return { | ||
| ...todo, | ||
| isCompleted: !todo.isCompleted, | ||
| }; | ||
| } | ||
| return todo; | ||
| }); | ||
| this.setState(nextState); | ||
| } | ||
| removeTodo(id) { | ||
| const nextState = this.state.filter((todo) => todo.id !== id); | ||
| this.setState(nextState); | ||
| } | ||
| setState(nextState) { | ||
| setItem(STORAGE_KEY, JSON.stringify(nextState)); | ||
| this.updateTodoCounter(nextState); | ||
| this.state = nextState; | ||
| this.render(); | ||
| } | ||
| render() { | ||
| this.$todoList.innerHTML = ` | ||
| <ul> | ||
| ${this.state | ||
| .map(({ title, isCompleted, id }) => { | ||
| return ` | ||
| <li class="toggled-text" data-id="${id}" | ||
| style="text-decoration: ${isCompleted ? 'line-through' : 'none'}"> | ||
| ${title} | ||
| <button class="remove-button" data-id="${id}"> | ||
| Remove | ||
| </button> | ||
| </li> | ||
| `; | ||
| }) | ||
| .join('')} | ||
| </ul> | ||
| `; | ||
| } | ||
| setEvent() { | ||
| this.$todoList.addEventListener('click', (event) => { | ||
| const target = event.target; | ||
| const id = target.dataset.id; | ||
| if (id !== null) { | ||
| target.className === 'toggled-text' && this.completedTodo(id); | ||
| target.className === 'remove-button' && this.removeTodo(id); | ||
| } | ||
| }); | ||
| } | ||
| } |
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.
id를 따로 날짜로 주셨군요? 좋은 방법이라고 생각됩니다!