7 changed files with 242 additions and 189 deletions
@ -0,0 +1,52 @@ |
|||||||
|
import React, { useEffect, useState } from "react"; |
||||||
|
import firebase from "firebase/app"; |
||||||
|
import { Stack } from "@mui/system"; |
||||||
|
import { CheckboxListSecondary } from "./index"; |
||||||
|
|
||||||
|
export const Admin = () => { |
||||||
|
const [messaggi, cambiaMessaggi] = useState([]); |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
// Get a reference to the messages node in the Realtime Database
|
||||||
|
var messagesRef = firebase.database().ref("/messaggi"); |
||||||
|
console.log(messagesRef); |
||||||
|
|
||||||
|
messagesRef.on("value", function (snapshot) { |
||||||
|
var messaggi = snapshot.val(); |
||||||
|
console.log(messaggi); |
||||||
|
messaggi = Object.keys(messaggi).map((d) => messaggi[d]); |
||||||
|
cambiaMessaggi(messaggi); |
||||||
|
}); |
||||||
|
|
||||||
|
messagesRef.on("child_changed", function (snapshot) { |
||||||
|
var messaggio = snapshot.val(); |
||||||
|
const i = messaggi.findIndex((m) => messaggio.timestamp === m.timestamp); |
||||||
|
}); |
||||||
|
}, []); |
||||||
|
|
||||||
|
let element; |
||||||
|
if (!messaggi.length) { |
||||||
|
element = <div>nessun messaggio</div>; |
||||||
|
} else { |
||||||
|
element = ( |
||||||
|
<CheckboxListSecondary |
||||||
|
messaggi={messaggi} |
||||||
|
onChecked={(m) => { |
||||||
|
firebase |
||||||
|
.database() |
||||||
|
.ref("messaggi/" + m.id) |
||||||
|
.update({ |
||||||
|
approvato: !m.approvato, |
||||||
|
}); |
||||||
|
}} |
||||||
|
/> |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
return ( |
||||||
|
<Stack> |
||||||
|
<h2>Admin</h2> |
||||||
|
{element} |
||||||
|
</Stack> |
||||||
|
); |
||||||
|
}; |
||||||
@ -1,38 +0,0 @@ |
|||||||
.App { |
|
||||||
text-align: center; |
|
||||||
} |
|
||||||
|
|
||||||
.App-logo { |
|
||||||
height: 40vmin; |
|
||||||
pointer-events: none; |
|
||||||
} |
|
||||||
|
|
||||||
@media (prefers-reduced-motion: no-preference) { |
|
||||||
.App-logo { |
|
||||||
animation: App-logo-spin infinite 20s linear; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
.App-header { |
|
||||||
background-color: #282c34; |
|
||||||
min-height: 100vh; |
|
||||||
display: flex; |
|
||||||
flex-direction: column; |
|
||||||
align-items: center; |
|
||||||
justify-content: center; |
|
||||||
font-size: calc(10px + 2vmin); |
|
||||||
color: white; |
|
||||||
} |
|
||||||
|
|
||||||
.App-link { |
|
||||||
color: #61dafb; |
|
||||||
} |
|
||||||
|
|
||||||
@keyframes App-logo-spin { |
|
||||||
from { |
|
||||||
transform: rotate(0deg); |
|
||||||
} |
|
||||||
to { |
|
||||||
transform: rotate(360deg); |
|
||||||
} |
|
||||||
} |
|
||||||
@ -0,0 +1,65 @@ |
|||||||
|
import React, { useState } from "react"; |
||||||
|
import firebase from "firebase/app"; |
||||||
|
import TextField from "@mui/material/TextField"; |
||||||
|
import { Container, Stack } from "@mui/system"; |
||||||
|
import { Button } from "@mui/material"; |
||||||
|
import { useForm } from "react-hook-form"; |
||||||
|
import Countdown from "./Countdown"; |
||||||
|
import ImageUpload from "./ImageUpload"; |
||||||
|
|
||||||
|
export const MyForm = () => { |
||||||
|
const [mandato, cambiaMandato] = useState(false); |
||||||
|
const [immagineURL, cambiaimmagineURL] = useState(null); |
||||||
|
const { register, handleSubmit } = useForm(); |
||||||
|
const onSubmit = ({ testo, autore }) => { |
||||||
|
cambiaMandato(true); |
||||||
|
var postListRef = firebase.database().ref("messaggi"); |
||||||
|
var newPostRef = postListRef.push(); |
||||||
|
const update = { |
||||||
|
id: newPostRef.key, |
||||||
|
autore, |
||||||
|
testo, |
||||||
|
timestamp: firebase.database.ServerValue.TIMESTAMP, |
||||||
|
approvato: true, |
||||||
|
}; |
||||||
|
if (immagineURL) update.immagineURL = immagineURL; |
||||||
|
newPostRef.set(update); |
||||||
|
}; |
||||||
|
|
||||||
|
const onImageURLSet = (url) => { |
||||||
|
cambiaimmagineURL(url); |
||||||
|
}; |
||||||
|
|
||||||
|
if (mandato) { |
||||||
|
return ( |
||||||
|
<Stack> |
||||||
|
<div>mandato e mo aspetti</div> |
||||||
|
<Countdown onComplete={() => cambiaMandato(false)} duration={5} /> |
||||||
|
</Stack> |
||||||
|
); |
||||||
|
} |
||||||
|
return ( |
||||||
|
<Container maxWidth="sm" style={{ marginTop: 100 }}> |
||||||
|
<form onSubmit={handleSubmit(onSubmit)}> |
||||||
|
<Stack spacing={2}> |
||||||
|
<ImageUpload onImageURLSet={onImageURLSet} /> |
||||||
|
<TextField |
||||||
|
{...register("testo", { required: true, maxLength: 20 })} |
||||||
|
id="outlined-basic" |
||||||
|
label="messaggio" |
||||||
|
variant="outlined" |
||||||
|
/> |
||||||
|
<TextField |
||||||
|
{...register("autore", { pattern: /^[A-Za-z]+$/i })} |
||||||
|
id="outlined-basic" |
||||||
|
label="da" |
||||||
|
variant="outlined" |
||||||
|
/> |
||||||
|
<Button type="submit" variant="contained"> |
||||||
|
manda |
||||||
|
</Button> |
||||||
|
</Stack> |
||||||
|
</form> |
||||||
|
</Container> |
||||||
|
); |
||||||
|
}; |
||||||
Loading…
Reference in new issue