Skip to content

Commit d65fc07

Browse files
authored
Finish feature/117
2 parents 2bf04ff + c04ea83 commit d65fc07

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { useEffect, useState } from 'react';
2+
import { useTitle } from '../../../hooks/js/useTitle.js';
3+
4+
const appNotifications = [
5+
{ id: 1, title: 'Notification 1' },
6+
{ id: 2, title: 'Notification 2' },
7+
{ id: 3, title: 'Notification 3' },
8+
{ id: 4, title: 'Notification 4' },
9+
{ id: 5, title: 'Notification 5' }
10+
];
11+
12+
export const Notifications = () => {
13+
const [notifications, setNotifications] = useState(appNotifications);
14+
const { title, changeTitle } = useTitle();
15+
16+
useEffect(() => {
17+
changeTitle(`${title} (${notifications.length})`);
18+
}, []);
19+
20+
// Delete notification
21+
const deleteNotification = (id) => {
22+
const updatedNotifications = notifications.filter(
23+
(notification) => notification.id !== id
24+
);
25+
setNotifications(updatedNotifications);
26+
27+
// Set the title depending on the number of notifications left
28+
const newTitle =
29+
updatedNotifications.length > 0
30+
? `${title.split(' (')[0]} (${updatedNotifications.length})`
31+
: title.split(' (')[0];
32+
changeTitle(newTitle);
33+
};
34+
35+
return (
36+
<>
37+
<h1>Notifications</h1>
38+
<ul>
39+
{notifications.map((notification) => (
40+
<li key={notification.id}>
41+
{notification.title}
42+
<button onClick={() => deleteNotification(notification.id)}>
43+
Delete
44+
</button>
45+
</li>
46+
))}
47+
</ul>
48+
</>
49+
);
50+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { useEffect, useState } from 'react';
2+
import { useTitle } from '../../../hooks/js/useTitle';
3+
4+
const appTasks = [
5+
{ id: 1, title: 'Task 1', isDone: false },
6+
{ id: 2, title: 'Task 2', isDone: false },
7+
{ id: 3, title: 'Task 3', isDone: false },
8+
{ id: 4, title: 'Task 4', isDone: false },
9+
{ id: 5, title: 'Task 5', isDone: false }
10+
];
11+
12+
export const ToDoList = () => {
13+
const [tasks, setTasks] = useState(appTasks);
14+
const { changeTitle } = useTitle();
15+
16+
useEffect(() => {
17+
const unfinishedTasks = tasks.filter((task) => !task.isDone).length;
18+
changeTitle(`${unfinishedTasks} pending tasks`);
19+
}, []);
20+
21+
// Handle check/uncheck task
22+
const checkTask = (id, isDone) => {
23+
const updatedTasks = tasks.map((task) =>
24+
task.id === id ? { ...task, isDone } : task
25+
);
26+
setTasks(updatedTasks);
27+
28+
// Set the title depending on the number of tasks left
29+
const unfinishedTasks = updatedTasks.filter((task) => !task.isDone).length;
30+
const newTitle =
31+
unfinishedTasks !== 1
32+
? `${unfinishedTasks} pending tasks`
33+
: `${unfinishedTasks} pending task`;
34+
changeTitle(newTitle);
35+
};
36+
37+
return (
38+
<>
39+
<h1>Tasks</h1>
40+
<ul>
41+
{tasks.map((task) => (
42+
<li key={task.id}>
43+
<input
44+
type="checkbox"
45+
checked={task.isDone}
46+
onChange={(e) => checkTask(task.id, e.target.checked)}
47+
/>
48+
{task.title}
49+
</li>
50+
))}
51+
</ul>
52+
</>
53+
);
54+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { useEffect, useState } from 'react';
2+
import { useTitle } from '../../../hooks/ts/useTitle.ts';
3+
4+
interface Notification {
5+
id: number;
6+
title: string;
7+
}
8+
9+
const appNotifications: Notification[] = [
10+
{ id: 1, title: 'Notification 1' },
11+
{ id: 2, title: 'Notification 2' },
12+
{ id: 3, title: 'Notification 3' },
13+
{ id: 4, title: 'Notification 4' },
14+
{ id: 5, title: 'Notification 5' }
15+
];
16+
17+
export const Notifications = () => {
18+
const [notifications, setNotifications] =
19+
useState<Notification[]>(appNotifications);
20+
const { title, changeTitle } = useTitle();
21+
22+
useEffect(() => {
23+
changeTitle(`${title} (${notifications.length})`);
24+
}, []);
25+
26+
// Delete notification
27+
const deleteNotification = (id: number) => {
28+
const updatedNotifications = notifications.filter(
29+
(notification) => notification.id !== id
30+
);
31+
setNotifications(updatedNotifications);
32+
33+
// Set the title depending on the number of notifications left
34+
const newTitle =
35+
updatedNotifications.length > 0
36+
? `${title.split(' (')[0]} (${updatedNotifications.length})`
37+
: title.split(' (')[0];
38+
changeTitle(newTitle);
39+
};
40+
41+
return (
42+
<>
43+
<h1>Notifications</h1>
44+
<ul>
45+
{notifications.map((notification) => (
46+
<li key={notification.id}>
47+
{notification.title}
48+
<button onClick={() => deleteNotification(notification.id)}>
49+
Delete
50+
</button>
51+
</li>
52+
))}
53+
</ul>
54+
</>
55+
);
56+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { useEffect, useState } from 'react';
2+
import { useTitle } from '../../../hooks/ts/useTitle';
3+
4+
interface Task {
5+
id: number;
6+
title: string;
7+
isDone: boolean;
8+
}
9+
10+
const appTasks: Task[] = [
11+
{ id: 1, title: 'Task 1', isDone: false },
12+
{ id: 2, title: 'Task 2', isDone: false },
13+
{ id: 3, title: 'Task 3', isDone: false },
14+
{ id: 4, title: 'Task 4', isDone: false },
15+
{ id: 5, title: 'Task 5', isDone: false }
16+
];
17+
18+
export const ToDoList = () => {
19+
const [tasks, setTasks] = useState<Task[]>(appTasks);
20+
const { changeTitle } = useTitle();
21+
22+
useEffect(() => {
23+
const unfinishedTasks = tasks.filter((task) => !task.isDone).length;
24+
changeTitle(`${unfinishedTasks} pending tasks`);
25+
}, []);
26+
27+
// Handle check/uncheck task
28+
const checkTask = (id: number, isDone: boolean) => {
29+
const updatedTasks = tasks.map((task) =>
30+
task.id === id ? { ...task, isDone } : task
31+
);
32+
setTasks(updatedTasks);
33+
34+
// Set the title depending on the number of tasks left
35+
const unfinishedTasks = updatedTasks.filter((task) => !task.isDone).length;
36+
const newTitle =
37+
unfinishedTasks !== 1
38+
? `${unfinishedTasks} pending tasks`
39+
: `${unfinishedTasks} pending task`;
40+
changeTitle(newTitle);
41+
};
42+
43+
return (
44+
<>
45+
<h1>Tasks</h1>
46+
<ul>
47+
{tasks.map((task) => (
48+
<li key={task.id}>
49+
<input
50+
type="checkbox"
51+
checked={task.isDone}
52+
onChange={(e) => checkTask(task.id, e.target.checked)}
53+
/>
54+
{task.title}
55+
</li>
56+
))}
57+
</ul>
58+
</>
59+
);
60+
};

0 commit comments

Comments
 (0)