Skip to content

Commit

Permalink
work on types
Browse files Browse the repository at this point in the history
  • Loading branch information
jwetzell committed Aug 14, 2023
1 parent 7387458 commit b17b32c
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 44 deletions.
8 changes: 7 additions & 1 deletion webui/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ export class AppComponent {

downloadConfig() {
const configToDownload = this.pendingConfig ? this.pendingConfig : this.config;
downloadJSON(configToDownload, 'config.json');
if (configToDownload) {
downloadJSON(configToDownload, 'config.json');
} else {
this.snackBar.open('No config to download.', 'Dismiss', {
duration: 3000,
});
}
}

validatePendingConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class ImportConfigComponent {
private schemaService: SchemaService,
public dialogRef: MatDialogRef<ImportConfigComponent>
) {
if (schemaService.schema) {
if (this.schemaService.schema !== undefined) {
this.formGroup = new FormGroup({
config: new FormControl(null, [Validators.required, schemaService.configValidator(this.schemaService.schema)]),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, EventEmitter, Input, Output, TemplateRef, ViewChild } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { SomeJSONSchema } from 'ajv/dist/types/json-schema';
import { Action } from 'src/app/models/action.model';
import { Transform } from 'src/app/models/transform.model';
import { Trigger } from 'src/app/models/trigger.model';
Expand All @@ -18,7 +19,7 @@ export class ObjectFormComponent {
@Output() updated: EventEmitter<Trigger | Action | Transform> = new EventEmitter<Trigger | Action | Transform>();
@ViewChild('settingsDialogRef') dialogRef?: TemplateRef<any>;

schema: any;
schema?: SomeJSONSchema;
formGroup: FormGroup = new FormGroup({
type: new FormControl('any'),
comment: new FormControl(''),
Expand Down
19 changes: 12 additions & 7 deletions webui/src/app/components/params-form/params-form.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { MatTabChangeEvent } from '@angular/material/tabs';
import { SomeJSONSchema } from 'ajv/dist/types/json-schema';
import { cloneDeep, has } from 'lodash';
import { Subscription } from 'rxjs';
import { ParamsFormInfo } from 'src/app/models/form.model';
Expand All @@ -11,21 +12,21 @@ import { SchemaService } from 'src/app/services/schema.service';
styleUrls: ['./params-form.component.css'],
})
export class ParamsFormComponent implements OnInit {
@Input() parentSchema: any;
@Input() parentSchema?: SomeJSONSchema;
@Input() data?: any;
@Output() updated: EventEmitter<any> = new EventEmitter<any>();

paramsSchema: any;
paramsSchema?: SomeJSONSchema;
paramsFormInfo?: ParamsFormInfo;

formGroupSubscription?: Subscription;
paramsOptions: { display: string; paramsFormInfo: ParamsFormInfo; keys: string[]; schema: any }[] = [];
paramsOptions: { display: string; paramsFormInfo: ParamsFormInfo; keys: string[]; schema: SomeJSONSchema }[] = [];
paramsOptionsSelectedIndex: number = 0;

constructor(private schemaService: SchemaService) {}

ngOnInit(): void {
this.paramsSchema = this.parentSchema.properties?.params;
this.paramsSchema = this.parentSchema?.properties?.params;
if (this.paramsSchema) {
if (this.paramsSchema.properties) {
this.paramsFormInfo = this.schemaService.getFormInfoFromParamsSchema(this.paramsSchema);
Expand Down Expand Up @@ -110,7 +111,7 @@ export class ParamsFormComponent implements OnInit {
}
});

const allowedParamKeys = Object.keys(this.paramsSchema.properties);
const allowedParamKeys = Object.keys(this.paramsSchema?.properties);
if (this.data) {
// NOTE(jwetzell): remove keys that aren't allow in the new params variation
Object.keys(this.data).forEach((paramKey) => {
Expand All @@ -132,8 +133,12 @@ export class ParamsFormComponent implements OnInit {
}

formUpdated() {
const params = this.schemaService.cleanParams(this.paramsSchema, this.paramsFormInfo?.formGroup.value);
this.updated.emit(params);
if (this.paramsSchema) {
const params = this.schemaService.cleanParams(this.paramsSchema, this.paramsFormInfo?.formGroup.value);
this.updated.emit(params);
} else {
console.error('params-form: no paramsSchema loaded');
}
}

paramKeys() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<ng-container *ngIf="hasSettings">
<ng-container *ngIf="hasSettings && schema">
<div class="flex items-center hover:cursor-pointer">
<mat-icon class="text-gray-500 hover:text-gray-400" (click)="openSettingsDialog()">settings</mat-icon>
</div>

<ng-template #settingsDialogRef>
<div class="p-2">
<div class="flex justify-around w-full mb-2 text-gray-200">
{{ schema.title }}
{{ schema?.title }}
</div>
<app-params-form
[parentSchema]="schema"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ProtocolFormComponent implements OnInit {
@Output() updated: EventEmitter<ProtocolConfiguration> = new EventEmitter<ProtocolConfiguration>();
@ViewChild('settingsDialogRef') dialogRef?: TemplateRef<any>;

schema: any;
schema?: any;

hasSettings: boolean = false;

Expand All @@ -26,7 +26,7 @@ export class ProtocolFormComponent implements OnInit {
ngOnInit(): void {
if (this.type) {
this.schema = this.schemaService.getSchemaForProtocol(this.type);
if (this.schema.properties.params) {
if (this.schema?.properties.params) {
this.hasSettings = true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions webui/src/app/components/protocol/protocol.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, EventEmitter, Input, Output } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { cloneDeep, merge } from 'lodash';
import { ProtocolConfiguration } from 'src/app/models/config.models';
import { ItemInfo } from 'src/app/models/form.model';
import { ObjectInfo } from 'src/app/models/form.model';
import { Trigger } from 'src/app/models/trigger.model';
import { SchemaService } from 'src/app/services/schema.service';

Expand All @@ -16,7 +16,7 @@ export class ProtocolComponent {
@Input() protocolType?: string;
@Input() protocol?: ProtocolConfiguration;
@Output() updated: EventEmitter<ProtocolConfiguration> = new EventEmitter<ProtocolConfiguration>();
triggerTypes: ItemInfo[] = [];
triggerTypes: ObjectInfo[] = [];

pendingUpdate?: ProtocolConfiguration;

Expand Down
2 changes: 1 addition & 1 deletion webui/src/app/models/form.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FormGroup } from '@angular/forms';

export interface ItemInfo {
export interface ObjectInfo {
name: string;
type: string;
schema: any;
Expand Down
42 changes: 16 additions & 26 deletions webui/src/app/services/schema.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { AbstractControl, FormControl, FormGroup, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
import Ajv, { JSONSchemaType } from 'ajv';
import Ajv, { ErrorObject, JSONSchemaType } from 'ajv';
import { SomeJSONSchema } from 'ajv/dist/types/json-schema';
import { noop } from 'lodash';
import { Action } from '../models/action.model';
import { ConfigFileSchema } from '../models/config.models';
import { ItemInfo, ParamsFormInfo } from '../models/form.model';
import { ObjectInfo, ParamsFormInfo } from '../models/form.model';
import { Trigger } from '../models/trigger.model';

@Injectable({
Expand All @@ -20,9 +20,9 @@ export class SchemaService {

ajv: Ajv = new Ajv();

actionTypes: ItemInfo[] = [];
transformTypes: ItemInfo[] = [];
triggerTypes: ItemInfo[] = [];
actionTypes: ObjectInfo[] = [];
transformTypes: ObjectInfo[] = [];
triggerTypes: ObjectInfo[] = [];

constructor(private http: HttpClient) {}

Expand All @@ -44,10 +44,14 @@ export class SchemaService {
return [];
}

getErrorMessagesFromAjvErrors(data: any) {
getErrorMessagesFromAjvErrors(): ErrorObject<string, Record<string, any>, unknown>[] {
if (this.ajv.errors && this.schema) {
console.log(this.ajv.errors);
}
if (this.ajv.errors === null || this.ajv.errors === undefined) {
return [];
}
return this.ajv.errors;
}

getTemplateForAction(actionType: string): Action {
Expand Down Expand Up @@ -88,7 +92,7 @@ export class SchemaService {
return template;
}

getTemplateForParamsSchema(paramsSchema: any): any {
getTemplateForParamsSchema(paramsSchema: SomeJSONSchema): { [key: string]: any } {
const paramsTemplate = {};
// TODO(jwetzell): make a smart version of this populating fields with defaults
return paramsTemplate;
Expand Down Expand Up @@ -215,7 +219,7 @@ export class SchemaService {
}
}

matchParamsDataToSchema(data: any, schemas: any[]) {
matchParamsDataToSchema(data: any, schemas: SomeJSONSchema[]) {
const matchingSchemaIndex = schemas.findIndex((schema) => {
return this.ajv.validate(schema, data);
});
Expand All @@ -225,16 +229,6 @@ export class SchemaService {
return 0;
}

getParamsForProtocol(protocol: string) {
if (this.schema) {
if (this.schema.properties[protocol]) {
return this.schema.properties.protocol;
}
} else {
console.error('schema is null');
}
}

getFormInfoFromParamsSchema(schema: SomeJSONSchema): ParamsFormInfo {
const paramsFormInfo: ParamsFormInfo = {
formGroup: new FormGroup({}),
Expand Down Expand Up @@ -318,7 +312,7 @@ export class SchemaService {
return paramsFormInfo;
}

cleanParams(paramsSchema: any, params: any): any {
cleanParams(paramsSchema: SomeJSONSchema, params: any): any {
Object.keys(params).forEach((paramKey) => {
// delete null/undefined params
if (params[paramKey] === undefined || params[paramKey] === null) {
Expand Down Expand Up @@ -401,8 +395,8 @@ export class SchemaService {
return params;
}

getTriggerTypesForProtocol(protocolType: string): ItemInfo[] {
const types: ItemInfo[] = [];
getTriggerTypesForProtocol(protocolType: string): ObjectInfo[] {
const types: ObjectInfo[] = [];
if (!this.schema) {
return types;
}
Expand Down Expand Up @@ -452,12 +446,8 @@ export class SchemaService {
}
}

configValidator(validateSchema: any) {
configValidator(validateSchema: JSONSchemaType<ConfigFileSchema>) {
return (control: AbstractControl): ValidationErrors | null => {
if (!validateSchema) {
return null;
}

try {
const configObj = JSON.parse(control.value);
if (this.ajv.validate(validateSchema, configObj)) {
Expand Down
2 changes: 1 addition & 1 deletion webui/src/app/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function downloadJSON(data: any, filename: string) {
export function downloadJSON(data: object, filename: string) {
const content = JSON.stringify(data, null, 2);
const dataUri = URL.createObjectURL(
new Blob([content], {
Expand Down

0 comments on commit b17b32c

Please sign in to comment.