import { BoxOverview, Participant, RecordingContent, VirtualBox } from './../../../interfaces/boxes'; import { BoxAssignmentComponent } from './../box-assignment/box-assignment.component'; import { BytefliesService } from './../../../services/byteflies/byteflies.service'; import { Component, OnInit, Input, OnChanges, Output, EventEmitter, ViewChild } from '@angular/core'; import { MdSnackBar } from '@angular/material'; import { parseDate } from 'app/utils/utils'; import { fadeInFadeOut } from 'app/animations/component.animations'; @Component({ selector: 'app-box', templateUrl: './box.component.html', styleUrls: ['./box.component.scss'], animations: [fadeInFadeOut()] }) export class BoxComponent implements OnChanges { @Input('box') box: BoxOverview; @Input() assigning = false; @Output() onAssigning = new EventEmitter(); @Output() reloadWanted = new EventEmitter(); @ViewChild('boxAssignment') boxAssignment: BoxAssignmentComponent; participants: Participant[]; virtualBoxes: { participant: Participant; recordings: RecordingContent[]; virtual_box: VirtualBox; }[]; saving: boolean; constructor(private service: BytefliesService, public snackBar: MdSnackBar) { } showAssigning() { this.onAssigning.next(true); } hideAssigning() { this.onAssigning.next(false); } saveAssigning() { if (this.boxAssignment.form.valid) { this.saving = true; const value = this.boxAssignment.form.value; const assignmentPromise = this.service.assignPatient({ patientId: value.patientId, startTime: value.startDatetime, endTime: value.endDatetime, boxId: this.box.box.id }, this.box.box.experiment); assignmentPromise .then(_ => { this.hideAssigning(); this.snackBar.open(`Box ${this.box.box.alias} reassigned to patient ${value.patientId}`, 'dismiss', { duration: 5000, extraClasses: ['success'] }); }, e => { this.snackBar.open(e.message, 'dismiss', { duration: 5000, extraClasses: ['error'] }); return e; }) .then(_ => this.saving = false) .then(_ => this.reloadBox()); } } ngOnChanges(changes) { if (changes.box) { this.virtualBoxes = Object.keys(this.box.virtual_boxes) .map(key => this.box.virtual_boxes[key]); } } reloadBox() { this.reloadWanted.next(); } }