added countdown to messaggio
This commit is contained in:
+7
-16
@@ -1,6 +1,6 @@
|
|||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
function useInterval(callback, delay) {
|
export function useInterval(callback, delay) {
|
||||||
const savedCallback = useRef();
|
const savedCallback = useRef();
|
||||||
|
|
||||||
// Remember the latest callback.
|
// Remember the latest callback.
|
||||||
@@ -20,29 +20,20 @@ function useInterval(callback, delay) {
|
|||||||
}, [delay]);
|
}, [delay]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function Countdown({ duration, onComplete }) {
|
function Countdown({ duration, onComplete }) {
|
||||||
// Initialize the countdown timer with the duration prop
|
// Initialize the countdown timer with the duration prop
|
||||||
const [timeLeft, setTimeLeft] = useState(duration);
|
const [timeLeft, setTimeLeft] = useState(duration);
|
||||||
|
|
||||||
// Decrement the timer by 1 second every 1000ms
|
// Decrement the timer by 1 second every 1000ms
|
||||||
useInterval(() => {
|
useInterval(() => {
|
||||||
if(timeLeft === 1) {
|
if (timeLeft === 1) {
|
||||||
onComplete()
|
onComplete();
|
||||||
} else{
|
} else {
|
||||||
|
setTimeLeft(timeLeft - 1);
|
||||||
setTimeLeft(timeLeft - 1);
|
|
||||||
}
|
}
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
// Return the time left formatted as minutes and seconds
|
|
||||||
let minutes = Math.floor(timeLeft / 60);
|
|
||||||
let seconds = timeLeft % 60;
|
let seconds = timeLeft % 60;
|
||||||
return (
|
return <div>{seconds}</div>;
|
||||||
<div>{seconds}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Countdown
|
export default Countdown;
|
||||||
|
|||||||
+38
-12
@@ -2,6 +2,7 @@ import React, { useEffect, useState } from "react";
|
|||||||
import firebase from "firebase/app";
|
import firebase from "firebase/app";
|
||||||
import { Button } from "@mui/material";
|
import { Button } from "@mui/material";
|
||||||
import { MyForm, Admin } from "./index";
|
import { MyForm, Admin } from "./index";
|
||||||
|
import { useInterval } from "./Countdown";
|
||||||
|
|
||||||
export const Proiezione = () => {
|
export const Proiezione = () => {
|
||||||
const [messaggi, cambiaMessaggi] = useState([]);
|
const [messaggi, cambiaMessaggi] = useState([]);
|
||||||
@@ -23,7 +24,6 @@ export const Proiezione = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
messagesRef.on("child_changed", function (snapshot) {
|
messagesRef.on("child_changed", function (snapshot) {
|
||||||
// console.log(indiceCorrente); Rimane alla versione iniziale!
|
|
||||||
const messaggio = snapshot.val();
|
const messaggio = snapshot.val();
|
||||||
cambiaMessaggi((oldArray) => {
|
cambiaMessaggi((oldArray) => {
|
||||||
const newArray = [...oldArray];
|
const newArray = [...oldArray];
|
||||||
@@ -42,23 +42,49 @@ export const Proiezione = () => {
|
|||||||
cambiaindiceCorrente(newIndice);
|
cambiaindiceCorrente(newIndice);
|
||||||
};
|
};
|
||||||
|
|
||||||
const Messaggio = () => {
|
const ContainerMessaggio = () => {
|
||||||
if (!messaggiApprovati.length) return <div>nessun messaggio</div>;
|
if (!messaggiApprovati.length) {
|
||||||
const messaggioCorrente = messaggiApprovati[indiceCorrente];
|
<div>nessun messaggio</div>;
|
||||||
return (
|
} else {
|
||||||
<div>
|
const messaggioCorrente = messaggiApprovati[indiceCorrente];
|
||||||
<h1>{messaggioCorrente.testo}</h1>
|
|
||||||
<p>da {messaggioCorrente.autore}</p>
|
return (
|
||||||
<Button onClick={() => nextMessage()}>next</Button>
|
<Messaggio
|
||||||
</div>
|
messaggioCorrente={messaggioCorrente}
|
||||||
);
|
nextMessage={nextMessage}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<MyForm />
|
<MyForm />
|
||||||
<Messaggio></Messaggio>
|
<ContainerMessaggio />
|
||||||
<Admin />
|
<Admin />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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 (
|
||||||
|
<div>
|
||||||
|
<h1>{messaggioCorrente.testo}</h1>
|
||||||
|
<p>da {messaggioCorrente.autore}</p>
|
||||||
|
<Button onClick={() => nextMessage()}>next</Button>
|
||||||
|
{timeLeft}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user