-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1536f7
commit 56e7e61
Showing
16 changed files
with
580 additions
and
252 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Action } from './types/Action'; | ||
import { Filter } from './types/Filter'; | ||
import { State } from './types/State'; | ||
import React, { useEffect, useReducer } from 'react'; | ||
|
||
const reducer = (state: State, action: Action): State => { | ||
switch (action.type) { | ||
case 'addTodo': | ||
return { | ||
...state, | ||
todos: [...state.todos, action.payload], | ||
}; | ||
|
||
case 'deleteTodo': | ||
return { | ||
...state, | ||
todos: state.todos.filter(todo => todo.id !== action.payload), | ||
}; | ||
|
||
case 'updateTodo': | ||
return { | ||
...state, | ||
todos: state.todos.map(todo => | ||
todo.id === action.payload.id ? action.payload : todo, | ||
), | ||
}; | ||
|
||
case 'setFilter': | ||
return { | ||
...state, | ||
filter: action.payload, | ||
}; | ||
|
||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const loadedTodos = localStorage.getItem('todos'); | ||
const initialState: State = { | ||
todos: loadedTodos ? JSON.parse(loadedTodos) : [], | ||
filter: Filter.All, | ||
}; | ||
|
||
export const StateContext = React.createContext<State>(initialState); | ||
export const DispatchContext = React.createContext<React.Dispatch<Action>>( | ||
() => {}, | ||
); | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export const GlobalProvider: React.FC<Props> = ({ children }) => { | ||
const [state, dispatch] = useReducer(reducer, initialState); | ||
|
||
useEffect(() => { | ||
localStorage.setItem('todos', JSON.stringify(state.todos)); | ||
}, [state.todos]); | ||
|
||
return ( | ||
<DispatchContext.Provider value={dispatch}> | ||
<StateContext.Provider value={state}>{children}</StateContext.Provider> | ||
</DispatchContext.Provider> | ||
); | ||
}; |
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,64 @@ | ||
import { useContext, useMemo } from 'react'; | ||
import { DispatchContext, StateContext } from '../GlobalProvider'; | ||
import { Filter } from '../types/Filter'; | ||
import classNames from 'classnames'; | ||
|
||
export const Footer = () => { | ||
const { todos, filter } = useContext(StateContext); | ||
const dispatch = useContext(DispatchContext); | ||
|
||
const activeTodosCount = useMemo( | ||
() => todos.filter(todo => !todo.completed).length, | ||
[todos], | ||
); | ||
|
||
const deleteCompleted = () => { | ||
todos.forEach(todo => { | ||
if (todo.completed) { | ||
dispatch({ type: 'deleteTodo', payload: todo.id }); | ||
} | ||
}); | ||
}; | ||
|
||
if (!todos.length) { | ||
return; | ||
} | ||
|
||
return ( | ||
<> | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{`${activeTodosCount} items left`} | ||
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
{Object.values(Filter).map(currFilter => ( | ||
<a | ||
href={`#/${currFilter === Filter.All ? '' : currFilter.toLowerCase()}`} | ||
className={classNames('filter__link', { | ||
selected: currFilter === filter, | ||
})} | ||
data-cy={`FilterLink${currFilter}`} | ||
key={currFilter} | ||
onClick={() => { | ||
dispatch({ type: 'setFilter', payload: currFilter }); | ||
}} | ||
> | ||
{currFilter} | ||
</a> | ||
))} | ||
</nav> | ||
|
||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
disabled={activeTodosCount === todos.length} | ||
onClick={deleteCompleted} | ||
> | ||
Clear completed | ||
</button> | ||
</footer> | ||
</> | ||
); | ||
}; |
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,91 @@ | ||
import { useContext, useEffect, useMemo, useRef, useState } from 'react'; | ||
import { DispatchContext, StateContext } from '../GlobalProvider'; | ||
import { Todo } from '../types/Todo'; | ||
import classNames from 'classnames'; | ||
|
||
export const Header = () => { | ||
const [title, setTitle] = useState(''); | ||
|
||
const titleField = useRef<HTMLInputElement>(null); | ||
|
||
const { todos } = useContext(StateContext); | ||
const dispatch = useContext(DispatchContext); | ||
|
||
useEffect(() => { | ||
if (titleField.current) { | ||
titleField.current.focus(); | ||
} | ||
}, [todos.length]); | ||
|
||
const handleSubmit = (e: React.FormEvent) => { | ||
e.preventDefault(); | ||
|
||
if (!title.trim()) { | ||
return; | ||
} | ||
|
||
const newTodo: Todo = { | ||
id: Date.now(), | ||
title: title.trim(), | ||
completed: false, | ||
}; | ||
|
||
dispatch({ type: 'addTodo', payload: newTodo }); | ||
setTitle(''); | ||
}; | ||
|
||
const handleTitleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setTitle(e.target.value); | ||
}; | ||
|
||
const allTodosCompleted = useMemo(() => { | ||
return todos.filter(todo => todo.completed).length === todos.length; | ||
}, [todos]); | ||
|
||
const handleToggleAllButtonClick = () => { | ||
let todosToChange = []; | ||
|
||
if (allTodosCompleted) { | ||
todosToChange = [...todos]; | ||
} else { | ||
todosToChange = todos.filter(todo => !todo.completed); | ||
} | ||
|
||
todosToChange.forEach(todo => { | ||
const { id, title: todoTitle, completed } = todo; | ||
|
||
dispatch({ | ||
type: 'updateTodo', | ||
payload: { id, title: todoTitle, completed: !completed }, | ||
}); | ||
}); | ||
}; | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{!!todos.length && ( | ||
<button | ||
type="button" | ||
className={classNames('todoapp__toggle-all', { | ||
active: allTodosCompleted, | ||
})} | ||
data-cy="ToggleAllButton" | ||
onClick={handleToggleAllButtonClick} | ||
/> | ||
)} | ||
|
||
<form onSubmit={handleSubmit}> | ||
<input | ||
ref={titleField} | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={title} | ||
onChange={handleTitleChange} | ||
autoFocus | ||
/> | ||
</form> | ||
</header> | ||
); | ||
}; |
Oops, something went wrong.