From 28c81237940a7df13c77ec91b437e96ac84ae000 Mon Sep 17 00:00:00 2001 From: Danilo Di Cuia Date: Fri, 30 Dec 2022 18:20:39 +0100 Subject: [PATCH] improvements --- src/Proiezione.js | 104 ++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 60 deletions(-) diff --git a/src/Proiezione.js b/src/Proiezione.js index a49318a..baf3687 100644 --- a/src/Proiezione.js +++ b/src/Proiezione.js @@ -3,14 +3,14 @@ import firebase from "firebase/app"; import { Button, Grid, Stack } from "@mui/material"; import videoSrc from "./video.mp4"; import logoSrc from "./logo.svg"; -import { useInterval } from "./Countdown"; + +const SHOW_MESSAGE_TIME = 6000; window.debug = false; export const Proiezione = () => { const [messaggi, cambiaMessaggi] = useState([]); const [messaggiApprovati, cambiaMessaggiApprovati] = useState([]); - const [indiceCorrente, cambiaindiceCorrente] = useState(0); useEffect(() => { var messagesRef = firebase.database().ref("/messaggi"); @@ -41,33 +41,18 @@ export const Proiezione = () => { }); }, []); - const nextMessage = () => { - if (!messaggiApprovati.length) return; - const newIndice = (indiceCorrente + 1) % messaggiApprovati.length; - cambiaindiceCorrente(newIndice); - }; - - const ContainerMessaggio = ({ messaggiApprovati, indiceCorrente }) => { + const ContainerMessaggio = ({ messaggiApprovati }) => { if (!messaggiApprovati.length) { return
nessun messaggio
; } else { - return ( - - ); + return ; } }; return (
- + {
); }; -const Immagine = ({ url }) => { - return ( -
- ); -}; -const Messaggio = ({ nextMessage, messaggiApprovati, indiceCorrente }) => { - const [timeLeft, setTimeLeft] = useState(6); +const Messaggio = ({ messaggiApprovati }) => { const [fadeProp, setFadeProp] = useState("in"); const [messaggioDaMostrare, setMessaggioDaMostrare] = useState(null); + const [indiceCorrente, cambiaindiceCorrente] = useState(0); + + const nextMessage = () => { + const newIndice = (indiceCorrente + 1) % messaggiApprovati.length; + console.log("newIndice", newIndice); + cambiaindiceCorrente(newIndice); + }; + + const showNextMessage = (newMessage) => { + setFadeProp("in"); + setMessaggioDaMostrare(newMessage); + setTimeout(() => { + console.log("ready for next"); + nextMessage(); + }, SHOW_MESSAGE_TIME); + }; useEffect(() => { - if (messaggiApprovati[indiceCorrente] != messaggioDaMostrare) { + const newMessage = messaggiApprovati[indiceCorrente]; + if (newMessage != messaggioDaMostrare) { setFadeProp("out"); - setTimeout(() => { - setMessaggioDaMostrare(messaggiApprovati[indiceCorrente]); - setFadeProp("in"); + const timeout = setTimeout(() => { + if (newMessage.immagineURL) { + const img = new Image(); + img.onload = (img) => { + console.log("loaded"); + showNextMessage(newMessage); + }; + img.src = newMessage.immagineURL; + } else { + showNextMessage(newMessage); + } }, 1000); + return () => clearTimeout(timeout); } - }, [messaggiApprovati, indiceCorrente]); - - useInterval(() => { - if (timeLeft === 1) { - setFadeProp("out"); - setTimeout(() => { - nextMessage(); - }, 2000); - } else { - setTimeLeft(timeLeft - 1); - } - }, 1000); + }, [indiceCorrente]); + console.log(messaggioDaMostrare); return (
{ {messaggioDaMostrare && (
{messaggioDaMostrare.immagineURL && ( - + )}

{ > da {messaggioDaMostrare.autore}

- - {debug && ( -
- - {timeLeft} -
- )}

)}