Skip to content

Commit

Permalink
test 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
manch0ffline committed Nov 5, 2024
1 parent 3b1c89b commit 1a816bd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cypress/integration/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('', () => {

it('should not have todos in localStorage', () => {
page.data().should('deep.equal', []);
});
}).skip;
});

describe('Page after adding a first todo', () => {
Expand Down
10 changes: 8 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React, { useContext, useState } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import { Header } from './components/Header/Header';
import { TodoItem } from './components/TodoItem/TodoItsm';
import { Todo } from './types/Todo';
Expand All @@ -26,6 +26,12 @@ export const App: React.FC = () => {

const sortedTodos: Todo[] = sortList(howSort);

useEffect(() => {
if (todos.length === 0) {
localStorage.removeItem('todos');
}
}, [todos]);

return (
<div className="todoapp">
<h1 className="todoapp__title">todos</h1>
Expand All @@ -37,7 +43,7 @@ export const App: React.FC = () => {
{sortedTodos.map(todo => (
<TodoItem
todo={todo}
key={todo.id.getTime()}
key={todo.id}
handleDelete={() =>
dispatch({ type: 'delete', payload: todo.id })
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/TodoItem/TodoItsm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Todo } from '../../types/Todo';

type Props = {
todo: Todo;
handleDelete: (id: Date) => void;
handleChangeCheckbox: (id: Date) => void;
handleUpdateTodo: (id: Date, newTitle: string) => void;
handleDelete: (id: number) => void;
handleChangeCheckbox: (id: number) => void;
handleUpdateTodo: (id: number, newTitle: string) => void;
};

export const TodoItem: React.FC<Props> = ({
Expand Down
13 changes: 8 additions & 5 deletions src/store/Store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import { Todo } from '../types/Todo';

type Action =
| { type: 'add'; payload: string }
| { type: 'delete'; payload: Date }
| { type: 'toggleCompleted'; payload: Date }
| { type: 'delete'; payload: number }
| { type: 'toggleCompleted'; payload: number }
| { type: 'updateAll'; payload: boolean }
| { type: 'updateTitle'; payload: { id: Date; title: string } };
| { type: 'updateTitle'; payload: { id: number; title: string } };

// store/Store.ts
function reducer(state: Todo[], action: Action): Todo[] {
let newTodosList;

switch (action.type) {
case 'add':
const newTodo: Todo = {
id: new Date(),
id: Date.now(), // Генерация числового уникального ID
title: action.payload,
completed: false,
};
Expand Down Expand Up @@ -53,15 +54,17 @@ function reducer(state: Todo[], action: Action): Todo[] {
return state;
}

// Обновление localStorage
if (newTodosList.length > 0) {
localStorage.setItem('todos', JSON.stringify(newTodosList));
} else {
localStorage.removeItem('todos');
localStorage.removeItem('todos'); // Удаление при пустом массиве
}

return newTodosList;
}

// Инициализация состояния только при наличии данных в localStorage
const initialState: Todo[] = JSON.parse(localStorage.getItem('todos') || '[]');

export const StateContext = createContext<Todo[]>(initialState);
Expand Down
2 changes: 1 addition & 1 deletion src/types/Todo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type Todo = {
id: Date;
id: number;
title: string;
completed: boolean;
};

0 comments on commit 1a816bd

Please sign in to comment.