File

src/app/tasks/task-definition-create/create-dialog/create-dialog.component.ts

Description

Component to display dialog to allow user to name and deploy a task.

Implements

OnInit OnDestroy

Example

Metadata

selector app-task-create-dialog
styleUrls styles.scss
templateUrl ./create-dialog.component.html

Index

Properties
Methods

Constructor

constructor(tasksService: TasksService, fb: FormBuilder, notificationService: NotificationService, loggerService: LoggerService, blockerService: BlockerService, bsModalRef: BsModalRef)
Parameters :
Name Type Optional Description
tasksService TasksService
fb FormBuilder
notificationService NotificationService
loggerService LoggerService
blockerService BlockerService
bsModalRef BsModalRef

Methods

canSubmit
canSubmit()

Used in a dialog to disable submit button.

Returns : boolean

returns true if ready to submit

handleCancel
handleCancel()

Handles cancel operation of this dialog.

Returns : void
handleCreate
handleCreate()

Handles creation of a task with TasksService. Hides dialog and calls success callback which gives parent a change to do its actions, i.e. clearing a flo graph.

Returns : void
ngOnDestroy
ngOnDestroy()

Will cleanup any {@link Subscription}s to prevent memory leaks.

Returns : void
ngOnInit
ngOnInit()
Returns : void
setDsl
setDsl(dsl: string)

Sets a dsl used to create a task.

Parameters :
Name Type Optional Description
dsl string
Returns : void

Properties

dsl
dsl: string
Type : string

Shown and used dsl for task.

errors
errors: Array<string>
Type : Array<string>

Shown generic errors in a dialog.

form
form: FormGroup
Type : FormGroup

The FormGroup having dialog controls.

Private ngUnsubscribe$
ngUnsubscribe$: Subject<any>
Type : Subject<any>
successCallback
successCallback: function
Type : function

Callback to parent which is called when task is created succesfully.

taskName
taskName:

The FormControl used to capture the task name.

warnings
warnings: Array<string>
Type : Array<string>

Shown generic warnings in a dialog.

