Skip to content

Commit

Permalink
Add validator to check unique VM name when create VM in course (#199)
Browse files Browse the repository at this point in the history
* add color to Content Management/Courses Name, Discription, Duration

* refactor function for set value in course in Management/Courses

* add list of vms and scenario in course in Management/Courses

* add edit color of vms in course in Management/Courses

* add edit color of scenario in course in Management/Courses

* fix: when empty vms then not start wizard of scenario in course in Management/Courses

* add validator to check unique VM Name when create in course

---------

Co-authored-by: Maksym Veres <[email protected]>
  • Loading branch information
maxsva and Maksym Veres authored Feb 26, 2024
1 parent c198542 commit 583acfb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/app/course/course-wizard/course-wizard.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,8 @@ <h4>VM Information</h4>
<td class="add-elem">{{ q.key }}</td>
<td class="add-elem">{{ q.value }}</td>
</ng-container>
<!-- view of edit value -->
<ng-container *ngIf="getSelectedCourseVM(i.key, q.key) != q.value && getSelectedCourseVM(i.key, q.key) != 0 ">
<td class=" del-elem after" style="margin-right: 0">
{{ getSelectedCourseVM(i.key, q.key) }}</td>
<!-- view of edit value (delete VM and create VM with the same VM Name) -->
<ng-container *ngIf="isEditedVM(i.key, q.key, q.value)">
<td class=" add-elem"> {{ q.value }}</td>
</ng-container>
</tr>
Expand Down
9 changes: 9 additions & 0 deletions src/app/course/course-wizard/course-wizard.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,13 @@ export class CourseWizardComponent implements OnChanges, OnInit {
isScenarioInList(scenario: Scenario, list?: Scenario[]): boolean {
return list.some(item => item.name === scenario.name);
}

isEditedVM(index: any, vmname: string, templateName: any): boolean {
// This function determines if the currently selected VM is either beeing edited throug the following condition:
const isEdited = this.getSelectedCourseVM(index, vmname) != templateName && this.getSelectedCourseVM(index, vmname) != 0
// ... or if it was deleted and created again with the same vm name through the following condition:
const deletedAndCreatedWithSameName = this.getSelectedCourseVM(index, vmname) === 0 && this.getEditSelectedCourseVM(index, vmname) != templateName
// this.getSelectedCourseVM(index, vmname) != templateName && this.getSelectedCourseVM(index, vmname) != 0
return isEdited || deletedAndCreatedWithSameName
}
}
2 changes: 2 additions & 0 deletions src/app/vmset/new-vm/new-vm.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ <h3 class="modal-title">Create VM</h3>
<clr-control-error *clrIfError="'required'">VM name is required</clr-control-error>
<clr-control-error *clrIfError="'minlength'">VM name must be longer than 4 characters
</clr-control-error>
<clr-control-error *clrIfError="'notUnique'">VM name must be unique
</clr-control-error>
</clr-input-container>
<clr-select-container>
<label>VM Template</label>
Expand Down
21 changes: 18 additions & 3 deletions src/app/vmset/new-vm/new-vm.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, EventEmitter, Output, ViewChild, Input, OnChanges } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { FormGroup, FormControl, Validators, ValidatorFn, AbstractControl } from '@angular/forms';
import { VmtemplateService } from 'src/app/data/vmtemplate.service';
import { VMTemplate } from 'src/app/data/vmtemplate';
import { ClrModal } from '@clr/angular';
Expand All @@ -12,6 +12,9 @@ export class NewVmComponent implements OnChanges {
public modalOpen: boolean = false;
public vmtemplates: VMTemplate[] = [];

@Input()
public vms: {}[] = []; // because JSONifying Maps is hard

@Input()
public listVms: boolean;

Expand Down Expand Up @@ -44,7 +47,8 @@ export class NewVmComponent implements OnChanges {
public vmform: FormGroup = new FormGroup({
'vm_name': new FormControl(null, [
Validators.required,
Validators.minLength(4)
Validators.minLength(4),
this.validateUniqueVmName()
]),
'vm_template': new FormControl(null, [
Validators.required
Expand All @@ -54,10 +58,21 @@ export class NewVmComponent implements OnChanges {
addVM() {
var vm_name = this.vmform.get('vm_name').value;
var vm_template = this.vmform.get('vm_template').value;

this.vm.emit([vm_name, vm_template]);

this.modal.close();
}

private validateUniqueVmName(): ValidatorFn {
return (control: FormControl) => {
const vmName = control.value;
const isNotUnique = this.vms.some((vmSet: {}) => vmSet.hasOwnProperty(vmName))
if (isNotUnique) {
return {
notUnique: true,
};
}
return null;
};
}
}
2 changes: 1 addition & 1 deletion src/app/vmset/vmset.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
</clr-stack-block>
</clr-stack-block>
</clr-stack-view>
<new-vm #newvm (vm)="addVM($event)" [listVms]="allowedAddVm | async"></new-vm>
<new-vm #newvm (vm)="addVM($event)" [listVms]="allowedAddVm | async" [vms]="vms"></new-vm>

0 comments on commit 583acfb

Please sign in to comment.