From 78b18af3ffa3c85602b4f5be90b35e540e96b404 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Fri, 12 Feb 2021 16:12:58 +0900 Subject: [PATCH] Add ConfigTemplateModel --- frontend/models/config/config-template.ts | 58 +++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 frontend/models/config/config-template.ts diff --git a/frontend/models/config/config-template.ts b/frontend/models/config/config-template.ts new file mode 100644 index 00000000..3b75d2d0 --- /dev/null +++ b/frontend/models/config/config-template.ts @@ -0,0 +1,58 @@ +export interface Schema { + title: string, + type: string, + properties: object +} + +export interface ConfigResponse { + name: string, + schema: Schema, + template: string +} + +export class ConfigTemplateItem { + constructor( + private schema: Schema, + public template: string + ) {} + + static valueOf( + { schema, template }: + { schema: Schema, template: string } + ): ConfigTemplateItem { + return new ConfigTemplateItem(schema, template) + } + + get modelName(): string { + return this.schema.title + } + + get fields() { + const response = [] + for (const [key, value] of Object.entries(this.schema.properties)) { + if ('type' in value && value.type === 'string') { + response.push({name: key, type: 'textField', value: ''}) + } else if ('anyOf' in value) { + response.push( + { + name: key, + type: 'selectField', + value: '', + items: value['anyOf'].map( + (item: {'const': string, 'type': string}) => item.const + ) + } + ) + } + } + return response + } + + toObject(): Object { + return { + model_name: this.modelName, + template: this.template, + model_attrs: this.fields + } + } +}