-
Notifications
You must be signed in to change notification settings - Fork 1.5k
implement solution #1310
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
whomngmnt
wants to merge
1
commit into
mate-academy:master
Choose a base branch
from
whomngmnt:develop
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
implement solution #1310
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 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 |
|---|---|---|
| @@ -1,157 +1,37 @@ | ||
| /* eslint-disable jsx-a11y/control-has-associated-label */ | ||
| import React from 'react'; | ||
| import { useContext } from 'react'; | ||
| import { Footer } from './components/Footer/Footer'; | ||
| import { Header } from './components/Header/Header'; | ||
| import { TodoContext, TodoProvider } from './components/Context/TodoContext'; | ||
| import { TodoList } from './components/TodoList/TodoList'; | ||
|
|
||
| const TodoApp = () => { | ||
| const context = useContext(TodoContext); | ||
|
|
||
| if (!context) { | ||
| return null; | ||
| } | ||
|
|
||
| const hasTodos = context.state.todos.length > 0; | ||
|
|
||
| export const App: React.FC = () => { | ||
| return ( | ||
| <div className="todoapp"> | ||
| <h1 className="todoapp__title">todos</h1> | ||
|
|
||
| <div className="todoapp__content"> | ||
| <header className="todoapp__header"> | ||
| {/* this button should have `active` class only if all todos are completed */} | ||
| <button | ||
| type="button" | ||
| className="todoapp__toggle-all active" | ||
| data-cy="ToggleAllButton" | ||
| /> | ||
|
|
||
| {/* Add a todo on form submit */} | ||
| <form> | ||
| <input | ||
| data-cy="NewTodoField" | ||
| type="text" | ||
| className="todoapp__new-todo" | ||
| placeholder="What needs to be done?" | ||
| /> | ||
| </form> | ||
| </header> | ||
|
|
||
| <section className="todoapp__main" data-cy="TodoList"> | ||
| {/* This is a completed todo */} | ||
| <div data-cy="Todo" className="todo completed"> | ||
| <label className="todo__status-label"> | ||
| <input | ||
| data-cy="TodoStatus" | ||
| type="checkbox" | ||
| className="todo__status" | ||
| checked | ||
| /> | ||
| </label> | ||
|
|
||
| <span data-cy="TodoTitle" className="todo__title"> | ||
| Completed Todo | ||
| </span> | ||
|
|
||
| {/* Remove button appears only on hover */} | ||
| <button type="button" className="todo__remove" data-cy="TodoDelete"> | ||
| × | ||
| </button> | ||
| </div> | ||
|
|
||
| {/* This todo is an active todo */} | ||
| <div data-cy="Todo" className="todo"> | ||
| <label className="todo__status-label"> | ||
| <input | ||
| data-cy="TodoStatus" | ||
| type="checkbox" | ||
| className="todo__status" | ||
| /> | ||
| </label> | ||
|
|
||
| <span data-cy="TodoTitle" className="todo__title"> | ||
| Not Completed Todo | ||
| </span> | ||
|
|
||
| <button type="button" className="todo__remove" data-cy="TodoDelete"> | ||
| × | ||
| </button> | ||
| </div> | ||
|
|
||
| {/* This todo is being edited */} | ||
| <div data-cy="Todo" className="todo"> | ||
| <label className="todo__status-label"> | ||
| <input | ||
| data-cy="TodoStatus" | ||
| type="checkbox" | ||
| className="todo__status" | ||
| /> | ||
| </label> | ||
|
|
||
| {/* This form is shown instead of the title and remove button */} | ||
| <form> | ||
| <input | ||
| data-cy="TodoTitleField" | ||
| type="text" | ||
| className="todo__title-field" | ||
| placeholder="Empty todo will be deleted" | ||
| value="Todo is being edited now" | ||
| /> | ||
| </form> | ||
| </div> | ||
|
|
||
| {/* This todo is in loadind state */} | ||
| <div data-cy="Todo" className="todo"> | ||
| <label className="todo__status-label"> | ||
| <input | ||
| data-cy="TodoStatus" | ||
| type="checkbox" | ||
| className="todo__status" | ||
| /> | ||
| </label> | ||
|
|
||
| <span data-cy="TodoTitle" className="todo__title"> | ||
| Todo is being saved now | ||
| </span> | ||
|
|
||
| <button type="button" className="todo__remove" data-cy="TodoDelete"> | ||
| × | ||
| </button> | ||
| </div> | ||
| </section> | ||
|
|
||
| {/* Hide the footer if there are no todos */} | ||
| <footer className="todoapp__footer" data-cy="Footer"> | ||
| <span className="todo-count" data-cy="TodosCounter"> | ||
| 3 items left | ||
| </span> | ||
|
|
||
| {/* Active link should have the 'selected' class */} | ||
| <nav className="filter" data-cy="Filter"> | ||
| <a | ||
| href="#/" | ||
| className="filter__link selected" | ||
| data-cy="FilterLinkAll" | ||
| > | ||
| All | ||
| </a> | ||
|
|
||
| <a | ||
| href="#/active" | ||
| className="filter__link" | ||
| data-cy="FilterLinkActive" | ||
| > | ||
| Active | ||
| </a> | ||
|
|
||
| <a | ||
| href="#/completed" | ||
| className="filter__link" | ||
| data-cy="FilterLinkCompleted" | ||
| > | ||
| Completed | ||
| </a> | ||
| </nav> | ||
|
|
||
| {/* this button should be disabled if there are no completed todos */} | ||
| <button | ||
| type="button" | ||
| className="todoapp__clear-completed" | ||
| data-cy="ClearCompletedButton" | ||
| > | ||
| Clear completed | ||
| </button> | ||
| </footer> | ||
| <Header /> | ||
| {hasTodos && ( | ||
| <> | ||
| <TodoList /> | ||
| <Footer /> | ||
| </> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const App = () => ( | ||
| <TodoProvider> | ||
| <TodoApp /> | ||
| </TodoProvider> | ||
| ); |
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,66 @@ | ||
| import { | ||
| createContext, | ||
| Dispatch, | ||
| ReactNode, | ||
| useEffect, | ||
| useReducer, | ||
| } from 'react'; | ||
| import { Todo } from '../../types/type'; | ||
| import { Action, todoReducer } from './TodoReducer'; | ||
|
|
||
| export type FilterStatus = 'all' | 'active' | 'completed'; | ||
|
|
||
| export type TodoState = { | ||
| todos: Todo[]; | ||
| filter: FilterStatus; | ||
| }; | ||
|
|
||
| type TodoContextValue = { | ||
| state: TodoState; | ||
| dispatch: Dispatch<Action>; | ||
| }; | ||
|
|
||
| const STORAGE_KEY = 'todos'; | ||
|
|
||
| const getInitialState = (): TodoState => { | ||
| const savedTodos = localStorage.getItem(STORAGE_KEY); | ||
|
|
||
| if (!savedTodos) { | ||
| return { | ||
| todos: [], | ||
| filter: 'all', | ||
| }; | ||
| } | ||
|
|
||
| try { | ||
| return { | ||
| todos: JSON.parse(savedTodos) as Todo[], | ||
| filter: 'all', | ||
| }; | ||
| } catch { | ||
| return { | ||
| todos: [], | ||
| filter: 'all', | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| export const TodoContext = createContext<TodoContextValue | null>(null); | ||
|
|
||
| type Props = { | ||
| children: ReactNode; | ||
| }; | ||
|
|
||
| export const TodoProvider = ({ children }: Props) => { | ||
| const [state, dispatch] = useReducer(todoReducer, undefined, getInitialState); | ||
|
|
||
| useEffect(() => { | ||
| localStorage.setItem(STORAGE_KEY, JSON.stringify(state.todos)); | ||
| }, [state.todos]); | ||
|
|
||
| return ( | ||
| <TodoContext.Provider value={{ state, dispatch }}> | ||
| {children} | ||
| </TodoContext.Provider> | ||
| ); | ||
| }; |
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,73 @@ | ||
| import type { FilterStatus, TodoState } from './TodoContext'; | ||
|
|
||
| type UpdateTodoPayload = { | ||
| id: number; | ||
| title?: string; | ||
| completed?: boolean; | ||
| }; | ||
|
|
||
| export type Action = | ||
| | { type: 'add'; payload: string } | ||
| | { type: 'delete'; payload: number } | ||
| | { type: 'update'; payload: UpdateTodoPayload } | ||
| | { type: 'setFilter'; payload: FilterStatus } | ||
| | { type: 'toggleAll' } | ||
| | { type: 'clearCompleted' }; | ||
|
|
||
| export const todoReducer = (state: TodoState, action: Action): TodoState => { | ||
| switch (action.type) { | ||
| case 'add': | ||
| return { | ||
| ...state, | ||
| todos: [ | ||
| ...state.todos, | ||
| { | ||
| id: +new Date(), | ||
| title: action.payload, | ||
| completed: false, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| case 'delete': | ||
| return { | ||
| ...state, | ||
| todos: state.todos.filter(todo => todo.id !== action.payload), | ||
| }; | ||
|
|
||
| case 'update': | ||
| return { | ||
| ...state, | ||
| todos: state.todos.map(todo => | ||
| todo.id === action.payload.id ? { ...todo, ...action.payload } : todo, | ||
| ), | ||
| }; | ||
|
|
||
| case 'setFilter': | ||
| return { | ||
| ...state, | ||
| filter: action.payload, | ||
| }; | ||
|
|
||
| case 'toggleAll': { | ||
| const shouldCompleteAll = state.todos.some(todo => !todo.completed); | ||
|
|
||
| return { | ||
| ...state, | ||
| todos: state.todos.map(todo => ({ | ||
| ...todo, | ||
| completed: shouldCompleteAll, | ||
| })), | ||
| }; | ||
| } | ||
|
|
||
| case 'clearCompleted': | ||
| return { | ||
| ...state, | ||
| todos: state.todos.filter(todo => !todo.completed), | ||
| }; | ||
|
|
||
| default: | ||
| return state; | ||
| } | ||
| }; | ||
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,38 @@ | ||
| import { useContext } from 'react'; | ||
| import { FilterStatus, TodoContext } from '../Context/TodoContext'; | ||
|
|
||
| const filters: { label: string; value: FilterStatus; dataCy: string }[] = [ | ||
|
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. Also you can use Enum for value in this case. |
||
| { label: 'All', value: 'all', dataCy: 'FilterLinkAll' }, | ||
| { label: 'Active', value: 'active', dataCy: 'FilterLinkActive' }, | ||
| { label: 'Completed', value: 'completed', dataCy: 'FilterLinkCompleted' }, | ||
| ]; | ||
|
|
||
| export const Filter = () => { | ||
| const context = useContext(TodoContext); | ||
|
|
||
| if (!context) { | ||
| return null; | ||
| } | ||
|
|
||
| const { filter } = context.state; | ||
| const { dispatch } = context; | ||
|
|
||
| return ( | ||
| <nav className="filter" data-cy="Filter"> | ||
| {filters.map(item => ( | ||
| <a | ||
| key={item.value} | ||
| href={`#/${item.value === 'all' ? '' : item.value}`} | ||
| className={`filter__link ${filter === item.value ? 'selected' : ''}`} | ||
| data-cy={item.dataCy} | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
| dispatch({ type: 'setFilter', payload: item.value }); | ||
| }} | ||
| > | ||
| {item.label} | ||
| </a> | ||
| ))} | ||
| </nav> | ||
| ); | ||
| }; | ||
File renamed without changes.
Oops, something went wrong.
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.
You also can describe Actions as Enum (not critical)