|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import React, { useMemo } from 'react'; |
| 21 | +import i18n from '@/i18n'; |
| 22 | +import { Modal, message } from 'antd'; |
| 23 | +import { ModalProps } from 'antd/es/modal'; |
| 24 | +import FormGenerator, { useForm } from '@/components/FormGenerator'; |
| 25 | +import { useRequest, useUpdateEffect } from '@/hooks'; |
| 26 | +import request from '@/utils/request'; |
| 27 | + |
| 28 | +export interface NodeEditModalProps extends ModalProps { |
| 29 | + id?: number; |
| 30 | + type: string; |
| 31 | + clusterId: number; |
| 32 | +} |
| 33 | + |
| 34 | +const NodeEditModal: React.FC<NodeEditModalProps> = ({ id, type, clusterId, ...modalProps }) => { |
| 35 | + const [form] = useForm(); |
| 36 | + |
| 37 | + const { data: savedData, run: getData } = useRequest( |
| 38 | + id => ({ |
| 39 | + url: `/cluster/node/get/${id}`, |
| 40 | + }), |
| 41 | + { |
| 42 | + manual: true, |
| 43 | + formatResult: result => ({ |
| 44 | + ...result, |
| 45 | + // inCharges: result.inCharges.split(','), |
| 46 | + }), |
| 47 | + onSuccess: result => { |
| 48 | + form.setFieldsValue(result); |
| 49 | + }, |
| 50 | + }, |
| 51 | + ); |
| 52 | + |
| 53 | + const onOk = async () => { |
| 54 | + const values = await form.validateFields(); |
| 55 | + const isUpdate = id; |
| 56 | + const submitData = { |
| 57 | + ...values, |
| 58 | + type, |
| 59 | + parentId: savedData.parentId || clusterId, |
| 60 | + // inCharges: values.inCharges?.join(','), |
| 61 | + }; |
| 62 | + if (isUpdate) { |
| 63 | + submitData.id = id; |
| 64 | + } |
| 65 | + await request({ |
| 66 | + url: `/cluster/node/${isUpdate ? 'update' : 'save'}`, |
| 67 | + method: 'POST', |
| 68 | + data: submitData, |
| 69 | + }); |
| 70 | + await modalProps?.onOk(submitData); |
| 71 | + message.success(i18n.t('basic.OperatingSuccess')); |
| 72 | + }; |
| 73 | + |
| 74 | + useUpdateEffect(() => { |
| 75 | + if (modalProps.visible) { |
| 76 | + // open |
| 77 | + form.resetFields(); |
| 78 | + if (id) { |
| 79 | + getData(id); |
| 80 | + } |
| 81 | + } |
| 82 | + }, [modalProps.visible]); |
| 83 | + |
| 84 | + const content = useMemo(() => { |
| 85 | + return [ |
| 86 | + { |
| 87 | + type: 'input', |
| 88 | + label: 'IP', |
| 89 | + name: 'ip', |
| 90 | + }, |
| 91 | + { |
| 92 | + type: 'input', |
| 93 | + label: i18n.t('pages.Clusters.Node.Port'), |
| 94 | + name: 'port', |
| 95 | + }, |
| 96 | + ]; |
| 97 | + }, []); |
| 98 | + |
| 99 | + return ( |
| 100 | + <Modal {...modalProps} title={i18n.t('pages.Clusters.Node.Name')} onOk={onOk}> |
| 101 | + <FormGenerator content={content} form={form} useMaxWidth /> |
| 102 | + </Modal> |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +export default NodeEditModal; |
0 commit comments