|
| 1 | +import BAIModal, { BAIModalProps } from './BAIModal'; |
| 2 | +import DeploymentMetadataFormItem from './DeploymentMetadataFormItem'; |
| 3 | +import DeploymentNetworkAccessFormItem from './DeploymentNetworkAccessFormItem'; |
| 4 | +import DeploymentStrategyFormItem from './DeploymentStrategyFormItem'; |
| 5 | +import { App, Form, FormInstance } from 'antd'; |
| 6 | +import { toLocalId } from 'backend.ai-ui'; |
| 7 | +import { useRef } from 'react'; |
| 8 | +import { useTranslation } from 'react-i18next'; |
| 9 | +import { graphql, useFragment, useMutation } from 'react-relay'; |
| 10 | +import { |
| 11 | + DeploymentModifyModalFragment$data, |
| 12 | + DeploymentModifyModalFragment$key, |
| 13 | +} from 'src/__generated__/DeploymentModifyModalFragment.graphql'; |
| 14 | +import { DeploymentModifyModalMutation } from 'src/__generated__/DeploymentModifyModalMutation.graphql'; |
| 15 | + |
| 16 | +interface DeploymentModifyModalProps extends BAIModalProps { |
| 17 | + deploymentFrgmt?: DeploymentModifyModalFragment$key | null; |
| 18 | + onRequestClose: (success?: boolean) => void; |
| 19 | +} |
| 20 | + |
| 21 | +const DeploymentModifyModal: React.FC<DeploymentModifyModalProps> = ({ |
| 22 | + onRequestClose, |
| 23 | + deploymentFrgmt, |
| 24 | + ...baiModalProps |
| 25 | +}) => { |
| 26 | + const { t } = useTranslation(); |
| 27 | + const { message } = App.useApp(); |
| 28 | + const formRef = |
| 29 | + useRef<FormInstance<DeploymentModifyModalFragment$data>>(null); |
| 30 | + |
| 31 | + const deployment = useFragment( |
| 32 | + graphql` |
| 33 | + fragment DeploymentModifyModalFragment on ModelDeployment { |
| 34 | + id |
| 35 | + metadata { |
| 36 | + name |
| 37 | + tags |
| 38 | + } |
| 39 | + networkAccess { |
| 40 | + openToPublic |
| 41 | + preferredDomainName |
| 42 | + } |
| 43 | + defaultDeploymentStrategy { |
| 44 | + type |
| 45 | + } |
| 46 | + replicaState { |
| 47 | + desiredReplicaCount |
| 48 | + } |
| 49 | + } |
| 50 | + `, |
| 51 | + deploymentFrgmt, |
| 52 | + ); |
| 53 | + |
| 54 | + const [commitUpdateDeployment, isInFlightUpdateDeployment] = |
| 55 | + useMutation<DeploymentModifyModalMutation>(graphql` |
| 56 | + mutation DeploymentModifyModalMutation( |
| 57 | + $input: UpdateModelDeploymentInput! |
| 58 | + ) { |
| 59 | + updateModelDeployment(input: $input) { |
| 60 | + deployment { |
| 61 | + id |
| 62 | + metadata { |
| 63 | + name |
| 64 | + tags |
| 65 | + } |
| 66 | + networkAccess { |
| 67 | + openToPublic |
| 68 | + } |
| 69 | + defaultDeploymentStrategy { |
| 70 | + type |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + `); |
| 76 | + |
| 77 | + const handleOk = () => { |
| 78 | + formRef.current?.validateFields().then((values) => { |
| 79 | + commitUpdateDeployment({ |
| 80 | + variables: { |
| 81 | + input: { |
| 82 | + id: toLocalId(deployment?.id || ''), |
| 83 | + name: values.metadata?.name, |
| 84 | + tags: values.metadata?.tags, |
| 85 | + defaultDeploymentStrategy: values?.defaultDeploymentStrategy, |
| 86 | + desiredReplicaCount: values.replicaState?.desiredReplicaCount, |
| 87 | + preferredDomainName: values.networkAccess?.preferredDomainName, |
| 88 | + openToPublic: values.networkAccess?.openToPublic, |
| 89 | + }, |
| 90 | + }, |
| 91 | + onCompleted: (res, errors) => { |
| 92 | + if (!res?.updateModelDeployment?.deployment?.id) { |
| 93 | + message.error(t('message.FailedToUpdate')); |
| 94 | + return; |
| 95 | + } |
| 96 | + if (errors && errors.length > 0) { |
| 97 | + const errorMsgList = errors.map((error) => error.message); |
| 98 | + for (const error of errorMsgList) { |
| 99 | + message.error(error); |
| 100 | + } |
| 101 | + } else { |
| 102 | + message.success(t('message.SuccessfullyUpdated')); |
| 103 | + onRequestClose(true); |
| 104 | + } |
| 105 | + }, |
| 106 | + onError: (err) => { |
| 107 | + message.error(err.message || t('message.FailedToUpdate')); |
| 108 | + }, |
| 109 | + }); |
| 110 | + }); |
| 111 | + }; |
| 112 | + |
| 113 | + return ( |
| 114 | + <BAIModal |
| 115 | + {...baiModalProps} |
| 116 | + destroyOnClose |
| 117 | + onOk={handleOk} |
| 118 | + onCancel={() => onRequestClose(false)} |
| 119 | + okText={t('button.Update')} |
| 120 | + confirmLoading={isInFlightUpdateDeployment} |
| 121 | + title={t('deployment.ModifyDeployment')} |
| 122 | + > |
| 123 | + <Form |
| 124 | + ref={formRef} |
| 125 | + layout="vertical" |
| 126 | + initialValues={{ |
| 127 | + ...deployment, |
| 128 | + }} |
| 129 | + style={{ maxWidth: '100%' }} |
| 130 | + > |
| 131 | + <DeploymentMetadataFormItem /> |
| 132 | + <DeploymentStrategyFormItem /> |
| 133 | + <DeploymentNetworkAccessFormItem /> |
| 134 | + </Form> |
| 135 | + </BAIModal> |
| 136 | + ); |
| 137 | +}; |
| 138 | + |
| 139 | +export default DeploymentModifyModal; |
0 commit comments