File

projects/cobbler-frontend/src/app/actions/sync/sync.component.ts

Implements

OnDestroy

Metadata

Index

Properties
Methods
Accessors

Constructor

constructor(cobblerApiService: CobblerApiService, userService: UserService, _snackBar: MatSnackBar)
Parameters :
Name Type Optional
cobblerApiService CobblerApiService No
userService UserService No
_snackBar MatSnackBar No

Methods

addNewKeyValueFG
addNewKeyValueFG()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
removeNewKeyValueFG
removeNewKeyValueFG(index: number)
Parameters :
Name Type Optional
index number No
Returns : void
syncFullSubmit
syncFullSubmit()
Returns : void
syncSystemsSubmit
syncSystemsSubmit()
Returns : void

Properties

Private Readonly _formBuilder
Default value : inject(FormBuilder)
Readonly fullSync
Default value : this._formBuilder.group({ fullSyncDhcp: false, fullSyncDns: false, fullSyncVerbose: false, })
keyValueFA
Default value : new FormArray([])
Private ngUnsubscribe
Default value : new Subject<void>()
systemsSync
Default value : this._formBuilder.group({ keyValue: this.keyValueFA, systemsSyncVerbose: false, })

Accessors

newKeyValueFG
getnewKeyValueFG()
keyValueArrayFGControls
getkeyValueArrayFGControls()
import { CommonModule } from '@angular/common';
import {
  ChangeDetectionStrategy,
  Component,
  inject,
  OnDestroy,
} from '@angular/core';
import {
  FormArray,
  FormBuilder,
  FormControl,
  FormGroup,
  ReactiveFormsModule,
  Validators,
} from '@angular/forms';
import { MatButton, MatIconButton } from '@angular/material/button';
import { MatCheckbox } from '@angular/material/checkbox';
import { MatDialogClose } from '@angular/material/dialog';
import { MatIcon } from '@angular/material/icon';
import {
  MatFormField,
  MatInput,
  MatPrefix,
  MatSuffix,
} from '@angular/material/input';
import { MatSnackBar } from '@angular/material/snack-bar';
import { RouterOutlet } from '@angular/router';
import { CobblerApiService } from 'cobbler-api';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { UserService } from '../../services/user.service';
import Utils from '../../utils';

