File

feature-libs/organization/administration/components/shared/form/form.service.ts

Index

Properties
Methods
Accessors

Methods

Protected Abstract build
build(item?: T)

Builds the form structure.

Parameters :
Name Type Optional
item T Yes
Returns : void
getForm
getForm(item?: T)
Parameters :
Name Type Optional
item T Yes
Returns : FormGroup
Protected patchData
patchData(item?: T)
Parameters :
Name Type Optional
item T Yes
Returns : void
Private toggleFreeze
toggleFreeze(item?: T)
Parameters :
Name Type Optional
item T Yes
Returns : void

Properties

Protected form
Type : FormGroup

Accessors

defaultValue
getdefaultValue()

returns the default form value.

Returns : T
import { Injectable } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { BaseItem } from '../organization.model';

@Injectable()
export abstract class FormService<T> {
  protected form: FormGroup;

  /**
   * Builds the form structure.
   */
  protected abstract build(item?: T): void;

  getForm(item?: T): FormGroup {
    if (this.form && !!item) {
      this.patchData(item);
      return this.form;
    }

    if (!this.form) {
      this.build(item);
    }

    // while we should be able to reset with initial value, this doesn't always work
    // hence, we're patching afterwards.
    this.form.reset();

    this.form.enable();
    this.patchData(item);
    return this.form;
  }

  protected patchData(item?: T): void {
    this.toggleFreeze(item);
    this.form.patchValue({ ...this.defaultValue, ...item });
  }

  private toggleFreeze(item?: T): void {
    if (this.form.enabled && (item as BaseItem)?.active === false) {
      this.form.disable();
    }
    if (this.form.disabled && (item as BaseItem)?.active === true) {
      this.form.enable();
    }
  }
  /**
   * returns the default form value.
   */
  protected get defaultValue(): T {
    return {} as T;
  }
}

result-matching ""

    No results matching ""