import React, { useState, useEffect } from 'react'; import { Card, CardHeader, CardBody } from '@custom/shared/components/Card'; import Loader from '@custom/shared/components/Loader'; import Well from '@custom/shared/components/Well'; import PropTypes from 'prop-types'; export const CreatingRoom = ({ onCreated }) => { const [room, setRoom] = useState(); const [fetching, setFetching] = useState(false); const [error, setError] = useState(false); useEffect(() => { if (room) return; async function createRoom() { setError(false); setFetching(true); console.log(`🚪 Creating new demo room...`); // Create a room server side (using Next JS serverless) const res = await fetch('/api/createRoom', { method: 'POST', headers: { 'Content-Type': 'application/json', }, }); const resJson = await res.json(); if (resJson.name) { setFetching(false); setRoom(resJson.name); return; } setError(resJson.error || 'An unknown error occured'); setFetching(false); } createRoom(); }, [room]); useEffect(() => { if (!room || !onCreated) return; console.log(`🚪 Room created: ${room}, joining now`); onCreated(room, true); }, [room, onCreated]); return (
{fetching && (
Creating new demo room...
)} {error && ( An error occured {error} An error occured when trying to create a demo room. Please check that your environmental variables are correct and try again. )}
); }; CreatingRoom.propTypes = { onCreated: PropTypes.func.isRequired, }; export default CreatingRoom;