@Component({
  selector: 'cobbler-sync',
  templateUrl: './sync.component.html',
  styleUrls: ['./sync.component.css'],
  standalone: true,
  imports: [
    RouterOutlet,
    MatButton,
    MatDialogClose,
    ReactiveFormsModule,
    MatCheckbox,
    CommonModule,
    MatInput,
    MatIconButton,
    MatIcon,
    MatFormField,
    MatPrefix,
    MatSuffix,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SyncComponent implements OnDestroy {
  // Unsubscribe
  private ngUnsubscribe = new Subject<void>();

  // Form
  private readonly _formBuilder = inject(FormBuilder);
  readonly fullSync = this._formBuilder.group({
    fullSyncDhcp: false,
    fullSyncDns: false,
    fullSyncVerbose: false,
  });

  keyValueFA = new FormArray([]);

  systemsSync = this._formBuilder.group({
    keyValue: this.keyValueFA,
    systemsSyncVerbose: false,
  });

  constructor(
    private cobblerApiService: CobblerApiService,
    private userService: UserService,
    private _snackBar: MatSnackBar,
  ) {}

  ngOnDestroy(): void {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

  get newKeyValueFG(): FormGroup {
    return new FormGroup({
      systemName: new FormControl(null, [Validators.required]),
    });
  }

  get keyValueArrayFGControls(): FormGroup[] {
    return this.keyValueFA.controls as FormGroup[];
  }

  addNewKeyValueFG(): void {
    this.keyValueFA.push(this.newKeyValueFG);
  }

  removeNewKeyValueFG(index: number): void {
    this.keyValueFA.removeAt(index);
  }

  syncFullSubmit(): void {
    const syncOptions = {
      dhcp: this.fullSync.controls.fullSyncDhcp.value,
      dns: this.fullSync.controls.fullSyncDns.value,
      verbose: this.fullSync.controls.fullSyncVerbose.value,
    };
    this.fullSync.controls.fullSyncDhcp.reset(false);
    this.fullSync.controls.fullSyncDns.reset(false);
    this.fullSync.controls.fullSyncVerbose.reset(false);
    this.cobblerApiService
      .background_sync(syncOptions, this.userService.token)
      .pipe(takeUntil(this.ngUnsubscribe))
      .subscribe(
        (value) => {
          console.log(value);
        },
        (error) => {
          // HTML encode the error message since it originates from XML
          this._snackBar.open(Utils.toHTML(error.message), 'Close');
        },
      );
  }

  syncSystemsSubmit(): void {
    if (this.systemsSync.invalid) {
      for (let control of this.systemsSync.controls.keyValue.controls) {
        control.markAsTouched();
      }
      this._snackBar.open('Please give all inputs a system name!', 'Close', {
        duration: 2000,
      });
      return;
    }
    let systemNames: Array<string> = [];
    for (let control of this.systemsSync.controls.keyValue.controls) {
      if (control instanceof FormGroup) {
        systemNames.push(control.value.systemName);
      }
    }
    const syncOptions = {
      systems: systemNames,
      verbose: this.systemsSync.controls.systemsSyncVerbose.value,
    };
    this.systemsSync.controls.systemsSyncVerbose.reset(false);
    this.systemsSync.controls.keyValue.reset([]);

    this.cobblerApiService
      .background_syncsystems(syncOptions, this.userService.token)
      .pipe(takeUntil(this.ngUnsubscribe))
      .subscribe(
        (value) => {
          console.log(value);
        },
        (error) => {
          // HTML encode the error message since it originates from XML
          this._snackBar.open(Utils.toHTML(error.message), 'Close');
        },
      );
  }
}
<div class="right-column" id="dataScreen">
  <router-outlet></router-outlet>
  <div class="Sync-div">
    <h1 class="title">SYNC</h1>
    <h2>Full Sync</h2>
    <section class="sync-section" [formGroup]="fullSync">
      <p>
        <mat-checkbox formControlName="fullSyncDhcp">DHCP</mat-checkbox>
        <mat-checkbox formControlName="fullSyncDns">DNS</mat-checkbox>
        <mat-checkbox formControlName="fullSyncVerbose">Verbose</mat-checkbox>
      </p>
      <button mat-button (click)="syncFullSubmit()">Sync</button>
    </section>
    <h2>Systems Sync</h2>
    <form class="systems-sync-section" [formGroup]="systemsSync">
      <button mat-button (click)="addNewKeyValueFG()">
        @if (keyValueArrayFGControls.length === 0) {
          Sync specific Systems
        }
        @if (keyValueArrayFGControls.length > 0) {
          Add system
        }
      </button>
      @for (fg of keyValueArrayFGControls; track fg; let i = $index) {
        <div formArrayName="keyValue">
          <div [formGroup]="fg">
            <mat-form-field>
              <span matTextPrefix>{{ i + 1 }}.&nbsp;</span>
              <input
                matInput
                required
                type="text"
                formControlName="systemName"
                placeholder="System Name"
              />
              <button
                mat-icon-button
                matSuffix
                (click)="removeNewKeyValueFG(i)"
              >
                <mat-icon>remove</mat-icon>
              </button>
              @if (
                systemsSync.controls.keyValue.controls[i].hasError("required")
              ) {
                <mat-error>System name is <strong>required</strong></mat-error>
              }
            </mat-form-field>
          </div>
        </div>
      } @empty {
        <p>Syncing All Systems</p>
      }
      <mat-checkbox formControlName="systemsSyncVerbose">Verbose</mat-checkbox>
    </form>
    <button mat-button (click)="syncSystemsSubmit()">Sync</button>
  </div>
</div>

./sync.component.css

Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""