From bd038a577d550775e8e9e4ffa354ca949d2fbaba Mon Sep 17 00:00:00 2001 From: Cooper Ransom Date: Wed, 3 Apr 2024 00:30:43 -0400 Subject: [PATCH] Add a cancel option to the random media button --- src/pages/Discover.tsx | 64 +++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/src/pages/Discover.tsx b/src/pages/Discover.tsx index 32cfca6d..b5613e3f 100644 --- a/src/pages/Discover.tsx +++ b/src/pages/Discover.tsx @@ -1,4 +1,3 @@ -import classNames from "classnames"; import React, { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { LazyLoadImage } from "react-lazy-load-image-component"; @@ -278,6 +277,8 @@ export function Discover() { ); } + const [countdownTimeout, setCountdownTimeout] = + useState(null); const handleRandomMovieClick = () => { const allMovies = Object.values(genreMovies).flat(); // Flatten all movie arrays @@ -292,14 +293,25 @@ export function Discover() { if (selectedMovie) { setRandomMovie(selectedMovie); - setCountdown(5); + if (countdown !== null && countdown > 0) { + // Clear the countdown interval + setCountdown(null); + if (countdownTimeout) { + clearTimeout(countdownTimeout); + setCountdownTimeout(null); + setRandomMovie(null); + } + } else { + setCountdown(5); - // Schedule navigation after 5 seconds - setTimeout(() => { - navigate( - `/media/tmdb-movie-${selectedMovie.id}-${selectedMovie.title}`, - ); - }, 5000); + // Schedule navigation after 5 seconds + const timeoutId = setTimeout(() => { + navigate( + `/media/tmdb-movie-${selectedMovie.id}-${selectedMovie.title}`, + ); + }, 5000); + setCountdownTimeout(timeoutId); + } } }; @@ -392,28 +404,34 @@ export function Discover() { type="button" className="flex items-center space-x-2 rounded-full px-4 text-white py-2 bg-pill-background bg-opacity-50 hover:bg-pill-backgroundHover transition-[background,transform] duration-100 hover:scale-105" onClick={handleRandomMovieClick} - disabled={countdown !== null && countdown > 0} // Disable the button during the countdown > - - {countdown !== null && countdown > 0 - ? `Playing in ${countdown} seconds` - : "Watch Something New"} - Small Image + + {countdown !== null && countdown > 0 ? ( +
+ Cancel Countdown + +
+ ) : ( +
+ Watch Something New + Small Image +
+ )}
{randomMovie && (

Now Playing {randomMovie.title}

- {/* You can add additional details or play functionality here */}
)}