import { Component , OnInit , AfterViewInit , Inject } from '@angular/core' ; import { MatDialogRef , MAT_DIALOG_DATA } from '@angular/material/dialog' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; import { I18nService } from '../service/i18n.service' ; import { LoggerService } from '../service/logger.service' ; import { HttpClient } from '@angular/common/http' ; import { MatSnackBar } from '@angular/material/snack-bar' ; import { EventbusService , EventType } from '../service/eventbus.service' ; import { getHost } from '../define' ; @ Component ( { selector : 'app-addroom' , templateUrl : './addroom.component.html' , styleUrls : [ './addroom.component.css' ] } ) export class AddroomComponent implements OnInit , AfterViewInit { roomForm : FormGroup ; name : string ; speakerPassword : string ; attendeePassword : string ; description ; rooms ; optUpdate = false ; dataRoom ; modules = { toolbar : [ [ 'bold' , 'italic' , 'underline' ] , [ { list : 'ordered' } , { list : 'bullet' } ] , [ { color : [ ] } ] , ] } ; constructor ( public dialogRef , public i18n , @ Inject ( MAT_DIALOG_DATA ) public data , private fb , private logger , private http , private snackbar , private eventbus ) { if ( data && data . room ) { this . optUpdate = true ; this . dataRoom = data . room ; this . name = data . room . name ; this . speakerPassword = data . room . speakerPassword ; this . attendeePassword = data . room . attendeePassword ; this . description = data . room . description ; } } ngOnInit ( ) { this . roomForm = this . fb . group ( { name : [ this . name , Validators . required ] , speakerPassword : [ this . speakerPassword ] , attendeePassword : [ this . attendeePassword ] } ) ; } ngAfterViewInit ( ) { } async create ( ) { const roomName = this . roomForm . get ( 'name' ) . value ; const speakerPassword = this . roomForm . get ( 'speakerPassword' ) . value ; const attendeePassword = this . roomForm . get ( 'attendeePassword' ) . value ; const roomDesc = this . description ; let roomId = '' ; let postUrl = '' ; if ( this . optUpdate ) { roomId = this . dataRoom . id ; postUrl = getHost ( ) + '/room/updateRoom' ; } else { roomId = this . makeRandomString ( 6 ) ; postUrl = getHost ( ) + '/room/createRoom' ; } this . logger . debug ( 'id: %s, name: %s, speakerPassword: %s, attendeePassword: %s, roomDesc: %s' , roomId , roomName , speakerPassword , attendeePassword , roomDesc ) ; this . http . post ( postUrl , { roomId , roomName , speakerPassword , attendeePassword , roomDesc } ) . subscribe ( ( res ) => { this . eventbus . room$ . next ( { type : EventType . room_created } ) ; const action = this . optUpdate ? this . i18n . lang . update : this . i18n . lang . create ; this . snackbar . open ( `${ action } ${ roomName } ${ this . i18n . lang . success }, ID: ${ roomId }` , 'close' , { duration : 5000 } ) ; } , error => { this . snackbar . open ( `${ error . name }: ${ error . message }` , 'close' , { duration : 5000 } ) ; } ) ; } makeRandomString ( length ) { let outString = '' ; const inOptions = '0123456789' ; for ( let i = 0 ; i < length ; i ++ ) { outString += inOptions . charAt ( Math . floor ( Math . random ( ) * inOptions . length ) ) ; } return outString ; } }