import { OnInit , AfterContentChecked , Injector , Directive } from '@angular/core' ; import { FormBuilder , FormGroup , Validators } from "@angular/forms" ; import { ActivatedRoute , Router } from "@angular/router" ; import { BaseResourceModel } from "../../models/base-resource.model" ; import { BaseResourceService } from "../../services/base-resource.service" ; import { switchMap } from "rxjs/operators" ; import toastr from "toastr" ; @ Directive ( ) export abstract class BaseResourceFormComponent < T extends BaseResourceModel > implements OnInit , AfterContentChecked { currentAction : string ; resourceForm : FormGroup ; pageTitle : string ; serverErrorMessages : string [ ] = null ; submittingForm : boolean = false ; protected route : ActivatedRoute ; protected router : Router ; protected formBuilder : FormBuilder ; constructor ( protected injector , public resource , protected resourceService , protected jsonDataToResourceFn ) { this . route = this . injector . get ( ActivatedRoute ) ; this . router = this . injector . get ( Router ) ; this . formBuilder = this . injector . get ( FormBuilder ) ; } ngOnInit ( ) { this . setCurrentAction ( ) ; this . buildResourceForm ( ) ; this . loadResource ( ) ; } ngAfterContentChecked ( ) { this . setPageTitle ( ) ; } submitForm ( ) { this . submittingForm = true ; if ( this . currentAction == "new" ) this . createResource ( ) ; else this . updateResource ( ) ; } protected setCurrentAction ( ) { if ( this . route . snapshot . url [ 0 ] . path == "new" ) this . currentAction = "new" ; else this . currentAction = "edit" ; } protected loadResource ( ) { if ( this . currentAction == "edit" ) { this . route . paramMap . pipe ( switchMap ( params => this . resourceService . getById ( + params . get ( "id" ) ) ) ) . subscribe ( ( resource ) => { this . resource = resource ; this . resourceForm . patchValue ( resource ) ; } , ( error ) => alert ( 'Ocorreu um erro no servidor, tente mais tarde.' ) ) ; } } protected setPageTitle ( ) { if ( this . currentAction == 'new' ) this . pageTitle = this . creationPageTitle ( ) ; else { this . pageTitle = this . editionPageTitle ( ) ; } } protected creationPageTitle ( ) { return "Novo" ; } protected editionPageTitle ( ) { return "Edição" ; } protected createResource ( ) { const resource = this . jsonDataToResourceFn ( this . resourceForm . value ) ; this . resourceService . create ( resource ) . subscribe ( resource => this . actionsForSuccess ( resource ) , error => this . actionsForError ( error ) ) ; } protected updateResource ( ) { const resource = this . jsonDataToResourceFn ( this . resourceForm . value ) ; this . resourceService . update ( resource ) . subscribe ( resource => this . actionsForSuccess ( resource ) , error => this . actionsForError ( error ) ) ; } protected actionsForSuccess ( resource ) { toastr . success ( "Solicitação processada com sucesso!" ) ; const baseComponentPath = this . route . snapshot . parent . url [ 0 ] . path ; this . router . navigateByUrl ( baseComponentPath , { skipLocationChange : true } ) . then ( ( ) => this . router . navigate ( [ baseComponentPath , resource . id , "edit" ] ) ) ; } protected actionsForError ( error ) { toastr . error ( "Ocorreu um erro ao processar a sua solicitação!" ) ; this . submittingForm = false ; if ( error . status === 422 ) this . serverErrorMessages = JSON . parse ( error . _body ) . errors ; else this . serverErrorMessages = [ "Falha na comunicação com o servidor. Por favor, tente mais tarde." ] ; } protected abstract buildResourceForm ( ) ; }