summaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/views/maas/knowledge-base-management/edit-knowledge-base/edit-knowledge-base.component.ts
blob: 00b65dfe32df8bea49dd367304ae7fdc51e7afbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { KnowledgeBase } from '../knowledge-base.type';
import { MaasApi } from '@src/app/api/maas.api';

@Component({
  selector: 'app-edit-knowledge-base',
  templateUrl: './edit-knowledge-base.component.html',
  styleUrls: ['./edit-knowledge-base.component.less']
})
export class EditKnowledgeBaseComponent implements OnInit {
  title = 'Edit Knowledge Base';
  @Input() showModal: boolean;
  @Input() knowledgeBaseId: string;
  @Output() modalOpreation = new EventEmitter();
  validateForm: FormGroup;
  defalutKnowledgeBase: KnowledgeBase = {
    knowledgeBaseName: '',
    knowledgeBaseDescription: '',
    knowledgeBaseId: '',
    operatorName: '',
    maaSPlatformName: '',
    maaSPlatformId: '',
    updateTime: '',
    fileList: [],
    operatorId: ''
  }
  knowledgeBase: KnowledgeBase = this.defalutKnowledgeBase;

  constructor(
    private myhttp: MaasApi,
    private message: NzMessageService,
    private fb: FormBuilder,
  ) { }

  ngOnInit() {
    this.validateForm = this.fb.group({
      name: [this.knowledgeBase.knowledgeBaseName, [Validators.required]],
      description: [this.knowledgeBase.knowledgeBaseDescription],
    });
    this.fetchKnowledgeBase();
  }

  checkForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
  }

  submitForm(): void {
    this.checkForm();
    this.create();
  }

  fetchKnowledgeBase(): void {
    this.myhttp.getKnowledgeBaseById(this.knowledgeBaseId)
      .subscribe(
        (response) => {
          if (response.result_header.result_code !== 200) {
            this.message.error('get Knowledge Base error');
            return;
          }
          this.knowledgeBase = response.result_body;
          this.validateForm.patchValue({
            name: this.knowledgeBase.knowledgeBaseName,
            description: this.knowledgeBase.knowledgeBaseDescription
          });
        },
        () => {
          this.message.error('Failed to obtain knowledge base data');
        }
      )
  }

  handleCancel(): void {
    this.showModal = false;
    this.modalOpreation.emit({ "cancel": true });
  }

  create() {
    const body = {
      knowledgeBaseId: this.knowledgeBase.knowledgeBaseId,
      knowledgeBaseName: this.validateForm.get('name').value,
      knowledgeBaseDescription: this.validateForm.get('description').value,
      operatorId: this.knowledgeBase.operatorId,
      operatorName: this.knowledgeBase.operatorName,
      maaSPlatformId: this.knowledgeBase.maaSPlatformId,
      maaSPlatformName: this.knowledgeBase.maaSPlatformName
    };
    this.myhttp.updateKnowledgeBase(body).subscribe(
      (response) => {
        if (response.result_header.result_code === 200) {
          this.message.success('update knowledge base successfully');
        } else {
          this.message.error(response.result_header.result_message);
        }
        this.modalOpreation.emit({ "cancel": false });
      },
      (error) => {
        console.log('Upload failed', error);
      }
    );
  }
}