-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #2037 added edit form component * #2037 added dynamic form controls * #2037 added editing * #2037 added page reload and last input focus * #2041 added tab storing in state * #2037 added directions input * #2037 added close button, fixed editing when not all fields are present * #2037 fixed formatting, added autofocus, cleaned code * #2037 reverted some changes * #2037 resolved comments * fixed test, temporarily deleted test file for edit form * #2037 resolve comments 2 * fixed name in template * #2037 added access modifiers to variables * #2037 added translation --------- Co-authored-by: Nazarii Ivasyshyn <nivasy@softserveinc.com>
- Loading branch information
Showing
17 changed files
with
316 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...itution-hierarchies-edit-form/directions-institution-hierarchies-edit-form.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<div class="create-form" fxLayout="column" fxLayoutAlign="center center"> | ||
<div class="wrapper"> | ||
<div class="create-form-header" fxLayout="column" fxLayoutAlign="center center"> | ||
<h3 class="wrapper-title">{{ 'TITLES.EDIT_INSTITUTION_HIERARCHY_FORM_TITLE' | translate }}</h3> | ||
<p class="wrapper-text"> | ||
{{ 'TITLES.EDIT_INSTITUTION_HIERARCHY_FORM_SUBTITLE' | translate }} | ||
</p> | ||
</div> | ||
<div fxLayout="row" fxLayoutAlign="center start" class="warning-box"> | ||
<span> | ||
<i class="material-icons status-info-icon inactiveInfoBtn">info_outline</i> | ||
</span> | ||
<div> | ||
<span>{{ 'FORMS.EDIT_INSTITUTION_HIERARCHY_WARNING_MSG' | translate }} | ||
</span> | ||
</div> | ||
</div> | ||
|
||
<form #editForm [formGroup]="editDirectionFormGroup" fxLayout="column" fxLayoutAlign="center space-between" class="step form"> | ||
<label class="step-label">{{ 'FORMS.LABELS.MINISTRY_SUBMISSION' | translate }}</label> | ||
<mat-form-field> | ||
<input matInput class="step-input" [formControlName]="ministryControl" type="text" autocomplete="none" appTrimValue /> | ||
</mat-form-field> | ||
<ng-container *ngFor="let field of this.fields.slice(1); let i = index;"> | ||
<label class="step-label">{{ field }}</label> | ||
<mat-form-field> | ||
<input matInput class="step-input" [formControlName]="field" type="text" autocomplete="none" appTrimValue /> | ||
</mat-form-field> | ||
</ng-container> | ||
<ng-container> | ||
<mat-form-field appearance="none"> | ||
<label class="step-label">{{ 'FORMS.LABELS.USER_DIRECTIONS' | translate }}</label> | ||
<mat-select | ||
#select | ||
multiple | ||
disableOptionCentering | ||
panelClass="dropdown-panel" | ||
class="step-input" | ||
[compareWith]="compareItems" | ||
[formControl]="this.directionsControl"> | ||
|
||
<mat-select-trigger> | ||
<mat-chip-list #chipList> | ||
<mat-chip *ngFor="let direction of this.directionsControl.value" (removed)="onRemoveItem(direction)"> | ||
<img class="min-logo" src="../../assets/icons/icon_painting.svg" alt="Link" /> | ||
<span>{{ direction.title }}</span> | ||
<mat-icon matChipRemove>cancel</mat-icon> | ||
</mat-chip> | ||
</mat-chip-list> | ||
</mat-select-trigger> | ||
<mat-option class="dropdown-option" *ngFor="let direction of (directions$ | async)" [value]="direction" > | ||
{{ direction.title }} | ||
</mat-option> | ||
</mat-select> | ||
</mat-form-field> | ||
</ng-container> | ||
</form> | ||
|
||
<div fxLayout="row" fxLayoutAlign="center center" class="footer"> | ||
<button mat-raised-button class="btn btn-cancel" (click)="onCancel()">Скасувати</button> | ||
<button class="btn" mat-button type="submit" (click)="onSubmit()">Зберегти</button> | ||
</div> | ||
</div> | ||
</div> |
24 changes: 24 additions & 0 deletions
24
...itution-hierarchies-edit-form/directions-institution-hierarchies-edit-form.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
@import 'src/app/shared/styles/create-form-wrapper.scss'; | ||
@import 'src/app/shared/styles/create-form.scss'; | ||
@import 'src/app/shared/styles/buttons.scss'; | ||
@import 'src/app/shared/styles/validation-form.scss'; | ||
@import 'src/app/shared/styles/dropdown.scss'; | ||
|
||
.warning-box { | ||
width: 410px; | ||
height: 50px; | ||
padding: 5px; | ||
border-radius: 5px; | ||
background-color: #ffdab9; | ||
|
||
i { | ||
color: orange; | ||
} | ||
} | ||
|
||
mat-select { | ||
img { | ||
width: 20px; | ||
height: 20px; | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
...stitution-hierarchies-edit-form/directions-institution-hierarchies-edit-form.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { Component, Inject, AfterViewInit, ViewChild, ElementRef } from '@angular/core'; | ||
import { FormControl, FormGroup } from '@angular/forms'; | ||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; | ||
import { MatOption } from '@angular/material/core'; | ||
import { MatSelect } from '@angular/material/select'; | ||
import { Router } from '@angular/router'; | ||
import { Select, Store } from '@ngxs/store'; | ||
import { Observable, forkJoin, asyncScheduler } from 'rxjs'; | ||
import { InstituitionHierarchy } from '../../../../../shared/models/institution.model'; | ||
import { GetAllInstitutionsHierarchy, GetDirections, UpdateInstitutionHierarchy } from '../../../../../shared/store/meta-data.actions'; | ||
import { MetaDataState } from '../../../../../shared/store/meta-data.state'; | ||
import { Direction } from '../../../../../shared/models/category.model'; | ||
import { DataItem } from '../../../../../shared/models/item.model'; | ||
import { EditInsHierarchyModel } from '../edit-ins-hierarchy-model'; | ||
|
||
@Component({ | ||
selector: 'app-directions-institution-hierarchies-edit-form', | ||
templateUrl: './directions-institution-hierarchies-edit-form.component.html', | ||
styleUrls: ['./directions-institution-hierarchies-edit-form.component.scss'] | ||
}) | ||
export class DirectionsInstitutionHierarchiesEditFormComponent implements AfterViewInit { | ||
@ViewChild('editForm') editForm: ElementRef; | ||
@ViewChild('select') select: MatSelect; | ||
|
||
@Select(MetaDataState.directions) directions$: Observable<Direction[]>; | ||
|
||
readonly ministryControl: string = 'Ministry'; | ||
readonly userDirectionsControl: string = 'USER_DIRECTIONS'; | ||
|
||
public editDirectionFormGroup: FormGroup; | ||
public directionsControl: FormControl = new FormControl([]); | ||
public fields: string[] = []; | ||
|
||
private lastInsHierarchy: InstituitionHierarchy; | ||
private editedInsHierarchies: InstituitionHierarchy[] = []; | ||
|
||
constructor(private router: Router, private dialogRef: MatDialogRef<DirectionsInstitutionHierarchiesEditFormComponent>, | ||
private store: Store, @Inject(MAT_DIALOG_DATA) public data: EditInsHierarchyModel) { | ||
this.store.dispatch(new GetDirections()); | ||
this.lastInsHierarchy = this.getLastInsHierarchy(); | ||
this.buildForm(); | ||
} | ||
|
||
private buildForm(): void { | ||
let directions = [...this.lastInsHierarchy.directions]; | ||
const formGroupFields = this.getFormControlsFields(); | ||
formGroupFields[this.userDirectionsControl] = new FormControl(directions); | ||
this.editDirectionFormGroup = new FormGroup(formGroupFields); | ||
this.directionsControl = this.editDirectionFormGroup.get(this.userDirectionsControl) as FormControl; | ||
} | ||
|
||
private getLastInsHierarchy(): InstituitionHierarchy { | ||
return this.data.element.insHierarchies[this.data.element.insHierarchies.length - 1]; | ||
} | ||
|
||
private getFormControlsFields(): any { | ||
const formGroupFields = {}; | ||
formGroupFields[this.ministryControl] = new FormControl({value: this.data.element.insHierarchies[0].institution.title, disabled: true}); | ||
this.fields.push(this.ministryControl); | ||
let field, title; | ||
for (let i = 0; i < this.data.columns.length; ++i) { | ||
field = this.data.columns[i]; | ||
title = this.data.element.insHierarchies[i]?.title; | ||
if (title) { | ||
formGroupFields[field] = new FormControl(title); | ||
} | ||
else { | ||
formGroupFields[field] = new FormControl({value: null, disabled: true}); | ||
} | ||
this.fields.push(field); | ||
} | ||
return formGroupFields; | ||
} | ||
|
||
private editInstitutionalHierarchy(insHierarchy: InstituitionHierarchy): Observable<InstituitionHierarchy | Observable<void>> { | ||
return this.store.dispatch(new UpdateInstitutionHierarchy(insHierarchy)); | ||
} | ||
|
||
private lastInputFocus(): void { | ||
let inputs = this.editForm.nativeElement.getElementsByTagName('input') as HTMLInputElement[]; | ||
let lastInput = inputs[inputs.length - 1]; | ||
lastInput.focus(); | ||
} | ||
|
||
private reloadPage(): void { | ||
const currentUrl = this.router.url; | ||
this.router.navigateByUrl('/', {skipLocationChange: true}).then(() => { | ||
this.router.navigate([currentUrl]); | ||
}); | ||
} | ||
|
||
private closeDialog(): void { | ||
this.dialogRef.close(); | ||
} | ||
|
||
private compareTwoArrays(array1: Direction[], array2: Direction[]): boolean { | ||
return ( | ||
array1.length === array2.length && | ||
array1.every((first: Direction) => | ||
array2.some((second: Direction) => | ||
Object.keys(first).every((key) => first[key] === second[key]) | ||
) | ||
) | ||
); | ||
} | ||
|
||
public ngAfterViewInit(): void { | ||
asyncScheduler.schedule(() => { | ||
if (this.editForm) { | ||
this.lastInputFocus(); | ||
} | ||
}, 0); | ||
} | ||
|
||
public onCancel(): void { | ||
this.closeDialog(); | ||
} | ||
|
||
public onSubmit(): void { | ||
for (let i = 0; i < this.data.columns.length; ++i) { | ||
const fieldName = this.data.columns[i]; | ||
const field = this.editDirectionFormGroup.controls[fieldName]; | ||
if (field.value != this.data.element.name[i]) { | ||
let editedInsHierarchy = this.data.element.insHierarchies[i]; | ||
editedInsHierarchy.title = field.value; | ||
this.editedInsHierarchies.push(editedInsHierarchy); | ||
} | ||
} | ||
|
||
if (!this.compareTwoArrays(this.directionsControl.value, this.lastInsHierarchy.directions)) { | ||
let editedInsHierarchy = this.editedInsHierarchies.find((ins: InstituitionHierarchy) => ins.id === this.lastInsHierarchy.id); | ||
if (editedInsHierarchy) { | ||
editedInsHierarchy.directions = this.directionsControl.value; | ||
} | ||
else { | ||
editedInsHierarchy = this.lastInsHierarchy; | ||
editedInsHierarchy.directions = this.directionsControl.value; | ||
this.editedInsHierarchies.push(editedInsHierarchy); | ||
} | ||
} | ||
|
||
forkJoin( | ||
this.editedInsHierarchies.map((ins: InstituitionHierarchy) => this.editInstitutionalHierarchy(ins))).subscribe((result) => { | ||
this.store.dispatch(new GetAllInstitutionsHierarchy()); | ||
this.reloadPage(); | ||
}); | ||
|
||
this.closeDialog(); | ||
} | ||
|
||
public compareItems(item1: DataItem, item2: DataItem): boolean { | ||
return item1.id === item2.id; | ||
} | ||
|
||
public onRemoveItem(direction: DataItem): void { | ||
this.directionsControl.value.splice(this.directionsControl.value.indexOf(direction), 1); | ||
this.select.options.find((option: MatOption) => option.value.id === direction.id).deselect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.