-
Notifications
You must be signed in to change notification settings - Fork 0
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
Improving components & Todo #3
Open
Rehan1579
wants to merge
15
commits into
main
Choose a base branch
from
improving-components
base: main
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
15 commits
Select commit
Hold shift + click to select a range
c1d8d0f
installed axios
Rehan1579 d547d59
disable strictNullChecks
Rehan1579 842aed5
move ITodo to interface folder
Rehan1579 9a55c99
create TodoForm component
Rehan1579 e31c533
installed styled-components
Rehan1579 064a47c
Delete TodoForm.scss
Rehan1579 9748058
turn form into styled component
Rehan1579 9409271
update styling
Rehan1579 1abbec1
Create todo-context.ts
Rehan1579 aec1400
Update TodoList.tsx
Rehan1579 5828a7e
move Todo Logic into TodoService
Rehan1579 c83d652
remove unnecessary custom hook
Rehan1579 8ed5956
update ITodoListContextValue to ITodoListContext
Rehan1579 49540d6
use TodoContext in TodoItem
Rehan1579 b0542f9
update TODO
Rehan1579 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,17 +1,41 @@ | ||
import "./TodoItem.scss"; | ||
import { TodoContextController } from "../../data-models/context/todo-context"; | ||
import { ITodo } from "../../data-models/interfaces/ITodo"; | ||
import { useContext } from "react"; | ||
|
||
|
||
interface ITodoItemProps { | ||
todo: ITodo; | ||
} | ||
|
||
|
||
export const TodoItem = (props: ITodoItemProps) => { | ||
|
||
const { todo } = props; | ||
|
||
const context = TodoContextController.getContext(); | ||
const todoContext = useContext(context); | ||
|
||
|
||
function onToggleTodo(id: string, checked: boolean) { | ||
todoContext.toggleTodo(id, checked); | ||
} | ||
|
||
|
||
function onRemoveTodo(id: string): void { | ||
todoContext.removeTodo(id); | ||
} | ||
|
||
export const TodoItem = (props: any) => { | ||
const { todo, removeTodo, toggleTodo } = props; | ||
|
||
return ( | ||
<li className="todo-item"> | ||
<label className="checkcontainer" style={{ textDecoration: todo.complete ? "line-through" : "none" }}> | ||
<input type="checkbox" checked={todo.complete} onChange={(e) => toggleTodo(todo.id, e.target.checked)}></input> | ||
<input type="checkbox" checked={todo.complete} onChange={e => onToggleTodo(todo.id, e.target.checked)}></input> | ||
{todo.title} | ||
<span className="checkmark"></span> | ||
</label> | ||
|
||
<button onClick={() => removeTodo(todo.id)}>Remove</button> | ||
<button onClick={_ => onRemoveTodo(todo.id)}>Remove</button> | ||
</li> | ||
); | ||
}; |
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,9 +1,12 @@ | ||
.todo-list { | ||
list-style-type: none; | ||
padding: 0; | ||
|
||
height: calc(100% - 51px); | ||
background: white; | ||
margin: 0; | ||
overflow-y: auto; | ||
|
||
&--empty { | ||
text-align: center; | ||
text-align: center; | ||
} | ||
} |
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,17 +1,23 @@ | ||
import { ITodo } from "../../data-models/ITodo"; | ||
import { ITodo } from "../../data-models/interfaces/ITodo"; | ||
import { TodoItem } from "../todo-item/TodoItem"; | ||
import "./TodoList.scss"; | ||
|
||
export const TodoList = (props: any) => { | ||
const { todoItems, removeTodo, toggleTodo } = props; | ||
|
||
return ( | ||
<ul className="todo-list"> | ||
{todoItems.length == 0 && <p className="todo-list--empty">No Todos</p>} | ||
interface ITodoListProps { | ||
todoItems?: ITodo[]; | ||
} | ||
|
||
{todoItems.map((item: ITodo) => { | ||
return <TodoItem key={item.id} todo={item} removeTodo={removeTodo} toggleTodo={toggleTodo} />; | ||
})} | ||
</ul> | ||
); | ||
|
||
export const TodoList = (props: ITodoListProps) => { | ||
const { todoItems } = props; | ||
|
||
const hasTodo = todoItems && todoItems.length > 0; | ||
|
||
const noTodos = <p className="todo-list--empty">No Todos</p>; | ||
|
||
const todos = todoItems?.map((item: ITodo) => { | ||
return <TodoItem key={item.id} todo={item} />; | ||
}); | ||
|
||
return <ul className="todo-list">{hasTodo ? todos : noTodos}</ul>; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { createContext } from "react"; | ||
|
||
|
||
|
||
export interface ITodoListContext { | ||
Provider?: React.Provider<ITodoListContext>; | ||
removeTodo(id: string): void; | ||
toggleTodo(id: string, completed: boolean): void; | ||
} | ||
|
||
|
||
|
||
export const TodoContextController = (() => { | ||
|
||
let context: React.Context<ITodoListContext> | undefined; | ||
|
||
|
||
|
||
function initializeContext() { | ||
return createContext(undefined); | ||
} | ||
|
||
|
||
|
||
function getContext() { | ||
|
||
if (!context) | ||
{ | ||
context = initializeContext(); | ||
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. how about we directly call |
||
} | ||
return context; | ||
} | ||
|
||
|
||
return { | ||
getContext, | ||
}; | ||
})(); |
File renamed without changes.
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
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.
no catch block to handle promise rejection?