import Button from '@mui/material/Button'; import Grid from '@mui/material/Grid'; import Typography from '@mui/material/Typography'; import { useState, useEffect } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import type { PlayerProps, roomData, SpotifyAuthResponse } from '../interfaces'; import CreateRoomPage from './CreateRoomPage'; import Player from './Player'; import { Stack } from '@mui/material'; function Room() { let { roomCode } = useParams<{ roomCode: string }>(); const [votesToSkip, setVotesToSkip] = useState(2); const [guestCanPause, setGuestCanPause] = useState(false); const [isHost, setIsHost] = useState(false); const [error, setError] = useState(''); const [settings, setSettigs] = useState(false); const [isSpotifyAuth, setIsSpotifyAuth] = useState(false); const [playing, setPlaying] = useState(); const navigate = useNavigate(); function LeaveRoom() { const requestOptions: RequestInit = { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', }; fetch('/api/leave-room/', requestOptions) .then((response) => response.json()) .then((data) => { if (data.message) { console.log('leaving room ', data); navigate('/'); } }); } function checkAuth() { const requestOptions: RequestInit = { method: 'GET', headers: { 'Content-Type': 'application/json' }, credentials: 'include', }; fetch('/spotify/is-auth', requestOptions) .then((response) => response.json()) .then((data) => { if (data.status) { setIsSpotifyAuth(true); console.log('is-auth response', data.status); } }); } function renderSettingsButton() { return ( ); } function renderSerrings() { return ( ); } // on maunt and settings change useEffect(() => { if (!roomCode) { setError('No room code provided'); return; } fetch('/api/get-room/?code=' + roomCode, { credentials: 'include', }) .then((response) => { if (!response.ok) { navigate('/home'); } roomCode = undefined; return response.json(); }) .then((data: roomData) => { console.log('data:', data); setVotesToSkip(data.votes_to_skip); setGuestCanPause(data.guest_can_pause); console.log('data.isHost', data.isHost); setIsHost(data.isHost); }) .catch((err) => { setError(err.message); }); }, [settings]); ///get playing song useEffect(() => { function getSong() { fetch('/spotify/current-song', { credentials: 'include', }) .then((response) => response.json()) .then((data) => { console.log('📡 fetchData called : ', data); setPlaying(data); }) .catch((err) => { console.log('❌ Error fetching data:', err); }); } checkAuth(); if (isSpotifyAuth === true) { getSong(); const intervalID = setInterval(() => { getSong(); }, 1000); return () => { clearInterval(intervalID); }; } console.log('isSpotifyAuth ', isSpotifyAuth); }, [isSpotifyAuth]); function authenticateSpotify() { if (isHost && !isSpotifyAuth) { setIsSpotifyAuth(true); console.log('isSpotifyAuth.current:', isSpotifyAuth); fetch('/spotify/is-auth', { credentials: 'include', }) .then((response) => response.json()) .then((data: SpotifyAuthResponse) => { // isSpotifyAuth.current = data.status; if (!data.status) { fetch(`/spotify/get-auth-url?state=${roomCode}`, { credentials: 'include', }).then((response) => response.json().then((data) => { //redirect to spotify upstream auth window.location.replace(data.url); return; }), ); } }); } else { console.log('Clicked Utenticate button '); console.log('isSpotifyAuth:', isSpotifyAuth); return; } } if (error) { return
Error: {error}
; } if (settings) { console.log('rendering settings', settings); return renderSerrings(); } else { return ( Room: {roomCode} {playing ? : Nothing playing} Votes to Skip: {votesToSkip} Guest Can Pause: {guestCanPause ? 'Yes' : 'No'} Is Host: {isHost ? 'Yes' : 'No'} {isHost ? renderSettingsButton() : null} ); } } export default Room;