Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
157 changes: 6 additions & 151 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,157 +1,12 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import { TodosContextProvider } from './components/TodosProvider';
import { TodoApp } from './components/TodoApp';
import './styles/index.scss';

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>
</div>
</div>
<TodosContextProvider>
<TodoApp />
</TodosContextProvider>
);
};
62 changes: 62 additions & 0 deletions src/api/todos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// crud operations with localstorage for todos

import { Todo } from '../components/TodosProvider';

const TODOS_STORAGE_NAME = 'todos';

function saveTodosToStorage(todos: Todo[]) {
if (todos.length === 0) {
localStorage.setItem(TODOS_STORAGE_NAME, '[]');
} else {
localStorage.setItem(TODOS_STORAGE_NAME, JSON.stringify(todos));
}
}

export function getTodosFromStorage(): Todo[] {
const todosFromStorage = localStorage.getItem(TODOS_STORAGE_NAME);

if (!todosFromStorage) {
localStorage.setItem(TODOS_STORAGE_NAME, '[]');
}

return todosFromStorage ? JSON.parse(todosFromStorage) : [];
}

export function setTodosInStorage(todos: Todo[]) {
saveTodosToStorage(todos);
}

export function addTodoToStorage(todo: Todo) {
const todosFromStorage = getTodosFromStorage();

todosFromStorage.push(todo);

saveTodosToStorage(todosFromStorage);

return todo;
}

export function deleteTodoFromStorage(todoId: Todo['id']) {
const todosFromStorage = getTodosFromStorage();

const filteredTodosList = todosFromStorage.filter(todo => todo.id !== todoId);

saveTodosToStorage(filteredTodosList);
}

export function updateTodoInStorage(
todoId: Todo['id'],
payload: Partial<Omit<Todo, 'id'>>,
) {
const todosFromStorage = getTodosFromStorage();

const updatedTodosList = todosFromStorage.map(todo => {
if (todo.id === todoId) {
return { ...todo, ...payload };
}

return todo;
});

saveTodosToStorage(updatedTodosList);
}
24 changes: 24 additions & 0 deletions src/components/TodoApp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useContext } from 'react';
import { TodoFooter } from './TodoFooter';
import { TodoHeader } from './TodoHeader';
import { TodoList } from './TodoList';
import { TodosContext } from './TodosProvider';
import '../styles/todoapp.scss';

export const TodoApp = () => {
const { todos } = useContext(TodosContext);

return (
<div className="todoapp">
<h1 className="todoapp__title">todos</h1>

<div className="todoapp__content">
<TodoHeader />

<TodoList />

{todos.length !== 0 && <TodoFooter />}
</div>
</div>
);
};
54 changes: 54 additions & 0 deletions src/components/TodoFooter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useContext } from 'react';
import '../styles/filters.scss';
import { TodoFilterStatus, TodosContext } from './TodosProvider';
import classNames from 'classnames';
import { checkSomeTodosCompleted } from '../utils';

export const TodoFooter = () => {
const { todos, setTodoFilterStatus, todoFilterStatus, deleteTodo } =
useContext(TodosContext);

function handleClearAllCompletedTodos() {
todos.forEach(todo => {
if (todo.completed) {
deleteTodo(todo.id);
}
});
}

return (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{todos.filter(todo => !todo.completed).length} items left
</span>
<nav className="filter" data-cy="Filter">
{[
TodoFilterStatus.All,
TodoFilterStatus.Active,
TodoFilterStatus.Completed,
].map(filterName => (
<a
key={filterName}
href="#/"
className={classNames('filter__link', {
selected: filterName === todoFilterStatus,
})}
onClick={() => setTodoFilterStatus(filterName)}
data-cy={`FilterLink${filterName}`}
>
{filterName}
</a>
))}
</nav>
Comment thread
0-nira-0 marked this conversation as resolved.
<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
disabled={!checkSomeTodosCompleted(todos)}
onClick={handleClearAllCompletedTodos}
>
Clear completed
</button>
</footer>
);
};
Loading
Loading