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

modified onclick reactions #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ And request an API key from https://thecatapi.com/

## Run Locally

Clone the project
Clone the project=]


```bash
git clone https://link-to-project
Expand Down
22 changes: 15 additions & 7 deletions src/components/Cat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ export default function Cat() {
}, []);

function getCatImage() {
if (buttonDisabled) return; // Don't do anything if the button is disabled

setButtonDisabled(true); // Disable the button temporarily
const randomNumber = Math.floor(Math.random() * catGifUrls.length);
const url = catGifUrls[randomNumber];
setCat((prevCat) => ({
...prevCat,
randomImage: url,
}));

// Re-enable the button after a short delay (1 second in this example)
setTimeout(() => {
setButtonDisabled(false);
}, 1000);
}

function handleChange(event) {
Expand Down Expand Up @@ -66,13 +74,13 @@ export default function Cat() {
value={cat.bottomText}
onChange={handleChange}
/>
<button
className="form--button"
onClick={getCatImage}
disabled={loading}
>
Get a new cat image 🐱
</button>
<button
className="form--button"
onClick={getCatImage}
disabled={loading || buttonDisabled} // Disable the button if loading or buttonDisabled is true
>
Get a new cat image 🐱
</button>
</div>
<div className="cat">
{loading && <img src={Loader} alt="Loading.." className="loading" />}
Expand Down