import { Component, OnInit, Inject, ElementRef, ViewChild } from '@angular/core'; import { MatDialogRef, MatDialog, MatSnackBar } from '@angular/material'; import { ErrorComponent } from '../error/error.component'; import { EvaluationsService } from '../evaluations/evaluations.service'; declare var openPicker: any; declare var onApiLoad: any; declare var start: any; @Component({ selector: 'add-evaluation', templateUrl: './add-evaluation.component.html', styleUrls: ['./add-evaluation.component.scss'] }) export class AddEvaluationsComponent implements OnInit { @ViewChild("avalNameInput") inputEl: ElementRef; evaluationService : EvaluationsService; public dialogRef : MatDialogRef; errDialog : MatDialog; selectedForm; selectedSheet; formName; sheetName; constructor(evaluationService : EvaluationsService,dialogRef: MatDialogRef, errDialog : MatDialog) { this.evaluationService = evaluationService; this.dialogRef = dialogRef; this.errDialog = errDialog; } ngOnInit() { onApiLoad(); this.formName = ''; this.sheetName = ''; } onFormButtonClick(){ const callback = (result) => { this.selectedForm = result; this.formName = this.selectedForm.name; this.inputEl.nativeElement.focus(); console.log(this.selectedForm); } start('f', callback); } onCloseClick() { this.dialogRef.close(); } onSheetButtonClick(){ const callback = (result) => { this.selectedSheet = result; this.sheetName = result.name; this.inputEl.nativeElement.focus(); console.log(this.selectedSheet); } start('s', callback); } checkIsValidName(name) { if (name.length <= 0 || name.length > 20) { return false; } return true; } onSubmit(submittedForm) { //TODO: //Colocar mensagem de erro se n tiver form ou sheet const evaluationData = { evaname: submittedForm.value.evaname, sheet: this.selectedSheet, evadesc: submittedForm.value.evadesc } let errMsg = 'Erro ao tentar adicionar uma avaliação:\n\n'; let hasError = false; if (evaluationData.sheet == undefined) { hasError = true; errMsg += "\u2022 Nenhuma planilha foi selecionada\n" } if (!this.checkIsValidName(evaluationData.evaname)) { hasError = true; errMsg += "\u2022 O nome da avaliação deve ter entre 1 e 20 caracteres\n" } if (!hasError) { const callback = () => { window.alert('Avaliacao salva com sucesso!'); //this.router.navigate(['main']); this.dialogRef.close(); } const errHandler = () => { this.openErrDialog('Algum problema ocorreu e a avaliação não foi salva!'); this.dialogRef.close(); } this.evaluationService.saveEvaluation(evaluationData, callback, errHandler); } else { this.openErrDialog(errMsg); } } openErrDialog(message) { let errDialogRef = this.errDialog.open(ErrorComponent, { height: '400px', width: '400px' }); errDialogRef.componentInstance.errorMessage = message; } }