Skip to content
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

Created Todo app #679

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Created Todo app #679

wants to merge 13 commits into from

Conversation

davinnchii
Copy link

Copy link

@BudnikOleksii BudnikOleksii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work 👍
Left some comments to improve your solution.
Use functional setState and array iteration methods to update your todos

Comment on lines 15 to 16
// [JSON.parse(localStorage.getItem('todos')
// || '{}')] as Todo[],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comments

type Props = {};

export const TodoApp: React.FC<Props> = () => {
const { setTodos } = useContext(TodosContext);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a hook for your context instead of passing TodosContext every time

const useTodos = useContext(TodosContext);

Comment on lines 20 to 21
setTodos((prev: Todo[]) => [...prev,
{ id: uuidv4(), title: newTodoTitle, completed: false }]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
setTodos((prev: Todo[]) => [...prev,
{ id: uuidv4(), title: newTodoTitle, completed: false }]);
setTodos((prev: Todo[]) => [
...prev,
{ id: uuidv4(), title: newTodoTitle, completed: false }
]);

Comment on lines 16 to 24
const index = todos.findIndex(({ id }) => id === todo.id);

const handleRemoveTodo = () => {
const todosCopy = [...todos];

todosCopy.splice(index, 1);

setTodos(todosCopy);
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use splice in this case, use filter

setTodos(previousTodos => previousTodos.filter(
  currentTodo => todo.id !== currentTodo.id
));

Comment on lines 26 to 34
const handleCompleteTodo = (
event: React.ChangeEvent<HTMLInputElement>,
) => {
const todosCopy = [...todos];

todosCopy.splice(index, 1, { ...todo, completed: event.target.checked });

setTodos(todosCopy);
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use map instead of splice to change your todo

Comment on lines 36 to 46
const handleEditTodo = (
title: string,
) => {
const todosCopy = [...todos];

if (title && title !== todo.title) {
todosCopy.splice(index, 1, { ...todo, title });

setTodos(todosCopy);
setIsEditingEnabled(false);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same, don't use splice

export const TodoList = () => {
const { todos, setTodos } = useContext(TodosContext);

const [filterParam, setFilterParam] = useState('All');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create an enum for your filter types, don't pass just strings

Comment on lines 13 to 15
const todosCopy = [...todos].map(todo => ({
...todo, completed: event.target.checked,
}));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map creates a new array, you don't need to create a copy.
Also, use functional setState

Comment on lines 35 to 45
{todos.filter(todo => {
switch (filterParam) {
case ('All'):
return true;
case ('Active'):
return !todo.completed;
case ('Completed'):
return todo.completed;
default:
return true;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move it outside jsx, create a handler or a helper function for this, or just store filteredTodos in a variable(you can use useMemo for this variable if you want)

Comment on lines 10 to 14
enum Status {
All = 'All',
Active = 'Active',
Completed = 'Completed',
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you have the enum but don't use it in TodoList component 🤷‍♂️

Copy link

@roman-mirzoian roman-mirzoian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be more attentive to the comments and if you update some part of the code, take a look, maybe you should remove something extra.

Comment on lines 41 to 45
export const useTodos = () => {
const todos = useContext(TodosContext);

return todos;
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const useTodos = () => {
const todos = useContext(TodosContext);
return todos;
};
export const useTodos = () => {
return useContext(TodosContext);
};

Comment on lines 18 to 24
const handleRemoveTodo = () => {
const todosCopy = [...todos];

todosCopy.splice(index, 1);

setTodos(prev => prev.filter(({ id }) => id !== todo.id));
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#679 (comment) - not fully fixed

Comment on lines 38 to 52
const handleEditTodo = (
title: string,
) => {
const todosCopy = [...todos];

if (title && title !== todo.title) {
todosCopy.splice(index, 1, { ...todo, title });

setTodos(prev => prev.map(currentTodo => {
if (currentTodo.id === todo.id) {
return { ...currentTodo, title };
}

return currentTodo;
}));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#679 (comment) - not fully fixed

Comment on lines 19 to 32
const filteredTodos = useMemo(() => {
return todos.filter(todo => {
switch (filterParam) {
case (Status.All):
return todo;
case (Status.Active):
return !todo.completed;
case (Status.Completed):
return todo.completed;
default:
return todo;
}
});
}, [filterParam, todos]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#679 (comment) - not fully fixed

Copy link

@BudnikOleksii BudnikOleksii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work 🚀

export interface Todo {
id: string,
title: string,
completed: true | false,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
completed: true | false,
completed: boolean,

boolean type is the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants