From 56ccb30abcf2475b9d64d5184a93917960b1cf68 Mon Sep 17 00:00:00 2001 From: Danilo Di Cuia Date: Sun, 25 Dec 2022 19:01:26 +0100 Subject: [PATCH] added countdown to messaggio --- src/Countdown.js | 23 +++++++--------------- src/Proiezione.js | 50 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/Countdown.js b/src/Countdown.js index 7722c3a..707e0d8 100644 --- a/src/Countdown.js +++ b/src/Countdown.js @@ -1,6 +1,6 @@ import { useEffect, useRef, useState } from "react"; -function useInterval(callback, delay) { +export function useInterval(callback, delay) { const savedCallback = useRef(); // Remember the latest callback. @@ -20,29 +20,20 @@ function useInterval(callback, delay) { }, [delay]); } - - function Countdown({ duration, onComplete }) { // Initialize the countdown timer with the duration prop const [timeLeft, setTimeLeft] = useState(duration); // Decrement the timer by 1 second every 1000ms useInterval(() => { - if(timeLeft === 1) { - onComplete() - } else{ - - setTimeLeft(timeLeft - 1); + if (timeLeft === 1) { + onComplete(); + } else { + setTimeLeft(timeLeft - 1); } }, 1000); - - // Return the time left formatted as minutes and seconds - let minutes = Math.floor(timeLeft / 60); let seconds = timeLeft % 60; - return ( -
{seconds} -
- ); + return
{seconds}
; } -export default Countdown \ No newline at end of file +export default Countdown; diff --git a/src/Proiezione.js b/src/Proiezione.js index e8055a5..06e6564 100644 --- a/src/Proiezione.js +++ b/src/Proiezione.js @@ -2,6 +2,7 @@ import React, { useEffect, useState } from "react"; import firebase from "firebase/app"; import { Button } from "@mui/material"; import { MyForm, Admin } from "./index"; +import { useInterval } from "./Countdown"; export const Proiezione = () => { const [messaggi, cambiaMessaggi] = useState([]); @@ -23,7 +24,6 @@ export const Proiezione = () => { }); messagesRef.on("child_changed", function (snapshot) { - // console.log(indiceCorrente); Rimane alla versione iniziale! const messaggio = snapshot.val(); cambiaMessaggi((oldArray) => { const newArray = [...oldArray]; @@ -42,23 +42,49 @@ export const Proiezione = () => { cambiaindiceCorrente(newIndice); }; - const Messaggio = () => { - if (!messaggiApprovati.length) return
nessun messaggio
; - const messaggioCorrente = messaggiApprovati[indiceCorrente]; - return ( -
-

{messaggioCorrente.testo}

-

da {messaggioCorrente.autore}

- -
- ); + const ContainerMessaggio = () => { + if (!messaggiApprovati.length) { +
nessun messaggio
; + } else { + const messaggioCorrente = messaggiApprovati[indiceCorrente]; + + return ( + + ); + } }; return (
- +
); }; + +const Messaggio = ({ nextMessage, messaggioCorrente }) => { + const [timeLeft, setTimeLeft] = useState(5); + + // Decrement the timer by 1 second every 1000ms + useInterval(() => { + if (timeLeft === 1) { + nextMessage(); + } else { + setTimeLeft(timeLeft - 1); + } + }, 1000); + + console.log("asdf"); + return ( +
+

{messaggioCorrente.testo}

+

da {messaggioCorrente.autore}

+ + {timeLeft} +
+ ); +};