Skip to content

Commit

Permalink
feat: show validation error in the json-editor
Browse files Browse the repository at this point in the history
  • Loading branch information
belsman committed Sep 23, 2024
1 parent 6ded0d2 commit db120ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
15 changes: 5 additions & 10 deletions packages/modules/order/src/lib/components/order-edit.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { transformOrderToInput } from '../utils';
[schema]="schema"
[update]="update"
/> -->
<form class="col flex">
<div class="col flex">
<rc-json-editor
#jsonEditor
[value]="getOrderSource(vm.order)"
Expand All @@ -43,22 +43,17 @@ import { transformOrderToInput } from '../utils';
<div class="py-2 row justify-content-end">
<div class="loose-button-group">
<button class="button transparent">Cancel</button>
<button
vcl-button
class="transparent"
>
Cancel
</button>
<button
vcl-button
class="button"
(click)="onSave()"
[disabled]="jsonError"
>
Save
</button>
</div>
</div>
</form>
</div>
</div>
</ng-container>
`,
Expand Down Expand Up @@ -105,7 +100,7 @@ export class OrderEditComponent {

getOrderSource(order: IOrder): string {
const orderInput = transformOrderToInput(order);
// console.log(order);
console.log('sHOW ANYTHIME WE HOVER...');
// The above log always shows up whenever we hover on the cancel and save button.
// Investigate this by first removing the vcl-button directive to see if this is the
// issue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import 'ace-builds/src-noconflict/theme-github_light_default';
})
export class JSONEditorComponent implements AfterViewInit, OnDestroy {
@HostBinding('class') classNames = 'col w-100p';

private editor!: ace.Ace.Editor;

@Input() value = '';
jsonError = '';

constructor(private elRef: ElementRef) {}

Expand All @@ -36,8 +36,26 @@ export class JSONEditorComponent implements AfterViewInit, OnDestroy {
value: this.value,
});

const innerTextArea = containerElement.querySelector(
'textarea'
) as HTMLTextAreaElement;

this.editor.setTheme('ace/theme/github_light_default');
this.editor.session.setMode('ace/mode/json');
this.editor.session.on('change', () => {
try {
JSON.stringify(JSON.parse(this.getValue()));
this.jsonError = '';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
this.jsonError = err.message;
}

innerTextArea.setCustomValidity(
this.jsonError ? 'Invalid JSON: ' + this.jsonError : ''
);
innerTextArea.reportValidity();
});
}

ngOnDestroy(): void {
Expand Down

0 comments on commit db120ab

Please sign in to comment.