Skip to content

Commit

Permalink
SHIBUI-970 Implemented fix for errors in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Nov 14, 2018
1 parent 9b51a67 commit 1d1d0c2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/src/main/resources/i18n/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ message.must-be-unique=Must be unique.
message.name-must-be-unique=Name must be unique.
message.uri-valid-format=URI must be valid format.
message.id-unique=ID must be unique.
message.array-items-must-be-unique=Items in list must be unique.

message.conflict=Conflict
message.data-version-contention=Data Version Contention
Expand Down
6 changes: 6 additions & 0 deletions ui/src/app/schema-form/widget/array/array.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
<span class="text-right" *ngIf="schema.description">
<info-icon [description]="schema.description | translate"></info-icon>
</span>
<small class="form-text text-danger px-2" *ngIf="hasErrors">
<ng-container *ngFor="let error of errors$ | async; let first=first;">
<ng-container *ngIf="!first">, </ng-container>
<translate-i18n [key]="messages[error.code]">{{ error.message }}</translate-i18n>
</ng-container>
</small>
</div>
<ul class="mt-2"
[ngClass]="{'bg-light border rounded p-3 list-group': getListType(formProperty) === 'object'}"
Expand Down
38 changes: 36 additions & 2 deletions ui/src/app/schema-form/widget/array/array.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
import { Component } from '@angular/core';
import { Component, AfterViewInit, OnDestroy } from '@angular/core';

import { ArrayWidget } from 'ngx-schema-form';
import { map } from 'rxjs/operators';
import { Observable, Subscription } from 'rxjs';

export interface FormError {
code: string;
description: string;
message: string;
params: any[];
path: string;
schemaId: any;
}

@Component({
selector: 'array-component',
templateUrl: `./array.component.html`
})
export class CustomArrayComponent extends ArrayWidget {
export class CustomArrayComponent extends ArrayWidget implements AfterViewInit, OnDestroy {
errors$: Observable<FormError[]>;
hasErrors: boolean;
hasErrorSub: Subscription;

messages = {
ARRAY_UNIQUE: 'message.array-items-must-be-unique'
};

ngAfterViewInit(): void {
this.errors$ = this.formProperty.errorsChanges.pipe(
map(errors => errors ? errors.reduce((coll, err) => {
coll[err.code] = err;
return coll;
}, {}) : {}),
map(collection => Object.values(collection))
);

this.hasErrorSub = this.errors$.subscribe(e => this.hasErrors = !!e.length);
}

ngOnDestroy(): void {
this.hasErrorSub.unsubscribe();
}
getListType(property: any): string {
return property.properties.length ? property.properties[0].type : null;
}
Expand Down

0 comments on commit 1d1d0c2

Please sign in to comment.