File

feature-libs/organization/administration/components/unit/links/users/roles/unit-user-roles-form.service.ts

Extends

FormService

Index

Properties
Methods

Constructor

constructor(userService: B2BUserService)
Parameters :
Name Type Optional
userService B2BUserService No

Methods

Protected build
build()
Inherited from FormService
Defined in FormService:24
Returns : void
getForm
getForm(item?: B2BUser)
Inherited from FormService
Defined in FormService:17
Parameters :
Name Type Optional
item B2BUser Yes
Returns : FormGroup
Protected patchData
patchData(item: B2BUser)
Inherited from FormService
Defined in FormService:32
Parameters :
Name Type Optional
item B2BUser No
Returns : void
Private toggleFreeze
toggleFreeze(item?: T)
Inherited from FormService
Defined in FormService:38
Parameters :
Name Type Optional
item T Yes
Returns : void

Properties

availableRoles
Type : B2BUserRole[]
Default value : this.userService.getAllRoles()
Protected form
Type : FormGroup
Inherited from FormService
Defined in FormService:7
import { Injectable } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { B2BUser, B2BUserRole } from '@spartacus/core';
import { B2BUserService } from '@spartacus/organization/administration/core';
import { FormService } from '../../../../shared/form/form.service';

@Injectable({
  providedIn: 'root',
})
export class UnitUserRolesFormService extends FormService<B2BUser> {
  availableRoles: B2BUserRole[] = this.userService.getAllRoles();

  constructor(protected userService: B2BUserService) {
    super();
  }

  getForm(item?: B2BUser): FormGroup {
    // if form already exist, while switching between users
    // it didn't patchData again, so used force rebuild
    this.form = null;
    return super.getForm(item);
  }

  protected build() {
    const form = new FormGroup({});
    this.availableRoles.forEach((role) =>
      form.addControl(role, new FormControl())
    );
    this.form = form;
  }

  protected patchData(item: B2BUser) {
    super.patchData(item);
    if (item) {
      item.roles?.forEach((role) => {
        this.form.get(role).setValue(true);
      });
    }
  }
}

result-matching ""

    No results matching ""