From 163375f62d1b3bdf20869bed37df52865e335a9d Mon Sep 17 00:00:00 2001
From: Anton Vakhrushev <anwinged@ya.ru>
Date: Thu, 2 Apr 2020 12:15:53 +0300
Subject: [PATCH] Add notifications

---
 src/Dashboard.ts | 42 +++++++++++++++++++++++++++++++-----------
 src/Scheduler.ts |  1 +
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/Dashboard.ts b/src/Dashboard.ts
index df15a40..a12d259 100644
--- a/src/Dashboard.ts
+++ b/src/Dashboard.ts
@@ -1,5 +1,5 @@
 import * as URLParse from 'url-parse';
-import { markPage, sleep, uniqId } from './utils';
+import { markPage, sleepShort, uniqId } from './utils';
 import Scheduler from './Scheduler';
 import UpgradeBuildingTask from './Task/UpgradeBuildingTask';
 import { Command } from './Common';
@@ -16,13 +16,14 @@ export default class Dashboard {
 
     async run() {
         await this.load();
-        await sleep(1000);
+        await sleepShort();
 
         const p = new URLParse(window.location.href, true);
-        console.log('PARSED LOCATION', p);
+        this.log('PARSED LOCATION', p);
 
         markPage('Dashboard', this.version);
-        new TaskQueueRenderer().render(this.scheduler.getTaskItems());
+        this.renderTaskQueue();
+        setInterval(() => this.renderTaskQueue(), 5000);
 
         if (p.pathname === '/dorf1.php') {
             this.showSlotIds('buildingSlot');
@@ -33,21 +34,32 @@ export default class Dashboard {
         }
 
         if (p.pathname === '/build.php') {
-            console.log('BUILD PAGE DETECTED');
+            this.log('BUILD PAGE DETECTED');
             const id = uniqId();
             jQuery('.upgradeButtonsContainer .section1').append(
                 `<div style="padding: 8px"><a id="${id}" href="#">В очередь</a></div>`
             );
-            jQuery(`#${id}`).on('click', () => {
-                const queueItem = new Command(UpgradeBuildingTask.NAME, {
-                    id: p.query['id'],
-                });
-                this.scheduler.scheduleTask(queueItem);
-                return false;
+            jQuery(`#${id}`).on('click', evt => {
+                evt.preventDefault();
+                this.onScheduleBuilding(p.query.id || '');
             });
         }
     }
 
+    private renderTaskQueue() {
+        this.log('RENDER TASK QUEUE');
+        new TaskQueueRenderer().render(this.scheduler.getTaskItems());
+    }
+
+    private onScheduleBuilding(id: string) {
+        const queueItem = new Command(UpgradeBuildingTask.NAME, {
+            id,
+        });
+        this.scheduler.scheduleTask(queueItem);
+        const n = new Notification(`Building ${id} scheduled`);
+        setTimeout(() => n && n.close(), 4000);
+    }
+
     private showSlotIds(prefix: string) {
         jQuery('.level.colorLayer').each((idx, el) => {
             let num = '';
@@ -68,4 +80,12 @@ export default class Dashboard {
     private async load() {
         return new Promise(resolve => jQuery(resolve));
     }
+
+    private log(...args) {
+        console.log('SCHEDULER:', ...args);
+    }
+
+    private logError(...args) {
+        console.error(...args);
+    }
 }
diff --git a/src/Scheduler.ts b/src/Scheduler.ts
index ead8030..dac50de 100644
--- a/src/Scheduler.ts
+++ b/src/Scheduler.ts
@@ -26,6 +26,7 @@ export default class Scheduler {
         await sleepShort();
         markPage('Executor', this.version);
 
+        this.renderTaskQueue();
         setInterval(() => this.renderTaskQueue(), 5000);
 
         while (true) {