Created
June 26, 2021 04:17
-
-
Save MoranggNormal/474524fc4188f1d3ca665a87d9524ce5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FormEvent, useState, useEffect } from "react"; | |
import { useParams } from "react-router-dom"; | |
import { Question } from '../components/Question' | |
import {database} from "../services/firebase" | |
import {useAuth} from '../Hooks/useAuths' | |
import {Button} from "../components/Button"; | |
import {RoomCode} from '../components/RoomCode' | |
import logoImg from "../assets/images/logo.svg"; | |
import '../styles/room.scss' | |
type FirebaseQuestions = Record<string, { | |
author: { | |
name: string; | |
avatar: string; | |
} | |
content: string; | |
isAnswered: boolean; | |
isHighLighted: boolean; | |
}> | |
type QuestionType = { | |
id: string; | |
author: { | |
name: string; | |
avatar: string; | |
} | |
content: string; | |
isAnswered: boolean; | |
isHighLighted: boolean; | |
} | |
type RoomsParams = { | |
id: string; | |
} | |
export default function Room() { | |
const {user} = useAuth() | |
const [newQuestion, setNewQuestion] = useState(''); | |
const [questions, setQuestions] = useState<QuestionType[]>([]); | |
const [title, setTitle] = useState('') | |
const params = useParams<RoomsParams>(); | |
const roomId = params.id; | |
useEffect(() => { | |
const roomRef = database.ref(`rooms/${roomId}`); | |
roomRef.on('value', room => { | |
const databaseRoom = room.val() | |
const firebaseQuestions: FirebaseQuestions = databaseRoom.questions ?? {}; | |
const parsedQuestions = Object.entries(firebaseQuestions).map(([key, value]) => { | |
return { | |
id: key, | |
content: value.content, | |
author: value.author, | |
isHighLighted: value.isHighLighted, | |
isAnswered: value.isAnswered | |
} | |
}) | |
setTitle(databaseRoom.title) | |
setQuestions(parsedQuestions) | |
}) | |
}, [roomId]) | |
async function handleSendQuestion(e: FormEvent) { | |
e.preventDefault(); | |
if(newQuestion.trim() === ''){ | |
return | |
} | |
if(!user){ | |
throw new Error('You must be logged in'); | |
} | |
const question = { | |
content: newQuestion, | |
author: { | |
name: user.name, | |
avatar: user.avatar, | |
}, | |
isHighLighted: false, | |
isAnswered: false | |
} | |
await database.ref(`rooms/${roomId}/questions`).push(question); | |
setNewQuestion('') | |
} | |
return ( | |
<div id="page-room"> | |
<header> | |
<div className="content"> | |
<img src={logoImg} alt="Letmeask" /> | |
<RoomCode code={roomId} /> | |
</div> | |
</header> | |
<main> | |
<div className="room-title"> | |
<h1>Sala: {title}</h1> | |
{questions.length > 0 && <span>{questions.length} perguntas</span>} | |
</div> | |
<form onSubmit={handleSendQuestion}> | |
<textarea | |
placeholder="O que você quer perguntar?" | |
onChange={e => setNewQuestion(e.target.value)} | |
value={newQuestion} | |
/> | |
<div className="form-footer"> | |
{user ? ( | |
<div className="user-info"> | |
<img src={user.avatar} alt="user.name" /> | |
<span>{user.name}</span> | |
</div> | |
) : ( | |
<span>Para enviar uma pergunta, <button>faça seu login.</button></span> | |
)} | |
<Button type="submit" disabled={!user}>Enviar pergunta</Button> | |
</div> | |
</form> | |
<div className="question-list"> | |
{questions.map(question => { | |
return ( | |
<Question | |
key={question.id} | |
content={question.content} | |
author={question.author} | |
/> | |
) | |
})} | |
</div> | |
</main> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment