Browse Source

image loading

master
Danilo Di Cuia 3 years ago
parent
commit
ce499bbed7
  1. 91
      src/Galleria.js

91
src/Galleria.js

@ -8,34 +8,62 @@ import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import IconButton from "@mui/material/IconButton"; import IconButton from "@mui/material/IconButton";
import { Stack } from "@mui/system"; import { Stack } from "@mui/system";
import logoSrc from "./logo_chat.svg"; import logoSrc from "./logo_chat.svg";
import { Fade } from "@mui/material"; import { Fade, LinearProgress } from "@mui/material";
export const Galleria = () => { export const Galleria = () => {
const { evento } = useParams(); const { evento } = useParams();
const [messaggi, cambiaMessaggi] = useState([]); const [messaggi, cambiaMessaggi] = useState([]);
const [imgIndex, cambiaimgIndex] = useState(0);
const [indiceCorrente, cambiaindiceCorrente] = useState(0); const [images, cambiaimmagini] = useState([]);
useEffect(() => { useEffect(() => {
var messagesRef = firebase.database().ref(`/messaggi/${evento}`); var messagesRef = firebase.database().ref(`/messaggi/${evento}`);
messagesRef.on("value", function (snapshot) { messagesRef.on("value", function (snapshot) {
var messaggi = snapshot.val(); const messaggiObject = snapshot.val();
const messaggi = Object.keys(messaggiObject)
cambiaMessaggi(
Object.keys(messaggi)
.map((d) => { .map((d) => {
return messaggi[d]; return messaggiObject[d];
}) })
.filter((d) => d.approvato) .filter((d) => d.approvato)
.sort((a, b) => a.timestamp - b.timestamp) .sort((a, b) => a.timestamp - b.timestamp);
cambiaMessaggi(messaggi);
cambiaimmagini(
messaggi
.filter((d) => d.immagineURL != null)
.map((d) => {
return d.immagineURL;
})
); );
}); });
}, [evento]); }, [evento]);
const messaggioDaMostrare = messaggi[indiceCorrente]; useEffect(() => {
if (images) {
function loadImage() {
const img = new Image();
img.src = images[imgIndex];
img.onload = () => {
if (imgIndex < images.length) {
cambiaimgIndex((i) => {
return i + 1;
});
}
};
}
loadImage();
}
}, [imgIndex, images]);
return ( return (
<div> <div>
{imgIndex !== images.length && (
<LinearProgress
variant="determinate"
value={(imgIndex / images.length) * 100}
style={{ position: "absolute", width: "100vw", top: "50%" }}
/>
)}
<div <div
style={{ style={{
margin: "0 auto", margin: "0 auto",
@ -51,6 +79,30 @@ export const Galleria = () => {
textAlign: "center", textAlign: "center",
}} }}
> >
{imgIndex === images.length && (
<ContenitoreMessaggio messaggi={messaggi} />
)}
<img
src={logoSrc}
alt=""
style={{
width: 120,
position: "absolute",
top: 20,
right: 20,
}}
/>
</div>
</div>
);
};
const ContenitoreMessaggio = ({ messaggi }) => {
const [indiceCorrente, cambiaindiceCorrente] = useState(0);
const messaggioDaMostrare = messaggi[indiceCorrente];
console.log(indiceCorrente);
return (
<Stack> <Stack>
<div> <div>
{messaggioDaMostrare && ( {messaggioDaMostrare && (
@ -79,32 +131,17 @@ export const Galleria = () => {
direction="row" direction="row"
> >
{indiceCorrente > 0 && ( {indiceCorrente > 0 && (
<IconButton <IconButton onClick={() => cambiaindiceCorrente(indiceCorrente - 1)}>
onClick={() => cambiaindiceCorrente(indiceCorrente + 1)}
>
<ArrowBackIcon style={{ fontSize: "3rem" }} /> <ArrowBackIcon style={{ fontSize: "3rem" }} />
</IconButton> </IconButton>
)} )}
{indiceCorrente < messaggi.length - 1 && ( {indiceCorrente < messaggi.length - 1 && (
<IconButton <IconButton onClick={() => cambiaindiceCorrente(indiceCorrente + 1)}>
onClick={() => cambiaindiceCorrente(indiceCorrente + 1)}
>
<ArrowForwardIcon style={{ fontSize: "3rem" }} /> <ArrowForwardIcon style={{ fontSize: "3rem" }} />
</IconButton> </IconButton>
)} )}
</Stack> </Stack>
</Stack> </Stack>
<img
src={logoSrc}
style={{
width: 120,
position: "absolute",
top: 20,
right: 20,
}}
/>
</div>
</div>
); );
}; };

Loading…
Cancel
Save