Skip to content

Commit

Permalink
fix(orb-ui) #1303 Code editors expose syntax errors (#2726)
Browse files Browse the repository at this point in the history
* fix(orb-ui) #1303 Code editors expose syntax errors

* lint fix

* fix lint 2
  • Loading branch information
joao-mendonca-encora authored Oct 6, 2023
1 parent 6a73979 commit 5a5e8d0
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeDetectorRef, Component, Input, OnChanges, OnInit, SimpleChange, SimpleChanges } from '@angular/core';
import { ChangeDetectorRef, Component, Input, OnChanges, OnInit, SimpleChange, SimpleChanges } from '@angular/core';
import {
AbstractControl,
FormBuilder,
Expand Down Expand Up @@ -182,8 +182,8 @@ export class DatasetFromComponent implements OnInit, OnChanges {

onMatchingAgentsModal() {
this.dialogService.open(AgentMatchComponent, {
context: {
agentGroupId: this.form.controls.agent_group_id.value,
context: {
agentGroupId: this.form.controls.agent_group_id.value,
policy: this.policy,
},
autoFocus: true,
Expand All @@ -193,8 +193,7 @@ export class DatasetFromComponent implements OnInit, OnChanges {
ngOnChanges(changes: SimpleChanges): void {
if (changes.agent_group_id.currentValue) {
this.isGroupSelected = true;
}
else {
} else {
this.isGroupSelected = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ <h4>Policy View</h4>
[(editMode)]="editMode.interface"
[policy]="policy"
[detailsEditMode]="editMode.details"
[errorConfigMessage]="errorConfigMessage"
></ngx-policy-interface>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class AgentPolicyViewComponent implements OnInit, OnDestroy {

lastUpdate: Date | null = null;

errorConfigMessage: string;

@ViewChild(PolicyDetailsComponent) detailsComponent: PolicyDetailsComponent;

@ViewChild(PolicyInterfaceComponent)
Expand All @@ -75,6 +77,7 @@ export class AgentPolicyViewComponent implements OnInit, OnDestroy {
private editor: CodeEditorService,
) {
this.isRequesting = false;
this.errorConfigMessage = '';
}

ngOnInit() {
Expand All @@ -95,10 +98,14 @@ export class AgentPolicyViewComponent implements OnInit, OnDestroy {


isEditMode() {
return Object.values(this.editMode).reduce(
const resp = Object.values(this.editMode).reduce(
(prev, cur) => prev || cur,
false,
);
if (!resp) {
this.errorConfigMessage = '';
}
return resp;
}

canSave() {
Expand All @@ -109,10 +116,22 @@ export class AgentPolicyViewComponent implements OnInit, OnDestroy {
const config = this.interfaceComponent?.code;
let interfaceValid = false;

if (this.editor.isJson(config)) {
interfaceValid = true;
} else if (this.editor.isYaml(config)) {
interfaceValid = true;
if (this.policy.format === 'json') {
if (this.editor.isJson(config)) {
interfaceValid = true;
this.errorConfigMessage = '';
} else {
interfaceValid = false;
this.errorConfigMessage = 'Invalid JSON configuration, check syntax errors';
}
} else if (this.policy.format === 'yaml') {
if (this.editor.isYaml(config)) {
interfaceValid = true;
this.errorConfigMessage = '';
} else {
interfaceValid = false;
this.errorConfigMessage = 'Invalid YAML configuration, check syntax errors';
}
}
return detailsValid && interfaceValid;
}
Expand Down Expand Up @@ -167,16 +186,16 @@ export class AgentPolicyViewComponent implements OnInit, OnDestroy {

this.policiesService.editAgentPolicy(payload).subscribe(
(resp) => {
this.notifications.success('Agent Policy updated successfully', '');
this.discard();
this.policy = resp;
this.orb.refreshNow();
this.isRequesting = false;
this.notifications.success('Agent Policy updated successfully', '');
this.discard();
this.policy = resp;
this.orb.refreshNow();
this.isRequesting = false;
},
(err) => {
this.isRequesting = false;
},
);
);

} catch (err) {
this.notifications.error(
Expand Down Expand Up @@ -214,17 +233,17 @@ export class AgentPolicyViewComponent implements OnInit, OnDestroy {
}
duplicatePolicy(agentPolicy: any) {
this.policiesService
.duplicateAgentPolicy(agentPolicy.id)
.subscribe((newAgentPolicy) => {
if (newAgentPolicy?.id) {
this.notifications.success(
'Agent Policy Duplicated',
`New Agent Policy Name: ${newAgentPolicy?.name}`,
);
this.router.navigateByUrl(`/pages/datasets/policies/view/${newAgentPolicy?.id}`);
this.fetchData(newAgentPolicy.id);
}
});
.duplicateAgentPolicy(agentPolicy.id)
.subscribe((newAgentPolicy) => {
if (newAgentPolicy?.id) {
this.notifications.success(
'Agent Policy Duplicated',
`New Agent Policy Name: ${newAgentPolicy?.name}`,
);
this.router.navigateByUrl(`/pages/datasets/policies/view/${newAgentPolicy?.id}`);
this.fetchData(newAgentPolicy.id);
}
});
}

ngOnDestroy() {
Expand Down
15 changes: 7 additions & 8 deletions ui/src/app/pages/fleet/agents/match/agent.match.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class AgentMatchComponent implements OnInit, AfterViewInit {
}

onOpenView(agent: any) {
this.router.navigateByUrl(`pages/fleet/agents/view/${ agent.id }`);
this.router.navigateByUrl(`pages/fleet/agents/view/${agent.id}`);
this.dialogRef.close();
}

Expand All @@ -142,10 +142,9 @@ export class AgentMatchComponent implements OnInit, AfterViewInit {
(resp) => {
this.agentGroup = resp;
this.getMatchingAgentsInfo();
}
)
}
else {
},
);
} else {
this.getMatchingAgentsInfo();
}
}
Expand All @@ -157,15 +156,15 @@ export class AgentMatchComponent implements OnInit, AfterViewInit {
if (!!this.policy) {
this.specificPolicy = true;
this.agents = resp.map((agent) => {
const {policy_state} = agent;
const { policy_state } = agent;
const policy_agg_info = !!policy_state && policy_state[this.policy.id]?.state || AgentPolicyStates.failedToApply;
return {...agent, policy_agg_info };
return { ...agent, policy_agg_info };
});
} else {
this.agents = resp;
}
},
);
);
}
onClose() {
this.dialogRef.close(false);
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/pages/sinks/add/sink-add.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ <h4>{{ strings.sink.add.header }}</h4>
</ngx-sink-details>
</div>
<div class="card-col col-8" style="padding-left: 0;">
<ngx-sink-config [(createMode)]="createMode" [(sinkBackend)]="sinkBackend">
<ngx-sink-config [(createMode)]="createMode" [(sinkBackend)]="sinkBackend" [errorConfigMessage]="errorConfigMessage">
</ngx-sink-config>
</div>
</div>
5 changes: 5 additions & 0 deletions ui/src/app/pages/sinks/add/sink-add.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class SinkAddComponent {

isRequesting: boolean;

errorConfigMessage: string;

constructor(
private sinksService: SinksService,
private notificationsService: NotificationsService,
Expand All @@ -37,6 +39,7 @@ export class SinkAddComponent {
) {
this.createMode = true;
this.isRequesting = false;
this.errorConfigMessage = '';
}

canCreate() {
Expand All @@ -51,7 +54,9 @@ export class SinkAddComponent {
config = JSON.parse(configSink);
} else if (this.editor.isYaml(configSink)) {
config = YAML.parse(configSink);
this.errorConfigMessage = '';
} else {
this.errorConfigMessage = 'Invalid YAML configuration, check syntax errors';
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/pages/sinks/view/sink.view.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h4>{{ strings.sink.view.header }}</h4>
</ngx-sink-details>
</div>
<div class="card-col col-8" style="padding-left: 0;">
<ngx-sink-config [(editMode)]="editMode.config" [sink]="sink" [detailsEditMode]="editMode.details">
<ngx-sink-config [(editMode)]="editMode.config" [sink]="sink" [detailsEditMode]="editMode.details" [errorConfigMessage]="errorConfigMessage">
</ngx-sink-config>
</div>
</div>
11 changes: 10 additions & 1 deletion ui/src/app/pages/sinks/view/sink.view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class SinkViewComponent implements OnInit, OnChanges, OnDestroy {

isRequesting: boolean;

errorConfigMessage: string;

@ViewChild(SinkDetailsComponent) detailsComponent: SinkDetailsComponent;

@ViewChild(SinkConfigComponent)
Expand All @@ -56,6 +58,7 @@ export class SinkViewComponent implements OnInit, OnChanges, OnDestroy {
private orb: OrbService,
) {
this.isRequesting = false;
this.errorConfigMessage = '';
}

ngOnInit(): void {
Expand All @@ -74,10 +77,14 @@ export class SinkViewComponent implements OnInit, OnChanges, OnDestroy {
}

isEditMode() {
return Object.values(this.editMode).reduce(
const resp = Object.values(this.editMode).reduce(
(prev, cur) => prev || cur,
false,
);
if (!resp) {
this.errorConfigMessage = '';
}
return resp;
}

canSave() {
Expand All @@ -93,7 +100,9 @@ export class SinkViewComponent implements OnInit, OnChanges, OnDestroy {
config = JSON.parse(configSink);
} else if (this.editor.isYaml(configSink)) {
config = YAML.parse(configSink);
this.errorConfigMessage = '';
} else {
this.errorConfigMessage = 'Invalid YAML configuration, check syntax errors';
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ nb-card {

nb-card-body {
padding-bottom: 0 !important;
margin: 0 !important;

label {
color: #969fb9;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
class="code-editor"
ngDefaultControl>
</ngx-monaco-editor>
<span *ngIf="errorConfigMessage !== ''" class="errorMessage">{{ errorConfigMessage }} </span>
</nb-card-body>
</nb-card>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ nb-card {

nb-card-body {
padding: 0.25rem !important;
margin: 0 !important;

label {
color: #969fb9;
Expand Down Expand Up @@ -84,3 +85,12 @@ nb-card {
.upload-button:hover {
background-color: #171c30 !important;
}

.errorMessage {
position: absolute;
color: #df316f;
font-weight: 600;
font-size: 13px;
left: 20px;
bottom: 3px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export class PolicyInterfaceComponent implements OnInit, AfterViewInit, OnChange
@Input()
detailsEditMode: boolean;

@Input()
errorConfigMessage: string;

@ViewChild(EditorComponent, { static: true })
editorComponent: EditorComponent;

Expand All @@ -44,10 +47,11 @@ export class PolicyInterfaceComponent implements OnInit, AfterViewInit, OnChange
detectIndentation: true,
tabSize: 2,
autoIndent: 'full',
formatOnPaste: true,
trimAutoWhitespace: true,
formatOnType: true,
matchBrackets: 'always',
language: 'yaml',
language: 'json',
automaticLayout: true,
glyphMargin: false,
folding: true,
Expand All @@ -62,6 +66,8 @@ export class PolicyInterfaceComponent implements OnInit, AfterViewInit, OnChange

formControl: FormControl;



constructor(
private fb: FormBuilder,
private orb: OrbService,
Expand All @@ -72,6 +78,7 @@ export class PolicyInterfaceComponent implements OnInit, AfterViewInit, OnChange
this.editModeChange = new EventEmitter<boolean>();
this.updateForm();
this.detailsEditMode = false;
this.errorConfigMessage = '';
}

getCodeLineCount() {
Expand All @@ -87,6 +94,9 @@ export class PolicyInterfaceComponent implements OnInit, AfterViewInit, OnChange

ngOnInit(): void {
this.code = this.policy.policy_data || JSON.stringify(this.policy.policy, null, 2);
if (this.policy.format === 'yaml') {
this.editorOptions = { ...this.editorOptions, language: 'yaml' };
}
}

ngAfterViewInit() {
Expand Down Expand Up @@ -126,8 +136,8 @@ export class PolicyInterfaceComponent implements OnInit, AfterViewInit, OnChange
const reader: FileReader = new FileReader();

reader.onload = (e: any) => {
const fileContent = e.target.result;
this.code = fileContent;
const fileContent = e.target.result;
this.code = fileContent;
};

reader.readAsText(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
class="code-editor editor-height-{{createMode}}"
ngDefaultControl>
</ngx-monaco-editor>
<span *ngIf="errorConfigMessage !== ''" class="errorMessage">{{ errorConfigMessage }} </span>
</div>
</nb-card-body>
</nb-card>
Expand Down
Loading

0 comments on commit 5a5e8d0

Please sign in to comment.