Created
December 28, 2021 19:57
-
-
Save walterjaworski/ea7bd2ca88fb02406041c61caddea5c2 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 { useEffect } from 'react'; | |
import { FormEvent, useState } from 'react'; | |
import { useParams } from 'react-router-dom' | |
import logoImg from '../assets/images/logo.svg'; | |
import { Button } from '../components/Button'; | |
import { RoomCode } from '../components/RoomCode'; | |
import { useAuth } from '../hooks/useAuth'; | |
import { database } from '../services/firebase'; | |
import '../styles/room.scss'; | |
type FirebaseQuestions = Record<string, { | |
author: { | |
name: string; | |
avatar: string; | |
} | |
content: string; | |
isAnswered: boolean; | |
isHighlighted: boolean; | |
}> | |
type Question = { | |
id: string; | |
author: { | |
name: string; | |
avatar: string; | |
} | |
content: string; | |
isAnswered: boolean; | |
isHighlighted: boolean; | |
} | |
type RoomParams = { | |
id: string; | |
} | |
export function Room() { | |
const { user } = useAuth(); | |
const params = useParams<RoomParams>(); | |
const [newQuestion, setNewQuestion] = useState(''); | |
const [questions, setQuestions] = useState<Question[]>([]) | |
const [title, setTitle] = useState(''); | |
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(event: FormEvent) { | |
event.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} /> | |
{/* <RoomCode code='-Ms0SQp-LGyM6BeAZG8b' /> */} | |
</div> | |
</header> | |
<main> | |
<div className="room-title"> | |
<h1>Sala {title}</h1> | |
{ questions.length > 0 && <span>{questions.length} pergunta(s)</span> } | |
</div> | |
<form onSubmit={handleSendQuestion}> | |
<textarea | |
placeholder="O que você quer perguntar?" | |
onChange={event => setNewQuestion(event.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> | |
{JSON.stringify(questions)} | |
</main> | |
</div> | |
); | |
} |
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 copyImg from '../assets/images/copy.svg' | |
import '../styles/room-code.scss' | |
type RoomCodeProps = { | |
code: string; | |
} | |
export function RoomCode(props: RoomCodeProps) { | |
function copyRoomCodeToClipboard() { | |
navigator.clipboard.writeText(props.code) | |
} | |
return ( | |
<button className="room-code" onClick={copyRoomCodeToClipboard}> | |
<div> | |
<img src={copyImg} alt="Copy room code" /> | |
</div> | |
<span>Sala #{props.code}</span> | |
</button> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment