Skip to content

Commit f36fc44

Browse files
Merge branch 'develop'
2 parents b7f9385 + ea98e96 commit f36fc44

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>Todo List TS</title>
7+
<title>MyTodo</title>
88
</head>
99
<body>
1010
<div id="root"></div>

src/App.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,15 @@ hr {
8686
border-top: 1px solid #d1d5db;
8787
}
8888

89+
hr+p {
90+
font-style: italic;
91+
text-align: center;
92+
}
93+
8994
li {
9095
list-style-type: none;
9196
background: #f8f9fa;
92-
margin-bottom: 0.5rem;
97+
margin-bottom: 0.2rem;
9398
padding: 0.5rem 1rem;
9499
border-radius: 0.5rem;
95100
color: #222;

src/App.tsx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { useReducer, useState } from 'react'
1+
import { useEffect, useReducer, useState } from 'react'
22
import './App.css'
3+
4+
const TODOS_STORAGE_KEY = 'todos'
35
interface Task {
46
id?: number;
57
text: string;
@@ -12,9 +14,22 @@ const App = () => {
1214
const [text, setText] = useState('');
1315
const [todos, dispatch] = useReducer(reducer, initialState)
1416

17+
useEffect(() => {
18+
const loadedTasks = loadTasks();
19+
if (loadedTasks.length > 0) {
20+
loadedTasks.forEach(task => {
21+
dispatch({ type: "ADD_TASK", text: task.text, done: task.done, id: task.id })
22+
})
23+
}
24+
}, [])
25+
26+
useEffect(() => {
27+
localStorage.setItem(TODOS_STORAGE_KEY, JSON.stringify(todos))
28+
}, [todos])
29+
1530
const handleAddTask = () => {
1631
if (!text) return
17-
dispatch({ type: 'ADD_TASK', value: text })
32+
dispatch({ type: 'ADD_TASK', text: text })
1833
setText('');
1934
}
2035

@@ -34,6 +49,7 @@ const App = () => {
3449

3550
<hr />
3651

52+
{todos.length === 0 && <p>No tasks found</p>}
3753
<ul>
3854
{todos.map(task => (
3955
<li key={task.id} >
@@ -62,23 +78,36 @@ const App = () => {
6278
export default App
6379

6480
type Action =
65-
| { type: 'ADD_TASK', value: string }
81+
| { type: 'ADD_TASK', text: string, done?: boolean, id?: number }
6682
| { type: 'DELETE_TASK', id: number }
6783
| { type: 'TOGGLE_TASK', id: number }
6884

6985
const reducer = (state: Task[], action: Action) => {
7086
switch (action.type) {
7187

7288
case 'ADD_TASK':
73-
return [...state, { id: Date.now(), text: action.value, done: false }]
89+
return [...state, {
90+
id: action.id ?? Date.now(),
91+
text: action.text,
92+
done: action.done ?? false
93+
}]
7494

7595
case 'DELETE_TASK':
7696
return state.filter(task => task.id !== action.id)
7797

7898
case 'TOGGLE_TASK':
79-
return state.map(task => (task.id === action.id) ? { ...task, done: !task.done } : task)
99+
return state.map(task => (
100+
task.id === action.id) ? { ...task, done: !task.done } : task)
80101

81102
default:
82103
return state
83104
}
84105
}
106+
107+
const loadTasks = (): Task[] => {
108+
const savedTasks = localStorage.getItem(TODOS_STORAGE_KEY)
109+
if (savedTasks) {
110+
return JSON.parse(savedTasks)
111+
}
112+
return []
113+
}

0 commit comments

Comments
 (0)