Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 1: Delete a specific Todo item instead of removing all #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions src/components/AddTodoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function AddTodoForm({ addTodo }: AddTodoFormProps) {
const [desc, setDesc] = useState<string>('');

const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
addTodo(title, desc);
setTitle('');
setDesc('');
Expand All @@ -23,6 +24,7 @@ function AddTodoForm({ addTodo }: AddTodoFormProps) {
<div>
<label>Title:</label>
<input
value={title}
type="text"
placeholder="Provide a title for the new To Do"
onChange={(e) => setTitle(e.target.value)}
Expand Down
12 changes: 10 additions & 2 deletions src/components/Banner.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useRef } from "react";

/*
* Banner: pulls an image url from api.nasa.gov and displays the site banner
Expand All @@ -9,6 +9,10 @@ const Banner: React.FC = () => {
const [currentImgUrl, setCurrentImgUrl] = useState<string | null>(null);
const [copyright, setCopyright] = useState<string | null>(null);

// Refs to hold the latest image URLs
const imgUrlRef = useRef<string | null>(null);
const hdImgUrlRef = useRef<string | null>(null);

useEffect(() => {
const fetchImg = async () => {
const res = await fetch('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY');
Expand All @@ -18,13 +22,17 @@ const Banner: React.FC = () => {
setHdImgUrl(data.hdurl);
setCurrentImgUrl(window.innerWidth > 800 ? data.hdurl : data.url);
setCopyright(data.copyright);

// update refs with the latest values
imgUrlRef.current = data.url;
hdImgUrlRef.current = data.hdurl;
};

fetchImg();

const handleResize = () => {
const bannerElement = document.querySelector('.banner');
bannerElement?.setAttribute('style', `background-image: url(${window.innerWidth > 800? hdImgUrl : imgUrl})`);
bannerElement?.setAttribute('style', `background-image: url(${window.innerWidth > 800? hdImgUrlRef.current : imgUrlRef.current})`);
};

window.addEventListener('resize', handleResize);
Expand Down
2 changes: 1 addition & 1 deletion src/components/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function TodoItem({ todo, deleteTodo, toggleComplete, toggleUrgent }: TodoItemPr
<button onClick={() => toggleUrgent(todo.id)}>
{todo.isUrgent ? 'Mark as not Urgent' : 'Mark as Urgent'}
</button>
{todo.isCompleted && <button onClick={() => deleteTodo(todo.id)}>Delete</button>}
<button onClick={() => deleteTodo(todo.id)}>Delete</button>
</div>
</li>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/TodoList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function TodoList({ todos, deleteTodo, toggleComplete, toggleUrgent }: TodoListP
<ul>
{todos.map((todo) => (
<TodoItem
key={todo.id} // Added key prop here
todo={todo}
deleteTodo={deleteTodo}
toggleComplete={toggleComplete}
Expand Down
16 changes: 5 additions & 11 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export default function Home() {
isUrgent: false,
};

todos.push(newTodo);
setTodos(todos);
// Create a new array instead of mutating the existing one
setTodos([...todos, newTodo]);
};

const deleteTodo = (id: number) => {
setTodos(todos.filter((todo) => todo.id === id));
setTodos(todos.filter((todo) => todo.id !== id));
};

const toggleProperty = useCallback((id: number, property: keyof Pick<Todo, 'isCompleted' | 'isUrgent'>) => {
Expand All @@ -48,7 +48,7 @@ export default function Home() {
return todo;
});
setTodos(updatedTodos);
}, [setTodos]);
}, [setTodos, todos]);

const displayTodoList = (todoList:Todo[]) => {
return (
Expand All @@ -62,13 +62,7 @@ export default function Home() {
};

const displayTodos = (displayUrgent: boolean) => {
return displayTodoList(todos.filter((x) => {
if (displayUrgent) {
return !x.isCompleted && x.isUrgent === displayUrgent;
} else {
return !x.isCompleted && x.isUrgent !== displayUrgent;
}
}));
return displayTodoList(todos.filter((x) => !x.isCompleted && x.isUrgent === displayUrgent));
};

const displayComplete = () => {
Expand Down