import m from 'mithril';
import { Page } from 'components/page';
import { MonitorApi, Status } from 'monitor-api';
import Clock from 'components/widgets/clock';

export class DashboardPage extends Page {
	status: Status = null;
	autoRefresh = true;
	
	oninit() {
		this.status = MonitorApi.get().getStatus();
		this.refresh();
	}
	
	onbeforeremove(vnode: m.Vnode<{}, {}>) {
    	this.autoRefresh = false;
	}
	
	async refresh() {
		this.status = await MonitorApi.get().fetchStatus();
		if(this.autoRefresh)
			setTimeout(() => { if(this.autoRefresh) this.refresh(); }, 500);
	}
	
	view() {
		return this.status
			? <div class="dashboard-page">
				<Clock/>
			</div>
			: <p>Chargement...</p>;
	}
}
