-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
dev #783
Open
nikkias
wants to merge
6
commits into
mate-academy:master
Choose a base branch
from
nikkias:dev
base: master
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
dev #783
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,71 @@ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
import { TodosContext } from './components/TodosContext'; | ||
import { Todo } from './types/Todo'; | ||
import { TodoContent } from './components/TodoContent'; | ||
import { useLocalStorage } from './hooks/useLocalStorage'; | ||
import { TodoFooter } from './components/TodoFooter'; | ||
import { TodosFilter } from './types/TodosFilter'; | ||
|
||
export const App: React.FC = () => { | ||
const [query, setQuery] = useState(''); | ||
const [todos, setTodos] = useLocalStorage<Todo[]>('todos', []) as [ | ||
Todo[], | ||
React.Dispatch<React.SetStateAction<Todo[]>>, | ||
]; | ||
const [todosFilter, setTodosFilter] = useState<TodosFilter>(TodosFilter.all); | ||
const [todoEditId, setTodoEditId] = useState<number>(0); | ||
const [todoEdit, setTodoEdit] = useState<string>(''); | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
if (query.trim()) { | ||
const todo = { | ||
id: Date.now(), | ||
title: query, | ||
completed: false, | ||
}; | ||
|
||
setTodos((prevTodos: Todo[]) => [...prevTodos, todo]); | ||
setQuery(''); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="todoapp"> | ||
<header className="header"> | ||
<h1>todos</h1> | ||
|
||
<form> | ||
<form onSubmit={handleSubmit}> | ||
<input | ||
type="text" | ||
data-cy="createTodo" | ||
className="new-todo" | ||
placeholder="What needs to be done?" | ||
value={query} | ||
onChange={(event) => setQuery(event.target.value)} | ||
/> | ||
</form> | ||
</header> | ||
|
||
<section className="main"> | ||
<input | ||
type="checkbox" | ||
id="toggle-all" | ||
className="toggle-all" | ||
data-cy="toggleAll" | ||
/> | ||
<label htmlFor="toggle-all">Mark all as complete</label> | ||
|
||
<ul className="todo-list" data-cy="todoList"> | ||
<li> | ||
<div className="view"> | ||
<input type="checkbox" className="toggle" id="toggle-view" /> | ||
<label htmlFor="toggle-view">asdfghj</label> | ||
<button type="button" className="destroy" data-cy="deleteTodo" /> | ||
</div> | ||
<input type="text" className="edit" /> | ||
</li> | ||
|
||
<li className="completed"> | ||
<div className="view"> | ||
<input type="checkbox" className="toggle" id="toggle-completed" /> | ||
<label htmlFor="toggle-completed">qwertyuio</label> | ||
<button type="button" className="destroy" data-cy="deleteTodo" /> | ||
</div> | ||
<input type="text" className="edit" /> | ||
</li> | ||
|
||
<li className="editing"> | ||
<div className="view"> | ||
<input type="checkbox" className="toggle" id="toggle-editing" /> | ||
<label htmlFor="toggle-editing">zxcvbnm</label> | ||
<button type="button" className="destroy" data-cy="deleteTodo" /> | ||
</div> | ||
<input type="text" className="edit" /> | ||
</li> | ||
|
||
<li> | ||
<div className="view"> | ||
<input type="checkbox" className="toggle" id="toggle-view2" /> | ||
<label htmlFor="toggle-view2">1234567890</label> | ||
<button type="button" className="destroy" data-cy="deleteTodo" /> | ||
</div> | ||
<input type="text" className="edit" /> | ||
</li> | ||
</ul> | ||
</section> | ||
|
||
<footer className="footer"> | ||
<span className="todo-count" data-cy="todosCounter"> | ||
3 items left | ||
</span> | ||
|
||
<ul className="filters"> | ||
<li> | ||
<a href="#/" className="selected">All</a> | ||
</li> | ||
|
||
<li> | ||
<a href="#/active">Active</a> | ||
</li> | ||
|
||
<li> | ||
<a href="#/completed">Completed</a> | ||
</li> | ||
</ul> | ||
|
||
<button type="button" className="clear-completed"> | ||
Clear completed | ||
</button> | ||
</footer> | ||
<TodosContext.Provider | ||
value={{ | ||
todos, | ||
todosFilter, | ||
todoEditId, | ||
todoEdit, | ||
setTodos, | ||
setTodosFilter, | ||
setTodoEditId, | ||
setTodoEdit, | ||
}} | ||
> | ||
{todos.length > 0 && ( | ||
<> | ||
<TodoContent /> | ||
<TodoFooter /> | ||
</> | ||
)} | ||
</TodosContext.Provider> | ||
</div> | ||
); | ||
}; |
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,46 @@ | ||
import React, { useContext } from 'react'; | ||
import cn from 'classnames'; | ||
import { TodosContext } from './TodosContext'; | ||
import { TodoList } from './TodoList'; | ||
import { Todo } from '../types/Todo'; | ||
|
||
export const TodoContent: React.FC = () => { | ||
const context = useContext(TodosContext); | ||
|
||
if (!context) { | ||
return null; | ||
} | ||
|
||
const { todos, setTodos } = context; | ||
|
||
const isCompleted = todos.every(todo => todo.completed); | ||
|
||
const toggleAll = () => { | ||
setTodos((prevTodos: Todo[]) => prevTodos.map(todo => ({ | ||
...todo, | ||
completed: !todo.completed, | ||
}))); | ||
}; | ||
|
||
return ( | ||
<section className="main"> | ||
<input | ||
data-cy="toggleAll" | ||
type="checkbox" | ||
id="toggle-all" | ||
className={cn( | ||
'toggle-all', | ||
{ active: isCompleted }, | ||
)} | ||
onClick={toggleAll} | ||
checked={isCompleted} | ||
/> | ||
|
||
<label htmlFor="toggle-all"> | ||
Mark all as complete | ||
</label> | ||
|
||
<TodoList /> | ||
</section> | ||
); | ||
}; |
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 React, { useContext } from 'react'; | ||
import cn from 'classnames'; | ||
import { TodosContext } from './TodosContext'; | ||
import { TodosFilter } from '../types/TodosFilter'; | ||
|
||
export const TodoFilter: React.FC = () => { | ||
const context = useContext(TodosContext); | ||
|
||
if (!context) { | ||
return null; | ||
} | ||
|
||
const { todosFilter, setTodosFilter } = context; | ||
|
||
const handleLink = (val: string) => { | ||
if (TodosFilter.all) { | ||
return '#/'; | ||
} | ||
|
||
return `#/${val.toLowerCase()}`; | ||
}; | ||
|
||
return ( | ||
<ul className="filters" data-cy="todosFilter"> | ||
{Object.values(TodosFilter).map(val => ( | ||
<li key={val}> | ||
<a | ||
href={handleLink(val)} | ||
onClick={() => setTodosFilter(val)} | ||
className={cn({ selected: todosFilter === val })} | ||
> | ||
{val} | ||
</a> | ||
</li> | ||
))} | ||
</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,47 @@ | ||
import React, { useContext } from 'react'; | ||
import { TodosContext } from './TodosContext'; | ||
import { TodoFilter } from './TodoFilter'; | ||
import { Todo } from '../types/Todo'; | ||
|
||
export const TodoFooter: React.FC = () => { | ||
const context = useContext(TodosContext); | ||
|
||
if (!context) { | ||
return null; | ||
} | ||
|
||
const { todos, setTodos } = context; | ||
|
||
const clearComplete = () => { | ||
setTodos((prevTodos: Todo[]) => { | ||
const updatedTodos = prevTodos.filter(todo => !todo.completed); | ||
|
||
return updatedTodos; | ||
}); | ||
}; | ||
|
||
const isCompleted = todos.some(todo => todo.completed); | ||
|
||
const todosLeft = todos.filter(todo => !todo.completed).length; | ||
|
||
return ( | ||
<footer className="footer"> | ||
<span className="todo-count" data-cy="todosCounter"> | ||
{`${todosLeft} items left`} | ||
</span> | ||
|
||
<TodoFilter /> | ||
|
||
{isCompleted | ||
&& ( | ||
<button | ||
type="button" | ||
className="clear-completed" | ||
onClick={clearComplete} | ||
> | ||
Clear completed | ||
</button> | ||
)} | ||
</footer> | ||
); | ||
}; |
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.
Move provider to the context file, it will be a good practice