Repository navigation
Jawad A. #2
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
base: main
Are you sure you want to change the base?
Jawad A. #2
Changes from all commits
10628e5
957190d
0e81a04
2036985
b7c2546
79495e4
599ea76
330401c
aff776d
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,16 +1,17 @@ | ||
| import { TaskManager } from "./task-manager.js"; | ||
| import { TaskStore } from "./store.js"; | ||
| import { AppView } from "./views/app-view.js"; | ||
| import { FilterStatus, TaskHandlers } from "./types.js"; | ||
|
|
||
| const taskManager = new TaskManager(new TaskStore()); | ||
| let currentFilter = "all"; | ||
| let appView; | ||
| let currentFilter: FilterStatus = "all"; | ||
| let appView: AppView; | ||
|
|
||
| function render() { | ||
| appView.render(taskManager.getFiltered(currentFilter), currentFilter); | ||
| } | ||
|
|
||
| const handlers = { | ||
| const handlers: TaskHandlers = { | ||
| onAdd(title, description) { | ||
| taskManager.addTask(title, description); | ||
| render(); | ||
|
Comment on lines
15
to
17
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. All parameters doesn't have a type in the methods |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| export interface Task { | ||
| id: string; | ||
| title: string; | ||
| description: string; | ||
| completed: boolean; | ||
|
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. I would add also here |
||
| } | ||
|
|
||
| export type FilterStatus = "all" | "completed" | "pending"; | ||
|
|
||
| export interface TaskHandlers { | ||
| onAdd(title: string, description: string): void; | ||
| onToggle(id: string): void; | ||
| onDelete(id: string): void; | ||
| onEdit(id: string, title: string, description: string): void; | ||
| onFilter(status: FilterStatus): void; | ||
| } | ||
|
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. Good approach on creating |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| import { FilterStatus, Task, TaskHandlers } from "../types.js"; | ||
| import { TaskListView } from "./task-list-view.js"; | ||
|
|
||
| export class AppView { | ||
| constructor(root, handlers) { | ||
| filterBar: HTMLDivElement; | ||
| taskListView: TaskListView; | ||
|
Comment on lines
+5
to
+6
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. I recommend the reading for encapsulation in TypeScript and how to protect the objects |
||
| constructor(root: HTMLElement, handlers: TaskHandlers) { | ||
| root.innerHTML = String.raw` | ||
| <h1>To-Do List</h1> | ||
| <form class="todo-form"> | ||
|
|
@@ -17,11 +20,11 @@ export class AppView { | |
| <ul class="task-list"></ul> | ||
| `; | ||
|
|
||
| const form = root.querySelector(".todo-form"); | ||
| const titleInput = root.querySelector("#title-input"); | ||
| const descInput = root.querySelector("#desc-input"); | ||
| const taskListEl = root.querySelector(".task-list"); | ||
| this.filterBar = root.querySelector(".filter-bar"); | ||
| const form = root.querySelector<HTMLFormElement>(".todo-form")!; | ||
| const titleInput = root.querySelector<HTMLInputElement>("#title-input")!; | ||
| const descInput = root.querySelector<HTMLInputElement>("#desc-input")!; | ||
| const taskListEl = root.querySelector<HTMLUListElement>(".task-list")!; | ||
| this.filterBar = root.querySelector<HTMLDivElement>(".filter-bar")!; | ||
|
|
||
| this.taskListView = new TaskListView(taskListEl, handlers); | ||
|
|
||
|
|
@@ -33,20 +36,23 @@ export class AppView { | |
| form.reset(); | ||
| }); | ||
|
|
||
| for (const btn of root.querySelectorAll(".filter-bar button")) { | ||
| for (const btn of root.querySelectorAll<HTMLButtonElement>( | ||
| ".filter-bar button", | ||
| )) { | ||
| btn.addEventListener("click", () => { | ||
| handlers.onFilter(btn.dataset.filter); | ||
| handlers.onFilter(btn.dataset.filter as FilterStatus); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| render(tasks, activeFilter) { | ||
| render(tasks: Task[], activeFilter: FilterStatus) { | ||
| this.taskListView.render(tasks); | ||
| this.updateFilterButtons(activeFilter); | ||
| } | ||
|
|
||
| updateFilterButtons(activeFilter) { | ||
| const buttons = this.filterBar.querySelectorAll("[data-filter]"); | ||
| updateFilterButtons(activeFilter: FilterStatus) { | ||
| const buttons = | ||
| this.filterBar.querySelectorAll<HTMLButtonElement>("[data-filter]"); | ||
| for (const btn of buttons) { | ||
| btn.classList.toggle("active", btn.dataset.filter === activeFilter); | ||
| } | ||
|
Comment on lines
+48
to
58
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. Both methods doesn't have a return type |
||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { FilterStatus, Task, TaskHandlers } from "../types.js"; | ||
| import { escapeHtml } from "./utils.js"; | ||
|
|
||
| export function enterEditMode( | ||
| li: HTMLLIElement, | ||
| task: Task, | ||
| handlers: TaskHandlers, | ||
| ) { | ||
| li.innerHTML = String.raw` | ||
| <input type="checkbox"${task.completed ? " checked" : ""}> | ||
| <div class="edit-form"> | ||
| <input type="text" class="edit-title" value="${escapeHtml(task.title)}"> | ||
| <input type="text" class="edit-desc" value="${escapeHtml(task.description)}"> | ||
| <div class="edit-actions"> | ||
| <button class="save-btn">Save</button> | ||
| <button class="cancel-btn">Cancel</button> | ||
| </div> | ||
| </div> | ||
| `; | ||
|
|
||
| const checkbox = li.querySelector<HTMLInputElement>( | ||
| "input[type='checkbox']", | ||
| )!; | ||
| const titleInput = li.querySelector<HTMLInputElement>(".edit-title")!; | ||
| const descInput = li.querySelector<HTMLInputElement>(".edit-desc")!; | ||
|
|
||
| checkbox.addEventListener("change", () => handlers.onToggle(task.id)); | ||
|
|
||
| li.querySelector<HTMLButtonElement>(".save-btn")!.addEventListener( | ||
| "click", | ||
| () => { | ||
| const newTitle = titleInput.value.trim(); | ||
| if (!newTitle) return; | ||
| handlers.onEdit(task.id, newTitle, descInput.value.trim()); | ||
| }, | ||
| ); | ||
|
|
||
| li.querySelector<HTMLButtonElement>(".cancel-btn")!.addEventListener( | ||
| "click", | ||
| () => { | ||
| const activeFilter = | ||
| (document.querySelector<HTMLButtonElement>(".filter-bar .active") | ||
| ?.dataset.filter as FilterStatus) ?? "all"; | ||
| handlers.onFilter(activeFilter); | ||
| }, | ||
| ); | ||
|
|
||
| titleInput.focus(); | ||
| } | ||
|
Comment on lines
+4
to
+49
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. This is a common pattern, even the function is no return, add |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,16 @@ | ||
| import { enterEditMode } from "./task-edit-view.js"; | ||
| import { escapeHtml } from "./utils.js"; | ||
| import { Task, TaskHandlers } from "../types.js"; | ||
|
|
||
| export class TaskListView { | ||
| constructor(root, handlers) { | ||
| root: HTMLUListElement; | ||
| handlers: TaskHandlers; | ||
| constructor(root: HTMLUListElement, handlers: TaskHandlers) { | ||
| this.root = root; | ||
| this.handlers = handlers; | ||
| } | ||
|
|
||
| render(tasks) { | ||
| render(tasks: Task[]): void { | ||
| if (tasks.length === 0) { | ||
| this.root.innerHTML = `<li class="empty-state">No tasks to show.</li>`; | ||
| return; | ||
|
|
@@ -31,21 +34,24 @@ export class TaskListView { | |
| ) | ||
| .join(""); | ||
|
|
||
| for (const li of this.root.querySelectorAll(".task-item")) { | ||
| const id = li.dataset.id; | ||
| const task = tasks.find((t) => t.id === id); | ||
| for (const li of this.root.querySelectorAll<HTMLLIElement>(".task-item")) { | ||
| const id = (li as HTMLLIElement).dataset.id!; | ||
|
Comment on lines
+37
to
+38
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. The cast in line 38 is redudant since you are already specifying that you want a list element in line 37 |
||
| const task = tasks.find((t) => t.id === id)!; | ||
|
|
||
| li.querySelector("input[type='checkbox']").addEventListener( | ||
| "change", | ||
| () => this.handlers.onToggle(id), | ||
| ); | ||
| li.querySelector<HTMLInputElement>( | ||
| "input[type='checkbox']", | ||
| )!.addEventListener("change", () => this.handlers.onToggle(id)); | ||
|
|
||
| li.querySelector(".edit-btn").addEventListener("click", () => { | ||
| enterEditMode(li, task, this.handlers); | ||
| }); | ||
| li.querySelector<HTMLButtonElement>(".edit-btn")!.addEventListener( | ||
| "click", | ||
| () => { | ||
| enterEditMode(li, task, this.handlers); | ||
| }, | ||
| ); | ||
|
|
||
| li.querySelector(".delete-btn").addEventListener("click", () => | ||
| this.handlers.onDelete(id), | ||
| li.querySelector<HTMLButtonElement>(".delete-btn")!.addEventListener( | ||
| "click", | ||
| () => this.handlers.onDelete(id), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Logs | ||
| logs | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| pnpm-debug.log* | ||
| lerna-debug.log* | ||
|
|
||
| node_modules | ||
| dist | ||
| dist-ssr | ||
| *.local | ||
|
|
||
| # Editor directories and files | ||
| .vscode/* | ||
| !.vscode/extensions.json | ||
| .idea | ||
| .DS_Store | ||
| *.suo | ||
| *.ntvs* | ||
| *.njsproj | ||
| *.sln | ||
| *.sw? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Task List</title> | ||
| <link rel="stylesheet" href="/src/style.css" /> | ||
| </head> | ||
| <body> | ||
| <div id="app"> | ||
| <h1>My Tasks</h1> | ||
|
|
||
| <form id="task-form"> | ||
| <input | ||
| type="text" | ||
| id="task-input" | ||
| placeholder="What needs to be done?" | ||
| required | ||
| /> | ||
| <button type="submit">Add task</button> | ||
| </form> | ||
|
|
||
| <ul id="task-list"></ul> | ||
|
|
||
| <p id="empty-message">No tasks yet. Add one above!</p> | ||
| </div> | ||
| <script type="module" src="/src/main.ts"></script> | ||
| </body> | ||
| </html> |
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.
No return type specified here