|
| 1 | +import React, { useState, useEffect, useRef, Fragment } from 'react'; |
| 2 | +import { isEmpty, isFunction, isNull } from 'lodash'; |
| 3 | +import { Modal, Input, Spin, message as Message, Select, Form } from 'antd'; |
| 4 | +import { API } from '@/api'; |
| 5 | +const FormItem = Form.Item; |
| 6 | +const { TextArea } = Input; |
| 7 | +const { Option } = Select |
| 8 | + |
| 9 | +const EnvForm = (props: any) => { |
| 10 | + const { value, tagList = [], forwardRef } = props; |
| 11 | + const { envName, hostIp, url, remark, tags = [] } = value; |
| 12 | + const tagIds = tags.map((item: any) => Number(item.id)); |
| 13 | + return ( |
| 14 | + <Form |
| 15 | + labelCol={{ span: 5 }} |
| 16 | + wrapperCol={{ span: 17 }} |
| 17 | + ref={forwardRef} |
| 18 | + initialValues={{ |
| 19 | + envName: envName, |
| 20 | + hostIp: hostIp, |
| 21 | + url: url, |
| 22 | + tagIds: tagIds, |
| 23 | + remark: remark |
| 24 | + }} |
| 25 | + > |
| 26 | + <FormItem |
| 27 | + label="环境名称" |
| 28 | + name="envName" |
| 29 | + rules={[{ required: true, message: '请输入环境名称' }]} |
| 30 | + hasFeedback |
| 31 | + > |
| 32 | + <Input placeholder="请输入环境名称" /> |
| 33 | + </FormItem> |
| 34 | + <FormItem |
| 35 | + label="主机IP" |
| 36 | + name="hostIp" |
| 37 | + rules={[{ required: true, message: '请输入主机IP' }]} |
| 38 | + hasFeedback |
| 39 | + > |
| 40 | + <Input placeholder="请输入主机IP" /> |
| 41 | + </FormItem> |
| 42 | + <Fragment> |
| 43 | + <FormItem |
| 44 | + label="访问地址" |
| 45 | + name="url" |
| 46 | + rules={[{ required: true, message: '请输入访问地址' }]} |
| 47 | + hasFeedback |
| 48 | + > |
| 49 | + <TextArea placeholder="请输入访问地址" rows={2} maxLength={2000} /> |
| 50 | + </FormItem> |
| 51 | + </Fragment> |
| 52 | + <FormItem |
| 53 | + label="标签" |
| 54 | + name="tagIds" |
| 55 | + rules={[{ |
| 56 | + type: 'array', |
| 57 | + required: true, message: '请选择标签' |
| 58 | + }]} |
| 59 | + hasFeedback |
| 60 | + > |
| 61 | + <Select mode="multiple" placeholder="请选择标签"> |
| 62 | + { |
| 63 | + tagList.map((item: any) => <Option key={item.id} value={item.id}>{item.tagName}</Option>) |
| 64 | + } |
| 65 | + </Select> |
| 66 | + </FormItem> |
| 67 | + <FormItem |
| 68 | + label="备注" |
| 69 | + name="remark" |
| 70 | + hasFeedback |
| 71 | + > |
| 72 | + <TextArea placeholder="请输入备注" rows={4} /> |
| 73 | + </FormItem> |
| 74 | + </Form> |
| 75 | + ) |
| 76 | +}; |
| 77 | + |
| 78 | +const EnvFormRef = React.forwardRef((props: any, ref: any) => { |
| 79 | + return <EnvForm {...props} forwardRef={ref} /> |
| 80 | +}) |
| 81 | + |
| 82 | +const EnvModal = (props: any) => { |
| 83 | + const { value, visible, onOk, onCancel, tagList } = props; |
| 84 | + const [confirmLoading, setConfirmLoading] = useState(false); |
| 85 | + const envFormRef: any = useRef(null); |
| 86 | + const isAdd = isEmpty(value); |
| 87 | + const { id, envName } = value; |
| 88 | + const handleModalOk = () => { |
| 89 | + if (!isNull(envFormRef.current)) { |
| 90 | + envFormRef.current.validateFields() |
| 91 | + .then((values: any) => { |
| 92 | + setConfirmLoading(true); |
| 93 | + API[isAdd ? 'addEnv' : 'editEnv']({ |
| 94 | + id: isAdd ? undefined : id, |
| 95 | + ...values |
| 96 | + }).then((response: any) => { |
| 97 | + setConfirmLoading(false); |
| 98 | + const { success } = response; |
| 99 | + if (success) { |
| 100 | + Message.success(isAdd ? '环境新增成功' : `环境「${envName}」编辑成功`); |
| 101 | + isFunction(onOk) && onOk(values); |
| 102 | + } |
| 103 | + }) |
| 104 | + }) |
| 105 | + } |
| 106 | + } |
| 107 | + const handleModalCancel = () => { |
| 108 | + isFunction(onCancel) && onCancel(); |
| 109 | + } |
| 110 | + useEffect(() => { |
| 111 | + if (!visible) { |
| 112 | + setConfirmLoading(false); |
| 113 | + } |
| 114 | + }, [props.visible]) |
| 115 | + return <Modal |
| 116 | + title={isAdd ? '新增环境' : '编辑环境'} |
| 117 | + visible={visible} |
| 118 | + maskClosable={false} |
| 119 | + confirmLoading={confirmLoading} |
| 120 | + onOk={handleModalOk} |
| 121 | + onCancel={handleModalCancel}> |
| 122 | + <Spin spinning={confirmLoading}> |
| 123 | + {visible && <EnvFormRef tagList={tagList} value={value} ref={envFormRef} />} |
| 124 | + </Spin> |
| 125 | + </Modal> |
| 126 | +} |
| 127 | +export default EnvModal; |
0 commit comments