From afe89f2ed332e4bc2f40e85214d3e6c7dbb45eb9 Mon Sep 17 00:00:00 2001 From: bytevictor Date: Thu, 28 Mar 2024 18:32:08 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Now=20you=20can=20add=20tas?= =?UTF-8?q?ks=20pressing=20'Enter'=20on=20any=20part=20of=20the=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Components/ConfigMenu/ThemeMenu.tsx | 2 +- app/Components/TasksApp/TaskApp.tsx | 76 ++++++++++++++----------- app/Components/TasksApp/TaskList.tsx | 6 +- 3 files changed, 49 insertions(+), 35 deletions(-) diff --git a/app/Components/ConfigMenu/ThemeMenu.tsx b/app/Components/ConfigMenu/ThemeMenu.tsx index f160088..d634a1b 100644 --- a/app/Components/ConfigMenu/ThemeMenu.tsx +++ b/app/Components/ConfigMenu/ThemeMenu.tsx @@ -94,7 +94,7 @@ const ThemePod = ({ }; export default function ThemeMenu() { - const [theme, setTheme] = useLocalStorage("theme", "dark"); + const [theme, setTheme] = useLocalStorage("theme", "dim"); // useState( // () => { diff --git a/app/Components/TasksApp/TaskApp.tsx b/app/Components/TasksApp/TaskApp.tsx index 824357b..b61852c 100644 --- a/app/Components/TasksApp/TaskApp.tsx +++ b/app/Components/TasksApp/TaskApp.tsx @@ -1,43 +1,52 @@ "use client"; -import { useReducer } from 'react'; -import AddTask from './AddTask'; -import TaskList from './TaskList'; +import { useEffect, useReducer } from "react"; +import AddTask from "./AddTask"; +import TaskList from "./TaskList"; export default function TaskApp() { - const [tasks, dispatch] = useReducer( - tasksReducer, - initialTasks - ); + const [tasks, dispatch] = useReducer(tasksReducer, initialTasks); + + useEffect(() => { + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === "Enter") { + handleAddTask(""); + } + }; + + document.addEventListener("keydown", handleKeyDown); + + return () => { + document.removeEventListener("keydown", handleKeyDown); + }; + }, []); function handleAddTask(text: any) { dispatch({ - type: 'added', + type: "added", id: nextId++, text: text, - isNew: true + isNew: true, }); } function handleChangeTask(task: any) { dispatch({ - type: 'changed', - task: task + type: "changed", + task: task, }); } - function handleDeleteTask(taskId: any ) { + function handleDeleteTask(taskId: any) { dispatch({ - type: 'deleted', - id: taskId + type: "deleted", + id: taskId, }); } return ( <> - + { + case "changed": { + return tasks.map((t: any) => { if (t.id === action.task.id) { return action.task; } else { @@ -66,18 +78,18 @@ function tasksReducer(tasks: any, action: any) { } }); } - case 'deleted': { - return tasks.filter( (t: any) => t.id !== action.id); + case "deleted": { + return tasks.filter((t: any) => t.id !== action.id); } default: { - throw Error('Unknown action: ' + action.type); + throw Error("Unknown action: " + action.type); } } } let nextId = 3; const initialTasks = [ - { id: 0, text: 'Philosopher’s Path', done: true, isNew: false }, - { id: 1, text: 'Visit the temple', done: false, isNew: false}, - { id: 2, text: 'Drink matcha', done: false, isNew: false } + { id: 0, text: "Philosopher’s Path", done: true, isNew: false }, + { id: 1, text: "Visit the temple", done: false, isNew: false }, + { id: 2, text: "Drink matcha", done: false, isNew: false }, ]; diff --git a/app/Components/TasksApp/TaskList.tsx b/app/Components/TasksApp/TaskList.tsx index 46c07a6..557e988 100644 --- a/app/Components/TasksApp/TaskList.tsx +++ b/app/Components/TasksApp/TaskList.tsx @@ -11,7 +11,9 @@ export default function TaskList({ onDeleteTask: any; }) { return ( -
    +
      {[...tasks].reverse().map((task: any) => (
    • @@ -49,7 +51,7 @@ function Task({ const audio = new Audio("/sounds/delete.mp3"); audio.play(); - } + }; console.log("Task", task);