Skip to content

Commit deebae9

Browse files
committed
fix(invoice): update json editor when an invoice is selected
1 parent 7ff52ff commit deebae9

4 files changed

Lines changed: 40 additions & 11 deletions

File tree

‎packages/modules/invoice/src/lib/components/invoice-edit.component.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { VCLFormFieldSchemaRoot } from '@vcl/ng-vcl';
1212

1313
import { ROUTER } from '@console-core/config';
1414
import { ModeType } from '@console-core/graphql';
15-
import { invoiceToInputDTO } from '@console-core/mappers';
1615
import {
1716
CustomerFacade,
1817
IamFacade,
@@ -21,7 +20,6 @@ import {
2120
ShopFacade,
2221
filterEmptyAndNullishAndUndefined,
2322
} from '@console-core/state';
24-
import { IInvoice } from '@console-core/types';
2523
import { omitNullishAndUndefined } from '@console-modules/shared';
2624
import { JSONEditorComponent } from '@console-modules/ui';
2725

@@ -35,7 +33,7 @@ import { buildInvoiceSchema } from '../jss-forms';
3533
<div class="col flex">
3634
<rc-json-editor
3735
#jsonEditor
38-
[value]="getInvoiceSource(vm.invoice)"
36+
[value]="vm.invoice | invoiceSource"
3937
class="flex"
4038
/>
4139
@@ -117,11 +115,6 @@ export class InvoiceEditComponent {
117115
private readonly iamFacade: IamFacade
118116
) {}
119117

120-
getInvoiceSource(invoice: IInvoice): string {
121-
const invoiceInput = invoiceToInputDTO(invoice);
122-
return JSON.stringify(invoiceInput, null, 4);
123-
}
124-
125118
onSave() {
126119
this.update({
127120
items: [

‎packages/modules/invoice/src/lib/modules-invoice.module.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { InvoiceViewDetailsComponent } from './components/invoice-view-details.c
1010
import { InvoiceViewComponent } from './components/invoice-view.component';
1111
import { InvoiceTemplateComponent } from './components/template/invoice-template.component';
1212
import { modulesInvoiceRoutes } from './lib.routes';
13+
import { InvoiceSourcePipe } from './pipes/invoice-sources.pipe';
1314

1415
@NgModule({
1516
declarations: [
@@ -19,6 +20,7 @@ import { modulesInvoiceRoutes } from './lib.routes';
1920
InvoiceViewComponent,
2021
InvoiceViewDetailsComponent,
2122
InvoiceTemplateComponent,
23+
InvoiceSourcePipe,
2224
],
2325
imports: [
2426
ModulesUiModule.forChild(),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
import { invoiceToInputDTO } from '@console-core/mappers';
4+
import { IInvoice } from '@console-core/types';
5+
6+
@Pipe({
7+
name: 'invoiceSource',
8+
standalone: false,
9+
})
10+
export class InvoiceSourcePipe implements PipeTransform {
11+
transform(invoice: IInvoice): string {
12+
const invoiceInput = invoiceToInputDTO(invoice);
13+
return JSON.stringify(invoiceInput, null, 4);
14+
}
15+
}

‎packages/modules/ui/src/lib/components/organisms/json-editor/json-editor.component.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
ElementRef,
55
HostBinding,
66
Input,
7+
OnChanges,
78
OnDestroy,
9+
SimpleChanges,
810
} from '@angular/core';
911
import ace from 'ace-builds';
1012
import 'ace-builds/src-noconflict/mode-json';
@@ -15,7 +17,9 @@ import 'ace-builds/src-noconflict/theme-github_light_default';
1517
templateUrl: './json-editor.component.html',
1618
standalone: false,
1719
})
18-
export class JSONEditorComponent implements AfterViewInit, OnDestroy {
20+
export class JSONEditorComponent
21+
implements AfterViewInit, OnChanges, OnDestroy
22+
{
1923
@HostBinding('class') classNames = 'col w-100p';
2024
private editor!: ace.Ace.Editor;
2125

@@ -45,8 +49,10 @@ export class JSONEditorComponent implements AfterViewInit, OnDestroy {
4549
this.editor.session.setMode('ace/mode/json');
4650
this.editor.session.on('change', () => {
4751
try {
48-
JSON.stringify(JSON.parse(this.getValue()));
49-
this.jsonError = '';
52+
if (this.getValue()) {
53+
JSON.stringify(JSON.parse(this.getValue()));
54+
this.jsonError = '';
55+
}
5056
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5157
} catch (err: any) {
5258
this.jsonError = err.message;
@@ -59,6 +65,19 @@ export class JSONEditorComponent implements AfterViewInit, OnDestroy {
5965
});
6066
}
6167

68+
ngOnChanges(changes: SimpleChanges): void {
69+
if (changes['value'] && this.editor && !this.editor.isFocused()) {
70+
const next = changes['value'].currentValue ?? '';
71+
if (next !== this.editor.getValue()) {
72+
const scrollTop = this.editor.session.getScrollTop();
73+
const pos = this.editor.getCursorPosition();
74+
this.editor.session.setValue(next);
75+
this.editor.session.setScrollTop(scrollTop);
76+
this.editor.moveCursorToPosition(pos);
77+
}
78+
}
79+
}
80+
6281
ngOnDestroy(): void {
6382
this.editor.destroy();
6483
this.editor.container.remove();

0 commit comments

Comments
 (0)