import { Component , Input , OnInit } from '@angular/core' ; import { FileMetadata } from '../../model/metadata' ; import { checkFolderExistsValidator , FileService } from '../../service/file.service' ; import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { BehaviorSubject } from 'rxjs' ; import { ToastService } from '../../../../toast/toast.service' ; @ Component ( { selector : 'cmdb-new-folder-dialog' , templateUrl : './new-folder-dialog.component.html' , styleUrls : [ './new-folder-dialog.component.scss' ] } ) export class NewFolderDialogComponent implements OnInit { private selectedFileElement : BehaviorSubject < any > ; @ Input ( ) set selectedFileFolder ( value ) { this . selectedFileElement = value ; } get selectedFileFolder ( ) : BehaviorSubject < any > { return this . selectedFileElement ; } public basicForm : FormGroup ; static generateMetaData ( parent ) { return new FileMetadata ( { folder : true , ... { parent : parent . getValue ( ) == null ? null : parent . getValue ( ) . public_id } } ) ; } constructor ( private fileService , public activeModal , private toast ) { } public ngOnInit ( ) { this . basicForm = new FormGroup ( { name : new FormControl ( '' , Validators . required ) } ) ; this . basicForm . get ( 'name' ) . setAsyncValidators ( checkFolderExistsValidator ( this . fileService , NewFolderDialogComponent . generateMetaData ( this . selectedFileFolder ) ) ) ; } public get name ( ) { return this . basicForm . get ( 'name' ) ; } public createFolder ( ) { const folder = new File ( [ 'folder' ] , this . name . value , { type : 'application/json' , } ) ; this . fileService . postFile ( folder , NewFolderDialogComponent . generateMetaData ( this . selectedFileElement ) ) . subscribe ( ( resp ) => { this . toast . info ( `Folder was successfully created: ${ this . name . value }` ) ; this . activeModal . close ( { data : resp } ) ; } ) ; } }