Skip to content

Commit

Permalink
chore: refactor order edit component
Browse files Browse the repository at this point in the history
  • Loading branch information
belsman committed Sep 17, 2024
1 parent f2ddb65 commit 7f2d183
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 53 deletions.
73 changes: 20 additions & 53 deletions packages/modules/order/src/lib/components/order-edit.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChangeDetectionStrategy,
Component,
ElementRef,
HostBinding,
ViewChild,
} from '@angular/core';
import { Router } from '@angular/router';
Expand All @@ -18,6 +19,7 @@ import {
filterEmptyAndNullishAndUndefined,
} from '@console-core/state';
import { IOrder } from '@console-core/types';
import { JSONEditorComponent } from '@console-modules/ui';

import { buildOrderSchema } from '../jss-forms';
import { transformOrderToInput } from '../utils';
Expand All @@ -26,21 +28,18 @@ import { transformOrderToInput } from '../utils';
selector: 'app-module-order-edit',
template: `
<ng-container *ngIf="vm$ | async as vm">
<div class="mt-2 h-100p">
<div class="mt-2 flex col">
<!-- <rc-crud-edit
[id]="vm.id"
[schema]="schema"
[update]="update"
/> -->
<form>
<!-- <textarea
#rawTextarea
name="json"
(input)="onInput()"
[value]="orderJSON || getOrderSource(vm.order)"
></textarea> -->
<rc-json-editor class="flex" />
<form class="col flex">
<rc-json-editor
#jsonEditor
[value]="getOrderSource(vm.order)"
class="flex"
/>
<div class="py-2 row justify-content-end">
<div class="loose-button-group">
Expand All @@ -63,28 +62,11 @@ import { transformOrderToInput } from '../utils';
</div>
</ng-container>
`,
styles: [
`
:host {
display: block;
height: 100%;
}
form {
display: flex;
height: 100%;
flex-direction: column;
}
textarea {
width: 100%;
flex: 1;
}
`,
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class OrderEditComponent {
@HostBinding('class') classNames = 'col w-100p h-100p';

schema!: VCLFormFieldSchemaRoot;
update = this.orderFacade.update;

Expand All @@ -95,6 +77,9 @@ export class OrderEditComponent {
@ViewChild('rawTextarea')
rawTextarea!: ElementRef;

@ViewChild('jsonEditor')
jsonEditor!: JSONEditorComponent;

readonly vm$ = combineLatest({
id: this.routerFacade.params$.pipe(
map(({ id }) => id),
Expand All @@ -118,9 +103,12 @@ export class OrderEditComponent {
),
});

getOrderSource(order: IOrder) {
getOrderSource(order: IOrder): string {
const orderInput = transformOrderToInput(order);
console.log(order);
// console.log(order);
// 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.
return JSON.stringify(orderInput, null, 4);
}

Expand All @@ -130,32 +118,11 @@ export class OrderEditComponent {
private readonly orderFacade: OrderFacade
) {}

onInput() {
const textarea = this.rawTextarea.nativeElement as HTMLTextAreaElement;
this.orderJSON = textarea.value;

this.modified = true;

let error = '';

try {
JSON.stringify(JSON.parse(this.orderJSON));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
error = e.message;
}

textarea.setCustomValidity(error ? 'Invalid JSON: ' + error : '');
textarea.reportValidity();

this.jsonError = !!error;
}

onSave() {
this.update({
items: [
{
...JSON.parse(this.orderJSON),
...JSON.parse(this.jsonEditor.editor.getValue()),
},
],
mode: ModeType.Update,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Component,
ElementRef,
HostBinding,
Input,
OnDestroy,
} from '@angular/core';
import ace from 'ace-builds';
Expand All @@ -16,6 +17,8 @@ export class JSONEditorComponent implements AfterViewInit, OnDestroy {

editor!: ace.Ace.Editor;

@Input() value = '';

constructor(private elRef: ElementRef) {}

ngAfterViewInit(): void {
Expand All @@ -24,6 +27,7 @@ export class JSONEditorComponent implements AfterViewInit, OnDestroy {
this.editor = ace.edit(containerElement, {
useWorker: false,
printMargin: false,
value: this.value,
});

// Seems we have to import it manually in both cases...
Expand Down

0 comments on commit 7f2d183

Please sign in to comment.