Skip to content

Commit

Permalink
added loaders and fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
taras-bilas committed Sep 14, 2023
1 parent 3cfc7aa commit ca508d3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
45 changes: 31 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable jsx-a11y/control-has-associated-label */
import React, { useEffect, useMemo, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { UserWarning } from './UserWarning';
import { TodoHeader } from './components/TodoHeader';
import { TodoList } from './components/TodoList';
Expand Down Expand Up @@ -28,24 +28,26 @@ export const App: React.FC = () => {

useEffect(() => {
getTodos(USER_ID)
.then(setTodos);
.then(setTodos)
.catch(() => setError(Error.load));
}, []);

const toggleTodo = (todo: Todo) => {
const changedStatus = {
completed: !todo.completed,
};
const toggleTodo = useCallback((todo: Todo) => {
const changedStatus = { completed: !todo.completed };

setDeletingIds((ids) => [...ids, todo.id]);

updateTodos(todo.id, changedStatus)
.then(() => {
return getTodos(USER_ID)
.then(setTodos);
.then(setTodos)
.catch(() => setError(Error.load));
})
.catch(() => setError(Error.update))
.finally(() => {
setDeletingIds((ids) => ids.filter(id => id !== todo.id));
});
};
}, [todos]);

const toggleAll = () => {
if (areAllCompleted) {
Expand All @@ -59,19 +61,34 @@ export const App: React.FC = () => {
.forEach(todo => toggleTodo(todo));
};

const renameTodo = (todoId: number, newTitle: string) => {
const renameTodo = useCallback((todoId: number, newTitle: string) => {
if (newTitle.trim().length === 0) {
setError(Error.update);

return;
}

const newData = { title: newTitle };

setDeletingIds((ids) => [...ids, todoId]);

updateTodos(todoId, newData)
.then(() => {
return getTodos(USER_ID)
.then(setTodos);
.then(setTodos)
.catch(() => setError(Error.load));
})
.catch(() => setError(Error.update));
};
.catch(() => {
setError(Error.update);
})
.finally(() => {
setDeletingIds((ids) => ids.filter(id => id !== todoId));
});
}, [todos]);

const deleteTodo = (todoId: number) => {
const deleteTodo = useCallback((todoId: number) => {
setDeletingIds((ids) => [...ids, todoId]);

deleteTodos(todoId)
.then(() => setTodos(
currentTodos => currentTodos.filter(todo => todo.id !== todoId),
Expand All @@ -80,7 +97,7 @@ export const App: React.FC = () => {
.finally(() => {
setDeletingIds((ids) => ids.filter(id => id !== todoId));
});
};
}, [todos]);

const visibleTodos = useMemo(() => {
if (todos) {
Expand Down
18 changes: 6 additions & 12 deletions src/components/Errors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,15 @@ export const Errors: React.FC<Props> = ({ error, onClearErrors }) => {
onClick={() => onClearErrors(Error.without)}
/>

{error === Error.add && (
<> Unable to add a todo </>
)}
{error === Error.load && 'Unable to load todos'}

{error === Error.delete && (
<> Unable to delete a todo </>
)}
{error === Error.add && 'Unable to add a todo'}

{error === Error.update && (
<> Unable to update a todo </>
)}
{error === Error.delete && 'Unable to delete a todo'}

{error === Error.empty && (
<> Title can&apos;t be empty </>
)}
{error === Error.update && 'Unable to update a todo'}

{error === Error.empty && 'Title can\'t be empty'}
</div>
);
};
5 changes: 3 additions & 2 deletions src/components/TodoHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const TodoHeader: React.FC<Props> = ({
const submitHandler = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

if (todoTitle === '') {
if (todoTitle.trim().length === 0) {
setError(Error.empty);

return;
Expand All @@ -51,7 +51,8 @@ export const TodoHeader: React.FC<Props> = ({
addTodos(userId, newTodo)
.then(() => {
return getTodos(userId)
.then(setTodos);
.then(setTodos)
.catch(() => setError(Error.load));
})
.catch(() => setError(Error.add))
.finally(() => {
Expand Down
1 change: 0 additions & 1 deletion src/components/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable max-len */
/* eslint-disable quote-props */
// import cn from 'classnames';
import { Todo } from '../types/Todo';
import { TodoRow } from './TodoRow';

Expand Down
5 changes: 4 additions & 1 deletion src/components/TodoRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export const TodoRow: React.FC<Props> = ({
<>
<span
className="todo__title"
onDoubleClick={() => setEditing(true)}
onDoubleClick={() => {
setEditing(true);
setTitle(todo.title);
}}
>
{todo.title}
</span>
Expand Down
1 change: 1 addition & 0 deletions src/types/Error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum Error {
without = '',
empty = 'empty',
load = 'load',
add = 'add',
update = 'update',
delete = 'delete',
Expand Down

0 comments on commit ca508d3

Please sign in to comment.