Methods
|
createSystem
|
createSystem()
|
|
|
|
|
|
ngOnDestroy
|
ngOnDestroy()
|
|
|
|
|
|
Private
Readonly
_formBuilder
|
Type : unknown
|
Default value : inject(FormBuilder)
|
|
|
|
Private
_snackBar
|
Type : unknown
|
Default value : inject(MatSnackBar)
|
|
|
|
Private
cobblerApiService
|
Type : unknown
|
Default value : inject(CobblerApiService)
|
|
|
|
Readonly
dialogRef
|
Type : unknown
|
Default value : inject(MatDialogRef<SystemCreateComponent>)
|
|
|
|
Private
ngUnsubscribe
|
Type : unknown
|
Default value : new Subject<void>()
|
|
|
|
systemCreateFormGroup
|
Type : unknown
|
Default value : this._formBuilder.group({
name: [''],
profile: [''],
})
|
|
|
|
userService
|
Type : unknown
|
Default value : inject(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-system-create',
imports: [
MatButtonModule,
MatDialogModule,
ReactiveFormsModule,
MatInputModule,
],
templateUrl: './system-create.component.html',
styleUrl: './system-create.component.scss',
})
export class SystemCreateComponent implements OnDestroy {
userService = inject(UserService);
private cobblerApiService = inject(CobblerApiService);
private _snackBar = inject(MatSnackBar);
// Dialog
readonly dialogRef = inject(MatDialogRef<SystemCreateComponent>);
// Form
private readonly _formBuilder = inject(FormBuilder);
systemCreateFormGroup = this._formBuilder.group({
name: [''],
profile: [''],
});
// Unsubscribe
private ngUnsubscribe = new Subject<void>();
ngOnDestroy(): void {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
createSystem(): void {
this.cobblerApiService
.new_system(this.userService.token)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe((systemHandle) => {
this.cobblerApiService
.modify_system(
systemHandle,
'name',
this.systemCreateFormGroup.get('name').value,
this.userService.token,
)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(() => {
this.cobblerApiService
.modify_system(
systemHandle,
'profile',
this.systemCreateFormGroup.get('profile').value,
this.userService.token,
)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: () => {
this.cobblerApiService
.save_system(systemHandle, this.userService.token, 'new')
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe({
next: () => {
this._snackBar.dismiss();
this.dialogRef.close(
this.systemCreateFormGroup.get('name').value,
);
},
error: (err) => {
// HTML encode the error message since it originates from XML
this._snackBar.open(
Utils.toHTML(err.message),
$localize`:@@snackbar.action.close:Close`,
);
},
});
},
});
});
});
}
}
<h1 mat-dialog-title i18n="@@system.create.title">Create System</h1>
<div mat-dialog-content>
<form [formGroup]="systemCreateFormGroup">
<mat-form-field>
<mat-label i18n="@@system.create.field.name">Name</mat-label>
<input
matInput
required
cdkFocusInitial
type="text"
formControlName="name"
/>
</mat-form-field>
<mat-form-field>
<mat-label i18n="@@system.create.field.profile">Profile</mat-label>
<input matInput required type="text" formControlName="profile" />
</mat-form-field>
</form>
<div mat-dialog-actions>
<button
mat-button
data-testid="item-create-submit"
(click)="createSystem()"
i18n="@@system.create.save-button"
>
Save System
</button>
<button mat-button [mat-dialog-close]="false" i18n="@@button.cancel">
Cancel
</button>
</div>
</div>
:host {
width: 100%;
}
:host ::ng-deep mat-form-field {
display: flex;
flex-direction: column;
align-items: flex-start;
}
Legend
Html element with directive