Private/Angular/Component/Crud/New-NgCrudComponentTsToString.ps1

Function New-NgCrudComponentTsToString([TableInfo]$tableInfo, [string]$returnToUrl) {


<##########################################################################################>
return @"

import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, Params } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

import { StringService } from '../service/string.service';
import { $($tableInfo.tableCapitalCamel) } from '../model/$($tableInfo.tableLowerKebab)';
import { $($tableInfo.tableCapitalCamel)Service } from '../service/$($tableInfo.tableLowerKebab).service';
import { ValidationHelperService } from 'box-turtle-ng';
import { ValidationMetaDataClass, ValidationMetaDataProperty } from 'box-turtle-ng';

@Component({
    selector: '$($tableInfo.tableLowerKebab)',
    templateUrl: './$($tableInfo.tableLowerKebab).component.html',
    styleUrls: ['./$($tableInfo.tableLowerKebab).component.css']
})
export class $($tableInfo.tableCapitalCamel)Component implements OnInit {
    private $($tableInfo.tableLowerCamel): $($tableInfo.tableCapitalCamel);
    private $($tableInfo.tableLowerCamel)Form: FormGroup;
    private validationMessages = {};
    private formErrors = {};
    private formMetaData: any = {};
    private title: string = "";
    private roles: any = {};

    constructor(
        private $($tableInfo.tableLowerCamel)Service: $($tableInfo.tableCapitalCamel)Service,
        private route: ActivatedRoute,
        private router: Router,
        private fb: FormBuilder,
        private validationHelperService: ValidationHelperService,
        private stringService: StringService
    ) {
    }

    ngOnInit(): void {
        this.route.params
            .switchMap(
            (params: Params) => {
                let $($tableInfo.tableLowerCamel)Promise: Promise<$($tableInfo.tableCapitalCamel)>;

                let id: number = params['id'] as number;
                if (id && (id > 0)) {
                    $($tableInfo.tableLowerCamel)Promise = this.$($tableInfo.tableLowerCamel)Service.get(id);
                } else {
                    $($tableInfo.tableLowerCamel)Promise = new Promise((resolve, reject) => { resolve(new $($tableInfo.tableCapitalCamel)()) });
                }
                return $($tableInfo.tableLowerCamel)Promise;
            }
            )
            .subscribe($($tableInfo.tableLowerCamel) => {
                this.$($tableInfo.tableLowerCamel) = $($tableInfo.tableLowerCamel);
                this.title = ($($tableInfo.tableLowerCamel).$($tableInfo.tableLowerCamel)Id ? "Edit" : "New") + " $($tableInfo.tablePretty)";
                this.buildForm();
            }
            );
    }

    buildForm(): void {
        // Use the current object and it's property decorators to generate all the angular validation metadata
        let formBuilderGroupData = {};
        this.validationHelperService.inferValidationMetadataFromPropertyDecorators($($tableInfo.tableCapitalCamel).name, this.$($tableInfo.tableLowerCamel),
            this.formErrors, this.validationMessages, formBuilderGroupData, this.formMetaData);
        this.$($tableInfo.tableLowerCamel)Form = this.fb.group(formBuilderGroupData);

        this.$($tableInfo.tableLowerCamel)Form.valueChanges
            .subscribe(data => this.onValueChanged(data));

        this.onValueChanged(); // (re)set validation message now
    }

    onValueChanged(data?: any): void {
        this.validationHelperService.copyValidationErrorsToLocalAssocArray(this.$($tableInfo.tableLowerCamel)Form, this.formErrors, this.validationMessages);
    }

    save(): void {
        this.$($tableInfo.tableLowerCamel) = this.$($tableInfo.tableLowerCamel)Form.value;
        if (this.$($tableInfo.tableLowerCamel).$($tableInfo.tableLowerCamel)Id > 0) {
            this.$($tableInfo.tableLowerCamel)Service.update(this.$($tableInfo.tableLowerCamel))
                .then(() => { this.router.navigate(['$($returnToUrl)']); });
        } else {
            this.$($tableInfo.tableLowerCamel)Service.create(this.$($tableInfo.tableLowerCamel))
                .then(() => { this.router.navigate(['$($returnToUrl)']); });
        }
    }

    delete(): void {
        this.$($tableInfo.tableLowerCamel)Service.delete(this.$($tableInfo.tableLowerCamel).$($tableInfo.tableLowerCamel)Id)
            .then(() => this.router.navigate(['$($returnToUrl)']));
    }

    goBack(): void {
        this.router.navigate(['$($returnToUrl)']);
    }
}

"@

<##########################################################################################>
}