4 changed files with 116 additions and 18393 deletions
@ -0,0 +1,64 @@ |
|||||||
|
import React, { useEffect, useState } from "react"; |
||||||
|
import firebase from "firebase/app"; |
||||||
|
import { Button } from "@mui/material"; |
||||||
|
import { MyForm, Admin } from "./index"; |
||||||
|
|
||||||
|
export const Proiezione = () => { |
||||||
|
const [messaggi, cambiaMessaggi] = useState([]); |
||||||
|
const [messaggiApprovati, cambiaMessaggiApprovati] = useState([]); |
||||||
|
const [indiceCorrente, cambiaindiceCorrente] = useState(0); |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
var messagesRef = firebase.database().ref("/messaggi"); |
||||||
|
|
||||||
|
messagesRef.on("child_added", function (snapshot) { |
||||||
|
var messaggio = snapshot.val(); |
||||||
|
cambiaMessaggi((oldArray) => { |
||||||
|
const newArray = [...oldArray, messaggio].sort( |
||||||
|
(a, b) => b.timestamp - a.timestamp |
||||||
|
); |
||||||
|
cambiaMessaggiApprovati(newArray.filter((d) => d.approvato)); |
||||||
|
return newArray; |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
messagesRef.on("child_changed", function (snapshot) { |
||||||
|
// console.log(indiceCorrente); Rimane alla versione iniziale!
|
||||||
|
const messaggio = snapshot.val(); |
||||||
|
cambiaMessaggi((oldArray) => { |
||||||
|
const newArray = [...oldArray]; |
||||||
|
newArray.forEach((d, i, a) => { |
||||||
|
if (d.id === messaggio.id) a[i] = messaggio; |
||||||
|
}); |
||||||
|
cambiaMessaggiApprovati(newArray.filter((d) => d.approvato)); |
||||||
|
return newArray; |
||||||
|
}); |
||||||
|
}); |
||||||
|
}, []); |
||||||
|
|
||||||
|
const nextMessage = () => { |
||||||
|
if (!messaggiApprovati.length) return; |
||||||
|
const newIndice = (indiceCorrente + 1) % messaggiApprovati.length; |
||||||
|
cambiaindiceCorrente(newIndice); |
||||||
|
}; |
||||||
|
|
||||||
|
const Messaggio = () => { |
||||||
|
if (!messaggiApprovati.length) return <div>nessun messaggio</div>; |
||||||
|
const messaggioCorrente = messaggiApprovati[indiceCorrente]; |
||||||
|
return ( |
||||||
|
<div> |
||||||
|
<h1>{messaggioCorrente.testo}</h1> |
||||||
|
<p>da {messaggioCorrente.autore}</p> |
||||||
|
<Button onClick={() => nextMessage()}>next</Button> |
||||||
|
</div> |
||||||
|
); |
||||||
|
}; |
||||||
|
|
||||||
|
return ( |
||||||
|
<div> |
||||||
|
<MyForm /> |
||||||
|
<Messaggio></Messaggio> |
||||||
|
<Admin /> |
||||||
|
</div> |
||||||
|
); |
||||||
|
}; |
||||||
Loading…
Reference in new issue