import React, { useRef, useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import moment from 'moment-timezone';
import { Button, Container, Header } from 'semantic-ui-react';
import { requester } from './utils.js';
const deviceNames = {
'trotec': {title: 'Trotec', device: 'TROTECS300'},
};
export function Usage(props) {
const { token } = props;
const { name } = useParams();
const title = deviceNames[name].title;
const device = deviceNames[name].device;
const [usage, setUsage] = useState(false);
const [fullElement, setFullElement] = useState(false);
const ref = useRef(null);
const getUsage = () => {
requester('/stats/usage_data/?device='+device, 'GET', token)
.then(res => {
setUsage(res);
})
.catch(err => {
console.log(err);
setUsage(false);
});
};
useEffect(() => {
getUsage();
const interval = setInterval(getUsage, 60000);
return () => clearInterval(interval);
}, []);
const goFullScreen = () => {
if ('wakeLock' in navigator) {
navigator.wakeLock.request('screen');
}
ref.current.requestFullscreen({ navigationUI: 'hide' }).then(() => {
setFullElement(true);
});
};
const inUse = usage && moment().unix() - usage.track.time <= 60;
const showUsage = usage && inUse && usage.track.username === usage.username;
return (
Waiting for job
{usage.first_name}
{parseInt(usage.session_time / 60)} mins
{today_total} mins
{month_total} mins
{month_total <= free_time ? <>{free_time - month_total} mins
> : <>${((month_total - free_time) * 0.5).toFixed(2)}
> } > ); }