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

Complete Task #1546

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 252 additions & 15 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,263 @@
/* eslint-disable max-len */
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import React, { useEffect, useRef, useState } from 'react';

import { UserWarning } from './UserWarning';

const USER_ID = 0;
import * as todoServices from './api/todos';

import { ErrorMessage } from './types/ErrorMessage';
import { Todo } from './types/Todo';
import { FilterBy } from './types/FilterBy';

import { Header } from './components/Header';
import { TodoList } from './components/TodoList';
import { Footer } from './components/Footer';
import { Error as ErrorCard } from './components/Error';

export const App: React.FC = () => {
if (!USER_ID) {
const [todos, setTodos] = useState<Todo[]>([]);
const [tempTodo, setTempTodo] = useState<Todo | null>(null);
const [filterBy, setFilterBy] = useState<FilterBy>(FilterBy.all);
const [loadingTodos, setLoadingTodos] = useState<number[]>([]);

const [errorMessage, setErrorMessage] = useState<ErrorMessage>('');

const [inputText, setInputText] = useState('');
const inputRef = useRef<HTMLInputElement>(null);

const [currentTodoList, setCurrentTodoList] = useState<Todo[]>([]);

const focusInput = () => {
if (inputRef.current) {
inputRef.current.focus();
}
};

useEffect(() => {
focusInput();

todoServices
.getTodos()
.then(setTodos)
.catch(() => {
setErrorMessage('Unable to load todos');
setTimeout(() => setErrorMessage(''), 3000);
});
}, []);

const handleDeleteTodo = (id: number, callback: (arg: boolean) => void) => {
callback(true);
todoServices
.deleteTodo(id)
.then(() => setTodos(todos.filter(todo => todo.id !== id)))
.catch(() => {
setErrorMessage('Unable to delete a todo');
setTimeout(() => setErrorMessage(''), 3000);
})
.finally(() => {
callback(false);
focusInput();
});
};

const handleDeleteAllCompletedTodos = async () => {
setLoadingTodos(todos.filter(todo => todo.completed).map(todo => todo.id));
todoServices
.deleteArrOfTodos(todos.filter(todo => todo.completed))
.then(res => {
const rejectedTodos = res
.map((result, index) =>
result.status === 'rejected'
? todos.filter(todo => todo.completed)[index]
: null,
)
.filter(todo => todo !== null);

setTodos(
todos.filter(todo => !todo.completed || rejectedTodos.includes(todo)),
);

if (rejectedTodos.length > 0) {
throw new Error('Some todos were not deleted');
}
})
.catch(() => {
setErrorMessage('Unable to delete a todo');
setTimeout(() => setErrorMessage(''), 3000);
})
.finally(() => {
setLoadingTodos([]);
focusInput();
});
};

const handleAddingTodo = () => {
if (inputRef.current) {
inputRef.current.disabled = true;

if (inputText.trim() === '') {
setErrorMessage('Title should not be empty');
setTimeout(() => setErrorMessage(''), 3000);
inputRef.current.disabled = false;
focusInput();

return;
}

setTempTodo({
id: 0,
userId: todoServices.USER_ID,
title: inputText.trim(),
completed: false,
});

todoServices
.addPost(inputText.trim())
.then(newTodo => {
setTodos(list => [...list, newTodo]);
setInputText('');
})
.catch(() => {
setErrorMessage('Unable to add a todo');
setTimeout(() => setErrorMessage(''), 3000);
})
.finally(() => {
if (inputRef.current) {
inputRef.current.disabled = false;
}

setTempTodo(null);
focusInput();
});
}
};

const handleUpdatingTodo = (
updatedTodo: Todo,
callback: (arg: boolean) => void,
): Promise<void> => {
callback(true);

return todoServices
.updateTodo(updatedTodo)
.then(item =>
setTodos(currTodos => {
const newTodos = [...currTodos];
const index = newTodos.findIndex(todo => todo.id === item.id);

newTodos.splice(index, 1, updatedTodo);

return newTodos;
}),
)
.catch(() => {
setErrorMessage('Unable to update a todo');
setTimeout(() => setErrorMessage(''), 3000);
throw new Error('Unable to update a todo');
})
.finally(() => {
callback(false);
focusInput();
});
};

const switchTodos = (arr: Todo[]) => {
setLoadingTodos(arr.map(todo => todo.id));
todoServices
.switchTodosStatus(arr)
.then(result => {
const updatedTodos = result
.filter(res => res.status === 'fulfilled')
.map(res => res.value);

// Now update the state with the API results
setTodos(currTodos =>
currTodos.map(todo => {
const updatedTodo = updatedTodos.find(t => t.id === todo.id);

return updatedTodo ? { ...todo, ...updatedTodo } : todo;
}),
);
})
.catch(() => {
setErrorMessage('Unable to update a todo');
setTimeout(() => setErrorMessage(''), 3000);
})
.finally(() => {
setLoadingTodos([]);
focusInput();
});
};

const handleSwitchTodosStatus = () => {
const activeTodos = todos.filter(item => !item.completed);

if (activeTodos.length > 0) {
switchTodos(activeTodos);

return;
}

switchTodos(todos);
};

useEffect(() => {
const filteredTodos = todos.filter(item => {
return (
filterBy === FilterBy.all ||
(filterBy === FilterBy.active && !item.completed) ||
(filterBy === FilterBy.completed && item.completed)
);
});

setCurrentTodoList(filteredTodos);
}, [filterBy, todos]);

if (!todoServices.USER_ID) {
return <UserWarning />;
}

return (
<section className="section container">
<p className="title is-4">
Copy all you need from the prev task:
<br />
<a href="https://github.com/mate-academy/react_todo-app-add-and-delete#react-todo-app-add-and-delete">
React Todo App - Add and Delete
</a>
</p>

<p className="subtitle">Styles are already copied</p>
</section>
<div className="todoapp">
<h1 className="todoapp__title">todos</h1>

<div className="todoapp__content">
<Header
inputRef={inputRef}
inputText={inputText}
setInputText={setInputText}
createFunc={handleAddingTodo}
todos={todos}
completeFunc={handleSwitchTodosStatus}
loadingTodos={loadingTodos}
/>

<TodoList
todos={currentTodoList}
handleDeleteTodo={handleDeleteTodo}
handleUpdateTodo={handleUpdatingTodo}
loadingTodos={loadingTodos}
tempTodo={tempTodo}
/>

{/* Hide the footer if there are no todos */}
{todos.length > 0 && (
<Footer
todos={todos}
filterBy={filterBy}
setFilterBy={setFilterBy}
deleteCompletedTodos={handleDeleteAllCompletedTodos}
/>
)}
</div>

{/* DON'T use conditional rendering to hide the notification */}
{/* Add the 'hidden' class to hide the message smoothly */}
<ErrorCard
errorMessage={errorMessage}
setErrorMessage={setErrorMessage}
/>
</div>
);
};
39 changes: 39 additions & 0 deletions src/api/todos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Todo } from '../types/Todo';
import { client } from '../utils/fetchClient';

