using Moment.js to generate the create grid
authorJane Sandberg <sandbej@linnbenton.edu>
Wed, 27 Mar 2019 02:21:25 +0000 (19:21 -0700)
committerJane Sandberg <sandbej@linnbenton.edu>
Wed, 27 Mar 2019 02:21:25 +0000 (19:21 -0700)
Open-ILS/src/eg2/src/app/staff/booking/create-reservation.component.ts

index f2da2d1..3563ad6 100644 (file)
@@ -9,6 +9,8 @@ import {IdlObject} from '@eg/core/idl.service';
 import {PcrudService} from '@eg/core/pcrud.service';
 import {ToastService} from '@eg/share/toast/toast.service';
 
+import * as Moment from 'moment-timezone';
+
 
 @Component({
     templateUrl: './create-reservation.component.html'
@@ -21,10 +23,9 @@ export class CreateReservationComponent implements OnInit {
     multiday = false;
     isBooked: (col: any, row: any) => string;
     resourceAvailabilityIcon: (row: any) => GridRowFlairEntry;
-    fetchData: (limiter: 'resource' | 'type', id: number) => void;
 
-    startTime: NgbTimeStruct = {hour: 9, minute: 0, second: 0};
-    endTime: NgbTimeStruct = {hour: 17, minute: 0, second: 0};
+    startTime: Moment;
+    endTime: Moment;
     granularity: 15 | 30 | 60 = 30;
 
     scheduleSource: GridDataSource = new GridDataSource();
@@ -40,58 +41,29 @@ export class CreateReservationComponent implements OnInit {
         private pcrud: PcrudService,
         private toast: ToastService,
     ) {
+        this.resourceAvailabilityIcon = (row: any) => {
+            let icon = {icon: 'event_busy', title: 'All resources are reserved at this time'};
+            let busy_columns = 0;
+            for (const key in row) {
+                if (row[key]) { busy_columns = busy_columns + 1; }
+            }
+            if (busy_columns <= this.resources.length) { // equal or less than, since it counts the time column
+                icon = {icon: 'event_available', title: 'Resources are available at this time'};
+            }
+            return icon;
+        };
     }
 
 
     ngOnInit() {
         this.dateLimiter.initialDate = new Date();
 
-        this.fetchData = (limiter: 'resource' | 'type', id: number) => {
-            this.resources = [];
-            let where = {};
-            if ('type' === limiter) {
-                where = {type: id};
-            } else if ('resource' === limiter) {
-                where = {id: id};
-            }
-            this.pcrud.search('brsrc', where, {
-                order_by: 'barcode ASC',
-                flesh: 1,
-                flesh_fields: {'brsrc': ['curr_rsrcs']},
-                select: {'curr_rsrcs': {'end_time': {'<' : '2019-04-01'}}}
-            }).subscribe(
-                r => { this.resources.push(r); });
-
-            this.scheduleSource.data = [];
-            let current_time = this.startTime;
-            while (this._firstTimeIsSmaller(current_time, this.endTime)) {
-                this.scheduleSource.data.push({
-                    'time': current_time.hour + ':' + current_time.minute,
-                    'ROOM1231': 'Professor Pickle'
-                });
-                if ((current_time.minute + this.granularity) >= 60) {
-                    current_time.hour = current_time.hour + 1;
-                }
-                current_time.minute = (current_time.minute + this.granularity) % 60;
-            }
-        };
 
         this.isBooked = (row: any, col: any) => {
             if ((col.name !== 'time') && (row[col.name])) {
                 return 'bg-warning';
             }
         };
-        this.resourceAvailabilityIcon = (row: any) => {
-            let icon = {icon: 'event_busy', title: 'All resources are reserved at this time'};
-            let busy_columns = 0;
-            for (const key in row) {
-                if (row[key]) { busy_columns = busy_columns + 1; }
-            }
-            if (busy_columns <= this.resources.length) { // equal or less than, since it counts the time column
-                icon = {icon: 'event_available', title: 'Resources are available at this time'};
-            }
-            return icon;
-        };
 
         this.limitByAttr = (attributeId: number, $event: ComboboxEntry) => {
             console.log('LIMIT');
@@ -99,7 +71,6 @@ export class CreateReservationComponent implements OnInit {
             console.log('event: ' + JSON.stringify($event));
         };
 
-
     }
     showNewDialog(idlThing: IdlObject) {
         return this.newDialog.open({size: 'lg'}).then(
@@ -110,25 +81,60 @@ export class CreateReservationComponent implements OnInit {
             err => {}
         );
     }
-
-    this.handleResourceTypeChange($event: ComboboxEntry) {
-            // TODO: unset resource barcode
+    handleResourceTypeChange(event: ComboboxEntry) {
+        // TODO: unset resource barcode
         this.attributes = [];
-        if ($event.id) {
-            this.pcrud.search('bra', {resource_type : $event.id}, {
+        if (event.id) {
+            this.pcrud.search('bra', {resource_type : event.id}, {
                 order_by: 'name ASC',
                 flesh: 1,
                 flesh_fields: {'bra' : ['valid_values']}
             }).subscribe(
-                a => { this.attributes.push(a); });
-            this.fetchData('type', $event.id);
+                a => { this.attributes.push(a);
+                }, err => {
+                    console.log(err);
+                }, () => {
+                    this.fetchData('type', event.id);
+                });
         }
     }
 
-    private _firstTimeIsSmaller(first: NgbTimeStruct, second: NgbTimeStruct) {
-        if (first.hour < second.hour) { return true; }
-        if ((first.hour === second.hour) && (first.minute < second.minute)) { return true; }
-        return false;
+    fetchData (limiter: 'resource' | 'type', id: number) {
+        this.resources = [];
+        let where = {};
+        if ('type' === limiter) {
+            where = {type: id};
+        } else if ('resource' === limiter) {
+            where = {id: id};
+        }
+        this.pcrud.search('brsrc', where, {
+            order_by: 'barcode ASC',
+            flesh: 1,
+            flesh_fields: {'brsrc': ['curr_rsrcs']},
+            select: {'curr_rsrcs': {'end_time': {'<' : '2019-04-01'}}}
+        }).subscribe(
+            r => { this.resources.push(r); });
+
+        this.scheduleSource.data = [];
+        this.startTime = Moment(new Date(
+            this.dateLimiter.current.year,
+            this.dateLimiter.current.month - 1,
+            this.dateLimiter.current.day,
+            9, 0, 0), 'Asia/Tokyo');
+        this.endTime = Moment(new Date(
+            this.dateLimiter.current.year,
+            this.dateLimiter.current.month - 1,
+            this.dateLimiter.current.day,
+            17, 0, 0), 'Asia/Tokyo');
+        let currentTime = this.startTime;
+        while (currentTime < this.endTime) {
+            this.scheduleSource.data.push({
+                'time': currentTime.format('LT'),
+                'ROOM1231': 'Professor Pickle'
+            });
+            currentTime.minute(currentTime.minute() + this.granularity);
+        }
     }
+
 }