From 1f7ebf8e2279059f5d6b5fdde3f6a6e5fa618013 Mon Sep 17 00:00:00 2001 From: Jon Date: Tue, 20 Jul 2021 11:05:11 +0100 Subject: [PATCH] expiry timer --- dailyjs/basic-call/components/App/App.js | 73 +++----- dailyjs/basic-call/pages/api/createRoom.js | 5 +- .../live-fitness/components/Splash/Splash.js | 170 +++++++++++++++--- dailyjs/live-fitness/pages/[room].js | 101 +++++++++++ dailyjs/live-fitness/pages/index.js | 17 +- dailyjs/live-fitness/pages/not-found.js | 21 +++ .../components/ExpiryTimer/ExpiryTimer.js | 47 +++++ .../shared/components/ExpiryTimer/index.js | 1 + .../shared/components/HairCheck/HairCheck.js | 14 +- dailyjs/shared/components/Loader/Loader.js | 16 +- dailyjs/shared/contexts/UIStateProvider.js | 4 +- dailyjs/shared/hooks/useCallUI.js | 16 ++ 12 files changed, 385 insertions(+), 100 deletions(-) create mode 100644 dailyjs/live-fitness/pages/not-found.js create mode 100644 dailyjs/shared/components/ExpiryTimer/ExpiryTimer.js create mode 100644 dailyjs/shared/components/ExpiryTimer/index.js diff --git a/dailyjs/basic-call/components/App/App.js b/dailyjs/basic-call/components/App/App.js index 51d3524..e025f3a 100644 --- a/dailyjs/basic-call/components/App/App.js +++ b/dailyjs/basic-call/components/App/App.js @@ -1,4 +1,5 @@ import React, { useState, useEffect, useMemo } from 'react'; +import ExpiryTimer from '@dailyjs/shared/components/ExpiryTimer'; import { useCallState } from '@dailyjs/shared/contexts/CallProvider'; import { useCallUI } from '@dailyjs/shared/hooks/useCallUI'; @@ -8,19 +9,6 @@ import { Modals } from './Modals'; export const App = () => { const { roomExp, state } = useCallState(); - const [secs, setSecs] = useState(); - - // If room has an expiry time, we'll calculate how many seconds until expiry - useEffect(() => { - if (!roomExp) { - return false; - } - const i = setInterval(() => { - const timeLeft = Math.round((roomExp - Date.now()) / 1000); - setSecs(`${Math.floor(timeLeft / 60)}:${`0${timeLeft % 60}`.slice(-2)}`); - }, 1000); - return () => clearInterval(i); - }, [roomExp]); const componentForState = useCallUI({ state, @@ -28,48 +16,29 @@ export const App = () => { }); // Memoize children to avoid unnecassary renders from HOC - const memoizedApp = useMemo( + return useMemo( () => ( -
- {componentForState()} - - - -
+ .loader { + margin: 0 auto; + } + `} + + ), - [componentForState] - ); - - return ( - <> - {roomExp &&
{secs}
} {memoizedApp} - - + [componentForState, roomExp] ); }; diff --git a/dailyjs/basic-call/pages/api/createRoom.js b/dailyjs/basic-call/pages/api/createRoom.js index 1c8ff2b..d8e4a02 100644 --- a/dailyjs/basic-call/pages/api/createRoom.js +++ b/dailyjs/basic-call/pages/api/createRoom.js @@ -1,4 +1,6 @@ export default async function handler(req, res) { + const { privacy, expiryMinutes } = req.body; + if (req.method === 'POST') { console.log(`Creating room on domain ${process.env.DAILY_DOMAIN}`); @@ -9,8 +11,9 @@ export default async function handler(req, res) { Authorization: `Bearer ${process.env.DAILY_API_KEY}`, }, body: JSON.stringify({ + privacy: privacy || 'public', properties: { - exp: Math.round(Date.now() / 1000) + 5 * 60, // expire in 5 minutes + exp: Math.round(Date.now() / 1000) + (expiryMinutes || 5) * 60, // expire in x minutes eject_at_room_exp: true, }, }), diff --git a/dailyjs/live-fitness/components/Splash/Splash.js b/dailyjs/live-fitness/components/Splash/Splash.js index 798a7bb..93068e8 100644 --- a/dailyjs/live-fitness/components/Splash/Splash.js +++ b/dailyjs/live-fitness/components/Splash/Splash.js @@ -1,17 +1,23 @@ import React, { useState } from 'react'; import Button from '@dailyjs/shared/components/Button'; +import Loader from '@dailyjs/shared/components/Loader'; +import { Well } from '@dailyjs/shared/components/Well'; import PropTypes from 'prop-types'; -export const Splash = ({ domain, onJoin, isConfigured }) => { - // const [joinAsInstructor, setJoinAsInstructor] = useState(false); +/** + * Splash + * --- + * - Checks our app is configured properly + * - Creates a new Daily room for this session + * - Calls the onJoin method with the room name and instructor (owner) status + */ +export const Splash = ({ onJoin, isConfigured }) => { const [fetching, setFetching] = useState(false); const [error, setError] = useState(false); - const [room, setRoom] = useState(''); - - async function createRoom() { - // Create a room + async function createRoom(asInstructor) { + // Create a new room for this class setError(false); setFetching(true); @@ -23,13 +29,16 @@ export const Splash = ({ domain, onJoin, isConfigured }) => { headers: { 'Content-Type': 'application/json', }, + body: JSON.stringify({ + privacy: 'private', + expiryMinutes: 10, + }), }); const resJson = await res.json(); if (resJson.name) { - setFetching(false); - setRoom(resJson.name); + onJoin(resJson.name, asInstructor); return; } @@ -39,25 +48,111 @@ export const Splash = ({ domain, onJoin, isConfigured }) => { return (
-