Skip to content

Commit

Permalink
added hook to handle ESC key to close cards
Browse files Browse the repository at this point in the history
  • Loading branch information
okrayum committed Jun 14, 2024
1 parent b08d426 commit 56353cd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 49 deletions.
102 changes: 53 additions & 49 deletions client/src/components/CardDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
import React from "react";
import { Card } from "../types";
import useKeyPress from "../hooks/useKeyPress";

interface CardDetailsProps {
selectedCard: Card;
handleUpdateSelectedCard: (card: Card) => void;
handleResetSelectedCard: () => void;
selectedCard: Card;
handleUpdateSelectedCard: (card: Card) => void;
handleResetSelectedCard: () => void;
}

const CardDetails: React.FC<CardDetailsProps> = ({
selectedCard,
handleUpdateSelectedCard,
handleResetSelectedCard,
selectedCard,
handleUpdateSelectedCard,
handleResetSelectedCard,
}) => {
const toggleCheck = (index: number) => {
if (!selectedCard || !selectedCard.details.checklist) return;
const toggleCheck = (index: number) => {
if (!selectedCard || !selectedCard.details.checklist) return;

const newSelectedCard = {
...selectedCard,
details: {
...selectedCard.details,
checklist: selectedCard.details.checklist.map((item, idx) =>
idx === index ? { ...item, checked: !item.checked } : item
),
},
};
const newSelectedCard = {
...selectedCard,
details: {
...selectedCard.details,
checklist: selectedCard.details.checklist.map((item, idx) =>
idx === index ? { ...item, checked: !item.checked } : item
),
},
};

handleUpdateSelectedCard(newSelectedCard);
};
handleUpdateSelectedCard(newSelectedCard);
};

return (
<div className="p-4 w-1/2 mx-auto bg-secondaryElements shadow-md rounded-lg">
<h2 className="text-lg font-bold mb-2">{selectedCard.cardName}</h2>
<ul>
{selectedCard.details.checklist?.map((item, index) => (
<li key={index} className="flex items-center mb-2">
<input
type="checkbox"
checked={item.checked}
onChange={() => toggleCheck(index)}
/>
<label className="ml-2" onClick={() => toggleCheck(index)}>
{item.value}
</label>
</li>
))}
</ul>
<p className="mt-4">Notes: {selectedCard.details.notes}</p>
<p className="mt-1">
Time Estimate: {selectedCard.details.timeEstimate} Minutes
</p>
<p className="mt-1">Column: {selectedCard.column}</p>
<button
className="mt-8 py-1.5 px-8 text-sm bg-black text-white rounded"
onClick={() => handleResetSelectedCard()}
>
Close
</button>
</div>
);
// Use custom hook to handle ESC key
useKeyPress("Escape", handleResetSelectedCard);

return (
<div className="p-4 w-1/2 mx-auto bg-secondaryElements shadow-md rounded-lg">
<h2 className="text-lg font-bold mb-2">{selectedCard.cardName}</h2>
<ul>
{selectedCard.details.checklist?.map((item, index) => (
<li key={index} className="flex items-center mb-2">
<input
type="checkbox"
checked={item.checked}
onChange={() => toggleCheck(index)}
/>
<label className="ml-2" onClick={() => toggleCheck(index)}>
{item.value}
</label>
</li>
))}
</ul>
<p className="mt-4">Notes: {selectedCard.details.notes}</p>
<p className="mt-1">
Time Estimate: {selectedCard.details.timeEstimate} Minutes
</p>
<p className="mt-1">Column: {selectedCard.column}</p>
<button
className="mt-8 py-1.5 px-8 text-sm bg-black text-white rounded"
onClick={() => handleResetSelectedCard()}
>
Close
</button>
</div>
);
};

export default CardDetails;
20 changes: 20 additions & 0 deletions client/src/hooks/useKeyPress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// hooks/useKeyPress.ts
import { useEffect } from "react";

const useKeyPress = (targetKey: string, callback: () => void) => {
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === targetKey) {
callback();
}
};

document.addEventListener("keydown", handleKeyDown);

return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, [targetKey, callback]);
};

export default useKeyPress;

0 comments on commit 56353cd

Please sign in to comment.