export const USER_ID = 2088;

export const getTodos = () => {
return client.get<Todo[]>(`/todos?userId=${USER_ID}`);
};

// Add more methods here
export const deleteTodo = (todoId: number) => {
return client.delete(`/todos/${todoId}`);
};

export const deleteArrOfTodos = async (arr: Todo[]) => {
const result = await Promise.allSettled(arr.map(todo => deleteTodo(todo.id)));

return result;
};

export const addPost = (title: string) => {
return client.post<Todo>(`/todos`, {
title,
userId: USER_ID,
completed: false,
});
};

export const updateTodo = ({ id, userId, title, completed }: Todo) => {
return client.patch<Todo>(`/todos/${id}`, { id, userId, title, completed });
};

export const switchTodosStatus = async (arr: Todo[]) => {
const result = await Promise.allSettled(
arr.map(todo => updateTodo({ ...todo, completed: !todo.completed })),
);

return result;
};
33 changes: 33 additions & 0 deletions src/components/Error/Error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import { ErrorMessage } from '../../types/ErrorMessage';
import classNames from 'classnames';

type Props = {
errorMessage: ErrorMessage;
setErrorMessage: (arg: ErrorMessage) => void;
};

export const Error: React.FC<Props> = ({ errorMessage, setErrorMessage }) => {
return (
<div
data-cy="ErrorNotification"
className={classNames(
'notification is-danger is-light has-text-weight-normal',
{ hidden: errorMessage === ('' as ErrorMessage) },
)}
>
<button
data-cy="HideErrorButton"
type="button"
className="delete"
onClick={e => {
e.preventDefault();
setErrorMessage('');
}}
/>
{errorMessage}
</div>
);
};
1 change: 1 addition & 0 deletions src/components/Error/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Error';
Loading
Loading