File

projects/cobbler-frontend/src/app/items/template/create/template-create.component.ts

Implements

OnDestroy

Metadata

Index

Properties
Methods

Constructor

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

Methods

createTemplate
createTemplate()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void

Properties

Private Readonly _formBuilder
Default value : inject(FormBuilder)
Readonly dialogRef
Default value : inject(MatDialogRef<TemplateCreateComponent>)
Private ngUnsubscribe
Default value : new Subject<void>()
templateCreateFormGroup
Default value : this._formBuilder.group({ name: [''], content: [''], })
Public userService
Type : UserService
import { Component, inject, OnDestroy } from '@angular/core';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { MatInputModule } from '@angular/material/input';
import { MatSnackBar } from '@angular/material/snack-bar';
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-template-create',
  standalone: true,
  imports: [
    MatButtonModule,
    MatDialogModule,
    ReactiveFormsModule,
    MatInputModule,
  ],
  templateUrl: './template-create.component.html',
  styleUrl: './template-create.component.scss',
})
export class TemplateCreateComponent implements OnDestroy {
  // Dialog
  readonly dialogRef = inject(MatDialogRef<TemplateCreateComponent>);

  // Form
  private readonly _formBuilder = inject(FormBuilder);
  templateCreateFormGroup = this._formBuilder.group({
    name: [''],
    content: [''],
  });

  // Unsubscribe
  private ngUnsubscribe = new Subject<void>();

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

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

  createTemplate(): void {
    this.cobblerApiService
      .write_autoinstall_template(
        this.templateCreateFormGroup.get('name').value,
        this.templateCreateFormGroup.get('content').value,
        this.userService.token,
      )
      .pipe(takeUntil(this.ngUnsubscribe))
      .subscribe({
        next: () => {
          this.dialogRef.close(this.templateCreateFormGroup.get('name').value);
        },
        error: (err) => {
          // HTML encode the error message since it originates from XML
          this._snackBar.open(Utils.toHTML(err.message), 'Close');
        },
      });
  }
}
<h1 mat-dialog-title>Create Template</h1>
<div mat-dialog-content>
  <form [formGroup]="templateCreateFormGroup">
    <mat-form-field>
      <mat-label>Name</mat-label>
      <input
        matInput
        required
        cdkFocusInitial
        type="text"
        formControlName="name"
      />
    </mat-form-field>
    <mat-form-field class="form-field-full-width">
      <mat-label>Template Content</mat-label>
      <textarea matInput formControlName="content"></textarea>
    </mat-form-field>
  </form>
  <div mat-dialog-actions>
    <button mat-button (click)="createTemplate()">Save Template</button>
    <button mat-button [mat-dialog-close]="false">Cancel</button>
  </div>
</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""