Skip to content
Snippets Groups Projects
useradmin.component.ts 4.12 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Component, OnInit, EventEmitter, Output } from '@angular/core';
    
    import { FormGroup, FormBuilder, Validators, ValidatorFn } from '@angular/forms';
    
    import { NotificationService } from '../../../service/notification/notification.service';
    
    import { LANG } from '../../translate.component';
    import { tap } from 'rxjs/internal/operators/tap';
    
    import { InstallerService } from '../installer.service';
    
    import { StepAction } from '../types';
    
        selector: 'app-useradmin',
        templateUrl: './useradmin.component.html',
        styleUrls: ['./useradmin.component.scss']
    
    })
    export class UseradminComponent implements OnInit {
    
        lang: any = LANG;
        stepFormGroup: FormGroup;
    
        hide: boolean = true;
    
        @Output() tiggerInstall = new EventEmitter<string>();
    
    
        constructor(
            private _formBuilder: FormBuilder,
            private notify: NotificationService,
    
            private installerService: InstallerService
    
    
            const valLogin: ValidatorFn[] = [Validators.pattern(/^[\w.@-]*$/), Validators.required];
    
            const valEmail: ValidatorFn[] = [Validators.pattern(/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/), Validators.required];
    
            this.stepFormGroup = this._formBuilder.group({
    
                login: ['superadmin', valLogin],
    
                firstname: ['Admin', Validators.required],
                lastname: ['SUPER', Validators.required],
    
                password: ['', Validators.required],
                passwordConfirm: ['', Validators.required],
    
                email: ['yourEmail@domain.com', valEmail],
    
        ngOnInit(): void {
            this.stepFormGroup.controls['password'].valueChanges.pipe(
                tap((data) => {
                    if (data !== this.stepFormGroup.controls['passwordConfirm'].value) {
                        this.stepFormGroup.controls['password'].setErrors({ 'incorrect': true });
                        this.stepFormGroup.controls['passwordConfirm'].setErrors({ 'incorrect': true });
                        this.stepFormGroup.controls['passwordConfirm'].markAsTouched();
                    } else {
                        this.stepFormGroup.controls['password'].setErrors(null);
                        this.stepFormGroup.controls['passwordConfirm'].setErrors(null);
                    }
                })
            ).subscribe();
            this.stepFormGroup.controls['passwordConfirm'].valueChanges.pipe(
                tap((data) => {
                    if (data !== this.stepFormGroup.controls['password'].value) {
                        this.stepFormGroup.controls['password'].setErrors({ 'incorrect': true });
                        this.stepFormGroup.controls['password'].markAsTouched();
                        this.stepFormGroup.controls['passwordConfirm'].setErrors({ 'incorrect': true });
                    } else {
                        this.stepFormGroup.controls['password'].setErrors(null);
                        this.stepFormGroup.controls['passwordConfirm'].setErrors(null);
                    }
                })
            ).subscribe();
        }
    
        initStep() {
    
            if (this.installerService.isStepAlreadyLaunched('userAdmin')) {
                this.stepFormGroup.disable();
            }
    
            return this.stepFormGroup === undefined ? false : this.stepFormGroup.valid || this.installerService.isStepAlreadyLaunched('userAdmin');
    
            return this.installerService.isStepAlreadyLaunched('userAdmin') ? true : this.stepFormGroup;
    
        getInfoToInstall(): StepAction[] {
            return [{
    
                body : {
                    login: this.stepFormGroup.controls['login'].value,
    
                    firstname: this.stepFormGroup.controls['firstname'].value,
                    lastname: this.stepFormGroup.controls['lastname'].value,
    
                    password: this.stepFormGroup.controls['password'].value,
    
                    email: this.stepFormGroup.controls['email'].value,
    
                route : {
                    method : 'PUT',
                    url : '../rest/installer/administrator'
                },
    
                description: this.lang.stepUserAdminActionDesc,
                installPriority: 3
    
        launchInstall() {
            this.tiggerInstall.emit();