Browse Source

image loading

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

143
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( .map((d) => {
Object.keys(messaggi) return messaggiObject[d];
})
.filter((d) => d.approvato)
.sort((a, b) => a.timestamp - b.timestamp);
cambiaMessaggi(messaggi);
cambiaimmagini(
messaggi
.filter((d) => d.immagineURL != null)
.map((d) => { .map((d) => {
return messaggi[d]; return d.immagineURL;
}) })
.filter((d) => d.approvato)
.sort((a, b) => a.timestamp - b.timestamp)
); );
}); });
}, [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,51 +79,13 @@ export const Galleria = () => {
textAlign: "center", textAlign: "center",
}} }}
> >
<Stack> {imgIndex === images.length && (
<div> <ContenitoreMessaggio messaggi={messaggi} />
{messaggioDaMostrare && ( )}
<ImagePlaceholder
messaggioDaMostrare={messaggioDaMostrare}
style={{
maxWidth: "100%",
maxHeight: "80vh",
}}
/>
)}
</div>
<Stack
style={{
display: "flex",
width: "100vw",
position: "absolute",
top: "90%",
left: 0,
alignItems: "center",
justifyContent: "space-between",
padding: 10,
boxSizing: "border-box",
}}
direction="row"
>
{indiceCorrente > 0 && (
<IconButton
onClick={() => cambiaindiceCorrente(indiceCorrente + 1)}
>
<ArrowBackIcon style={{ fontSize: "3rem" }} />
</IconButton>
)}
{indiceCorrente < messaggi.length - 1 && (
<IconButton
onClick={() => cambiaindiceCorrente(indiceCorrente + 1)}
>
<ArrowForwardIcon style={{ fontSize: "3rem" }} />
</IconButton>
)}
</Stack>
</Stack>
<img <img
src={logoSrc} src={logoSrc}
alt=""
style={{ style={{
width: 120, width: 120,
position: "absolute", position: "absolute",
@ -108,6 +98,53 @@ export const Galleria = () => {
); );
}; };
const ContenitoreMessaggio = ({ messaggi }) => {
const [indiceCorrente, cambiaindiceCorrente] = useState(0);
const messaggioDaMostrare = messaggi[indiceCorrente];
console.log(indiceCorrente);
return (
<Stack>
<div>
{messaggioDaMostrare && (
<ImagePlaceholder
messaggioDaMostrare={messaggioDaMostrare}
style={{
maxWidth: "100%",
maxHeight: "80vh",
}}
/>
)}
</div>
<Stack
style={{
display: "flex",
width: "100vw",
position: "absolute",
top: "90%",
left: 0,
alignItems: "center",
justifyContent: "space-between",
padding: 10,
boxSizing: "border-box",
}}
direction="row"
>
{indiceCorrente > 0 && (
<IconButton onClick={() => cambiaindiceCorrente(indiceCorrente - 1)}>
<ArrowBackIcon style={{ fontSize: "3rem" }} />
</IconButton>
)}
{indiceCorrente < messaggi.length - 1 && (
<IconButton onClick={() => cambiaindiceCorrente(indiceCorrente + 1)}>
<ArrowForwardIcon style={{ fontSize: "3rem" }} />
</IconButton>
)}
</Stack>
</Stack>
);
};
function ImagePlaceholder({ messaggioDaMostrare }) { function ImagePlaceholder({ messaggioDaMostrare }) {
const [imageLoaded, setImageLoaded] = useState(false); const [imageLoaded, setImageLoaded] = useState(false);

Loading…
Cancel
Save