2020-10-03 20:59:25 -07:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-10-08 00:17:40 -07:00
|
|
|
import { BROADCASTER, fetchData, FETCH_INTERVAL } from './utils/apis';
|
|
|
|
import Main from './home';
|
2020-10-03 20:59:25 -07:00
|
|
|
|
|
|
|
export default function Admin() {
|
|
|
|
const [broadcasterStatus, setBroadcasterStatus] = useState({});
|
2020-10-08 00:17:40 -07:00
|
|
|
const [count, setCount] = useState(0);
|
2020-10-03 20:59:25 -07:00
|
|
|
|
2020-10-08 00:17:40 -07:00
|
|
|
const getBroadcastStatus = async () => {
|
2020-10-03 20:59:25 -07:00
|
|
|
try {
|
|
|
|
const result = await fetchData(BROADCASTER);
|
2020-10-08 00:17:40 -07:00
|
|
|
const broadcastActive = !!result.broadcaster;
|
|
|
|
|
|
|
|
console.log("====",{count, result})
|
|
|
|
|
|
|
|
setBroadcasterStatus({ ...result, broadcastActive });
|
2020-10-10 18:48:29 -07:00
|
|
|
setCount(c => c + 1);
|
2020-10-03 20:59:25 -07:00
|
|
|
|
2020-10-03 23:07:37 -07:00
|
|
|
} catch (error) {
|
2020-10-03 20:59:25 -07:00
|
|
|
setBroadcasterStatus({ ...broadcasterStatus, message: error.message });
|
2020-10-08 00:17:40 -07:00
|
|
|
}
|
2020-10-03 20:59:25 -07:00
|
|
|
};
|
2020-10-08 00:17:40 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let getStatusIntervalId = null;
|
|
|
|
|
|
|
|
getBroadcastStatus();
|
|
|
|
getStatusIntervalId = setInterval(getBroadcastStatus, FETCH_INTERVAL);
|
|
|
|
|
|
|
|
// returned function will be called on component unmount
|
|
|
|
return () => {
|
|
|
|
clearInterval(getStatusIntervalId);
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
2020-10-03 20:59:25 -07:00
|
|
|
return (
|
2020-10-08 00:17:40 -07:00
|
|
|
<Main {...broadcasterStatus} />
|
2020-10-03 20:59:25 -07:00
|
|
|
);
|
|
|
|
}
|