00b65dfe32df8bea49dd367304ae7fdc51e7afbc
[usecase-ui.git] /
1 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2 import { NzMessageService } from 'ng-zorro-antd';
3 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
4 import { KnowledgeBase } from '../knowledge-base.type';
5 import { MaasApi } from '@src/app/api/maas.api';
6
7 @Component({
8   selector: 'app-edit-knowledge-base',
9   templateUrl: './edit-knowledge-base.component.html',
10   styleUrls: ['./edit-knowledge-base.component.less']
11 })
12 export class EditKnowledgeBaseComponent implements OnInit {
13   title = 'Edit Knowledge Base';
14   @Input() showModal: boolean;
15   @Input() knowledgeBaseId: string;
16   @Output() modalOpreation = new EventEmitter();
17   validateForm: FormGroup;
18   defalutKnowledgeBase: KnowledgeBase = {
19     knowledgeBaseName: '',
20     knowledgeBaseDescription: '',
21     knowledgeBaseId: '',
22     operatorName: '',
23     maaSPlatformName: '',
24     maaSPlatformId: '',
25     updateTime: '',
26     fileList: [],
27     operatorId: ''
28   }
29   knowledgeBase: KnowledgeBase = this.defalutKnowledgeBase;
30
31   constructor(
32     private myhttp: MaasApi,
33     private message: NzMessageService,
34     private fb: FormBuilder,
35   ) { }
36
37   ngOnInit() {
38     this.validateForm = this.fb.group({
39       name: [this.knowledgeBase.knowledgeBaseName, [Validators.required]],
40       description: [this.knowledgeBase.knowledgeBaseDescription],
41     });
42     this.fetchKnowledgeBase();
43   }
44
45   checkForm(): void {
46     for (const i in this.validateForm.controls) {
47       this.validateForm.controls[i].markAsDirty();
48       this.validateForm.controls[i].updateValueAndValidity();
49     }
50   }
51
52   submitForm(): void {
53     this.checkForm();
54     this.create();
55   }
56
57   fetchKnowledgeBase(): void {
58     this.myhttp.getKnowledgeBaseById(this.knowledgeBaseId)
59       .subscribe(
60         (response) => {
61           if (response.result_header.result_code !== 200) {
62             this.message.error('get Knowledge Base error');
63             return;
64           }
65           this.knowledgeBase = response.result_body;
66           this.validateForm.patchValue({
67             name: this.knowledgeBase.knowledgeBaseName,
68             description: this.knowledgeBase.knowledgeBaseDescription
69           });
70         },
71         () => {
72           this.message.error('Failed to obtain knowledge base data');
73         }
74       )
75   }
76
77   handleCancel(): void {
78     this.showModal = false;
79     this.modalOpreation.emit({ "cancel": true });
80   }
81
82   create() {
83     const body = {
84       knowledgeBaseId: this.knowledgeBase.knowledgeBaseId,
85       knowledgeBaseName: this.validateForm.get('name').value,
86       knowledgeBaseDescription: this.validateForm.get('description').value,
87       operatorId: this.knowledgeBase.operatorId,
88       operatorName: this.knowledgeBase.operatorName,
89       maaSPlatformId: this.knowledgeBase.maaSPlatformId,
90       maaSPlatformName: this.knowledgeBase.maaSPlatformName
91     };
92     this.myhttp.updateKnowledgeBase(body).subscribe(
93       (response) => {
94         if (response.result_header.result_code === 200) {
95           this.message.success('update knowledge base successfully');
96         } else {
97           this.message.error(response.result_header.result_message);
98         }
99         this.modalOpreation.emit({ "cancel": false });
100       },
101       (error) => {
102         console.log('Upload failed', error);
103       }
104     );
105   }
106 }