1414 * limitations under the License.
1515 *
1616 */
17- import React from 'react' ;
17+ import React , { useEffect } from 'react' ;
1818import _ from 'lodash' ;
1919import { Form , Modal , Input , Select , message } from 'antd' ;
2020import ModalHOC , { ModalWrapProps } from '@/components/ModalHOC' ;
21- import { updateDashboard , createDashboard } from '@/services/dashboardV2' ;
21+ import { updateDashboard , createDashboard , updateDashboardConfigs , getDashboard } from '@/services/dashboardV2' ;
22+ import { JSONParse } from '../utils' ;
2223
2324interface IProps {
2425 mode : 'crate' | 'edit' ;
2526 initialValues ?: any ;
2627 busiId : number ;
2728 refreshList : ( ) => void ;
29+ clusters : string [ ] ;
2830}
2931
3032const layout = {
@@ -41,33 +43,58 @@ const titleMap = {
4143} ;
4244
4345function FormCpt ( props : IProps & ModalWrapProps ) {
44- const { mode, initialValues = { } , visible, busiId, refreshList, destroy } = props ;
46+ const { mode, initialValues = { } , visible, busiId, refreshList, destroy, clusters } = props ;
4547 const [ form ] = Form . useForm ( ) ;
4648 const handleOk = async ( ) => {
47- const values = await form . validateFields ( ) ;
49+ try {
50+ const values = await form . validateFields ( ) ;
51+ let result ;
4852
49- if ( mode === 'edit' ) {
50- await updateDashboard ( initialValues . id , {
51- name : values . name ,
52- tags : _ . join ( values . tags , ' ' ) ,
53- } ) ;
54- message . success ( '编辑大盘成功' ) ;
55- } else if ( mode === 'crate' ) {
56- await createDashboard ( busiId , {
57- name : values . name ,
58- tags : _ . join ( values . tags , ' ' ) ,
59- configs : JSON . stringify ( {
60- var : [ ] ,
61- panels : [ ] ,
62- version : '2.0.0' ,
63- } ) ,
53+ if ( mode === 'edit' ) {
54+ result = await updateDashboard ( initialValues . id , {
55+ name : values . name ,
56+ tags : _ . join ( values . tags , ' ' ) ,
57+ } ) ;
58+ message . success ( '编辑大盘成功' ) ;
59+ } else if ( mode === 'crate' ) {
60+ result = await createDashboard ( busiId , {
61+ name : values . name ,
62+ tags : _ . join ( values . tags , ' ' ) ,
63+ configs : JSON . stringify ( {
64+ var : [ ] ,
65+ panels : [ ] ,
66+ version : '2.0.0' ,
67+ } ) ,
68+ } ) ;
69+ message . success ( '新建大盘成功' ) ;
70+ }
71+ if ( result ) {
72+ const configs = JSONParse ( result . configs ) ;
73+ await updateDashboardConfigs ( result . id , {
74+ configs : JSON . stringify ( {
75+ ...configs ,
76+ datasourceValue : values . datasourceValue ,
77+ } ) ,
78+ } ) ;
79+ }
80+ refreshList ( ) ;
81+ destroy ( ) ;
82+ } catch ( error ) {
83+ message . error ( '操作失败' ) ;
84+ }
85+ } ;
86+
87+ useEffect ( ( ) => {
88+ if ( initialValues . id ) {
89+ getDashboard ( initialValues . id ) . then ( ( res ) => {
90+ const configs = JSONParse ( res . configs ) ;
91+ form . setFieldsValue ( {
92+ datasourceValue : configs . datasourceValue ,
93+ } ) ;
6494 } ) ;
65- message . success ( '新建大盘成功' ) ;
6695 }
96+ } , [ initialValues . id ] ) ;
6797
68- refreshList ( ) ;
69- destroy ( ) ;
70- } ;
7198 return (
7299 < Modal
73100 title = { titleMap [ mode ] }
@@ -109,6 +136,23 @@ function FormCpt(props: IProps & ModalWrapProps) {
109136 placeholder = { '请输入分类标签(请用回车分割)' }
110137 />
111138 </ Form . Item >
139+ < Form . Item
140+ wrapperCol = { {
141+ span : 24 ,
142+ } }
143+ label = '默认集群'
144+ name = 'datasourceValue'
145+ >
146+ < Select >
147+ { _ . map ( clusters , ( item ) => {
148+ return (
149+ < Select . Option key = { item } value = { item } >
150+ { item }
151+ </ Select . Option >
152+ ) ;
153+ } ) }
154+ </ Select >
155+ </ Form . Item >
112156 < Form . Item name = 'id' hidden >
113157 < Input />
114158 </ Form . Item >
0 commit comments