import { Component, OnInit, OnDestroy } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { TasksService } from '../../tasks.service';
import { finalize, takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { NotificationService } from '../../../shared/services/notification.service';
import { LoggerService } from '../../../shared/services/logger.service';
import { AppError } from '../../../shared/model/error.model';
import { BlockerService } from '../../../shared/components/blocker/blocker.service';

/**
 * Component to display dialog to allow user to name and deploy a task.
 *
 * @author Janne Valkealahti
 */
@Component({
  selector: 'app-task-create-dialog',
  templateUrl: './create-dialog.component.html',
  styleUrls: ['./styles.scss']
})
export class TaskDefinitionCreateDialogComponent implements OnInit, OnDestroy {

  private ngUnsubscribe$: Subject<any> = new Subject();

  /**
   * Shown and used dsl for task.
   */
  dsl: string;

  /**
   * Shown generic errors in a dialog.
   */
  errors: Array<string>;

  /**
   * Shown generic warnings in a dialog.
   */
  warnings: Array<string>;

  /**
   * The FormGroup having dialog controls.
   * @type {FormGroup}
   */
  form: FormGroup;

  /**
   * The FormControl used to capture the task name.
   * @type {FormControl}
   */
  taskName = new FormControl('', validateTaskName);

  /**
   * Callback to parent which is called when task is created succesfully.
   */
  successCallback: () => void;

  constructor(private tasksService: TasksService,
              private fb: FormBuilder,
              private notificationService: NotificationService,
              private loggerService: LoggerService,
              private blockerService: BlockerService,
              private bsModalRef: BsModalRef) {
    this.form = fb.group({
      'taskName': this.taskName
    });
  }

  ngOnInit() {
    this.errors = new Array<string>();
    this.warnings = new Array<string>();
  }

  /**
   * Will cleanup any {@link Subscription}s to prevent
   * memory leaks.
   */
  ngOnDestroy() {
    this.ngUnsubscribe$.next();
    this.ngUnsubscribe$.complete();
  }

  /**
   * Sets a dsl used to create a task.
   * @param {string} the task dsl
   */
  setDsl(dsl: string) {
    this.loggerService.log('dsl', dsl);
    this.dsl = dsl;
  }

  /**
   * Handles cancel operation of this dialog.
   */
  handleCancel() {
    this.bsModalRef.hide();
  }

  /**
   * Handles creation of a task with TasksService. Hides dialog and
   * calls success callback which gives parent a change to do its
   * actions, i.e. clearing a flo graph.
   */
  handleCreate() {
    if (!this.form.valid) {
      this.notificationService.error('Some field(s) are missing or invalid.');
    } else {
      this.blockerService.lock();
      this.tasksService.createDefinition({ name: this.taskName.value, definition: this.dsl })
        .pipe(takeUntil(this.ngUnsubscribe$), finalize(() => this.blockerService.unlock()))
        .subscribe(
          () => {
            this.loggerService.log('Succesfully created task', this.taskName.value, this.dsl);
            if (this.successCallback) {
              this.successCallback();
            }
            this.bsModalRef.hide();
            this.notificationService.success('Composed task created for ' + this.taskName.value);
          },
          (error) => {
            this.notificationService.error(AppError.is(error) ? error.getMessage() : error);
            this.bsModalRef.hide();
          }
        );
    }
  }

  /**
   * Used in a dialog to disable submit button.
   * @returns {boolean} returns true if ready to submit
   */
  canSubmit(): boolean {
    this.loggerService.log('canSubmit');
    return this.form.valid;
  }

}

/**
 * Validates the task name.
 *
 * @param formControl the form that contains the task name input.
 * @returns {any} null if successful or the validateProperties message if a failure.
 */
function validateTaskName(formControl: FormControl) {
  if (formControl.value.length > 0) {
    return null;
  } else {
    return { validateTaskName: { reason: 'Cannot be empty' } };
  }
}
<div class="modal-header">
  <h4 class="modal-title pull-left">Confirm Task Creation</h4>
  <button type="button" class="close pull-right" aria-label="Close" (click)="handleCancel()">
    <span aria-hidden="true">&times;</span>
  </button>
</div>


<form (submit)="handleCreate()" class="form-horizontal" [formGroup]="form">

  <div class="modal-body">

    <div *ngIf="errors && errors.length > 0" class="alert alert-error">
      <div *ngFor="let error of errors">• {{ error }}</div>
    </div>

    <div *ngIf="!(errors && errors.length) && warnings && warnings.length > 0" class="alert alert-warning">
      <div *ngFor="let warning of warnings">{{ warning }}</div>
    </div>

    <p>This action will create a task:</p>

    <div class="row-task">
      <div class="form-group">
        <label class="control-label col-sm-4">Definition</label>
        <div class="col-sm-18">
          <div class="control-empty">
            <app-stream-dsl>{{ dsl }}</app-stream-dsl>
          </div>
        </div>
      </div>

      <div class="form-group" [class.has-error]="form.controls.taskName.errors">
        <label for="name" class="control-label col-sm-4 control-label-sm">
          Name <em class="required">*</em>
        </label>
        <div class="col-sm-16">
          <input class="form-control input-sm" id="name"
                 name="name" formControlName="taskName"
                 type="text" placeholder="<Task Name>"
                 [dataflowFocus]="true"/>
          <span *ngIf="form.controls.taskName.errors"
                class="help-block validation-block">
            The format of your task name is invalid. {{ form.controls.taskName.errors.validateTaskName.reason }}
          </span>
        </div>
      </div>

    </div>

  </div>

  <div class="modal-footer">
    <button type="button" class="btn btn-default" (click)="handleCancel()">Cancel</button>
    <button type="submit" class="btn btn-primary">Create the task</button>
  </div>

</form>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""