|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * SPDX-FileCopyrightText: Huawei Inc. |
| 4 | + */ |
| 5 | + |
| 6 | +import { CheckCircleOutlined, CloseCircleOutlined, EditOutlined } from '@ant-design/icons'; |
| 7 | +import { Button, Empty, Form, Input, Popconfirm, Row, Skeleton } from 'antd'; |
| 8 | +import React, { useState } from 'react'; |
| 9 | +import '../../../../styles/app.module.css'; |
| 10 | +import serviceOrderStyles from '../../../../styles/service-order.module.css'; |
| 11 | +import { |
| 12 | + type ChangeServiceConfigurationData, |
| 13 | + DeployedServiceDetails, |
| 14 | + ServiceConfigurationUpdate, |
| 15 | + serviceHostingType, |
| 16 | + taskStatus, |
| 17 | + UserOrderableServiceVo, |
| 18 | + VendorHostedDeployedServiceDetails, |
| 19 | +} from '../../../../xpanse-api/generated'; |
| 20 | +import { useLatestServiceOrderStatusQuery } from '../../common/queries/useLatestServiceOrderStatusQuery.ts'; |
| 21 | +import { ServiceConfigParameterItem } from './ServiceConfigParameterItem.tsx'; |
| 22 | +import ServiceConfigStatusAlert from './ServiceConfigStatusAlert.tsx'; |
| 23 | +import useGetCurrentConfigurationOfServiceQuery from './useGetConfigurationOfServiceQuery.ts'; |
| 24 | +import { useUpdateServiceConfigurationRequest } from './useUpdateServiceConfigurationRequest.ts'; |
| 25 | + |
| 26 | +export const ServiceConfigurationDetails = ({ |
| 27 | + userOrderableServiceVo, |
| 28 | + deployedService, |
| 29 | +}: { |
| 30 | + deployedService: DeployedServiceDetails | VendorHostedDeployedServiceDetails; |
| 31 | + userOrderableServiceVo: UserOrderableServiceVo | undefined; |
| 32 | +}): React.JSX.Element => { |
| 33 | + const [form] = Form.useForm(); |
| 34 | + const [isUpdateConfig, setIsUpdateConfig] = useState<boolean>(false); |
| 35 | + const [isShowUpdateConfigResult, setIsShowUpdateConfigResult] = useState<boolean>(false); |
| 36 | + |
| 37 | + const getCurrentConfigurationOfServiceQuery = useGetCurrentConfigurationOfServiceQuery(deployedService.serviceId); |
| 38 | + |
| 39 | + const updateConfigRequest = useUpdateServiceConfigurationRequest(); |
| 40 | + |
| 41 | + const getUpdateConfigStatusPollingQuery = useLatestServiceOrderStatusQuery( |
| 42 | + updateConfigRequest.data?.orderId ?? '', |
| 43 | + updateConfigRequest.isSuccess, |
| 44 | + [taskStatus.SUCCESSFUL, taskStatus.FAILED] |
| 45 | + ); |
| 46 | + |
| 47 | + const updateConfig = (values: ServiceConfigurationUpdate) => { |
| 48 | + setIsShowUpdateConfigResult(true); |
| 49 | + const data: ChangeServiceConfigurationData = { |
| 50 | + requestBody: { configuration: values }, |
| 51 | + serviceId: deployedService.serviceId, |
| 52 | + }; |
| 53 | + updateConfigRequest.mutate(data); |
| 54 | + }; |
| 55 | + |
| 56 | + const initialValues = React.useMemo(() => { |
| 57 | + const config = getCurrentConfigurationOfServiceQuery.data?.configuration ?? {}; |
| 58 | + const numericKeys = |
| 59 | + userOrderableServiceVo?.configurationParameters |
| 60 | + ?.filter((param) => param.dataType === 'number') |
| 61 | + .map((param) => param.name) ?? []; |
| 62 | + |
| 63 | + const numericConfig = Object.keys(config).reduce<Record<string, unknown>>((acc, key) => { |
| 64 | + if (numericKeys.includes(key)) { |
| 65 | + acc[key] = Number(config[key]) || 0; |
| 66 | + } else { |
| 67 | + acc[key] = config[key]; |
| 68 | + } |
| 69 | + return acc; |
| 70 | + }, {}); |
| 71 | + |
| 72 | + return { |
| 73 | + ...numericConfig, |
| 74 | + updatedTime: getCurrentConfigurationOfServiceQuery.data?.updatedTime, |
| 75 | + }; |
| 76 | + }, [getCurrentConfigurationOfServiceQuery.data, userOrderableServiceVo]); |
| 77 | + |
| 78 | + if (getCurrentConfigurationOfServiceQuery.isLoading || getCurrentConfigurationOfServiceQuery.isPending) { |
| 79 | + return <Skeleton active />; |
| 80 | + } |
| 81 | + |
| 82 | + if (!userOrderableServiceVo || !getCurrentConfigurationOfServiceQuery.data) { |
| 83 | + return <Empty />; |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <> |
| 88 | + <div className={serviceOrderStyles.serviceConfigContainer}> |
| 89 | + {isShowUpdateConfigResult ? ( |
| 90 | + <ServiceConfigStatusAlert |
| 91 | + key={deployedService.serviceId} |
| 92 | + serviceId={deployedService.serviceId} |
| 93 | + serviceHostType={deployedService.serviceHostingType as serviceHostingType} |
| 94 | + updateConfigRequest={updateConfigRequest} |
| 95 | + getUpdateConfigStatusPollingQuery={getUpdateConfigStatusPollingQuery} |
| 96 | + /> |
| 97 | + ) : null} |
| 98 | + <Form |
| 99 | + form={form} |
| 100 | + key={'serviceConfiguration'} |
| 101 | + layout='vertical' |
| 102 | + disabled={!isUpdateConfig} |
| 103 | + className={serviceOrderStyles.orderFormInlineDisplay} |
| 104 | + initialValues={initialValues} |
| 105 | + > |
| 106 | + <div className={serviceOrderStyles.orderParamItemRow}> |
| 107 | + <div className={serviceOrderStyles.orderParamItemContent}> |
| 108 | + <Form.Item |
| 109 | + key={getCurrentConfigurationOfServiceQuery.data.updatedTime} |
| 110 | + label={'config last updated at:'} |
| 111 | + initialValue={getCurrentConfigurationOfServiceQuery.data.updatedTime} |
| 112 | + > |
| 113 | + <Input value={getCurrentConfigurationOfServiceQuery.data.updatedTime} disabled={true} /> |
| 114 | + </Form.Item> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + {getCurrentConfigurationOfServiceQuery.data.configuration ? ( |
| 118 | + Object.entries(getCurrentConfigurationOfServiceQuery.data.configuration).map(([key]) => ( |
| 119 | + <div key={key}> |
| 120 | + <ServiceConfigParameterItem |
| 121 | + configParameter={userOrderableServiceVo.configurationParameters?.find( |
| 122 | + (param) => param.name === key |
| 123 | + )} |
| 124 | + /> |
| 125 | + </div> |
| 126 | + )) |
| 127 | + ) : ( |
| 128 | + <></> |
| 129 | + )} |
| 130 | + <Row justify='space-around'> |
| 131 | + <div className={serviceOrderStyles.serviceConfigUpdate}> |
| 132 | + <> |
| 133 | + <Button |
| 134 | + type='primary' |
| 135 | + onClick={() => { |
| 136 | + setIsUpdateConfig(true); |
| 137 | + }} |
| 138 | + disabled={ |
| 139 | + isUpdateConfig || |
| 140 | + updateConfigRequest.isPending || |
| 141 | + updateConfigRequest.isSuccess || |
| 142 | + getUpdateConfigStatusPollingQuery.isSuccess |
| 143 | + } |
| 144 | + > |
| 145 | + <EditOutlined /> Update |
| 146 | + </Button> |
| 147 | + |
| 148 | + <Popconfirm |
| 149 | + title='Update current config' |
| 150 | + description='Are you sure to update current config?' |
| 151 | + cancelText='Yes' |
| 152 | + okText='No' |
| 153 | + onCancel={() => { |
| 154 | + form.validateFields() |
| 155 | + .then((values: ServiceConfigurationUpdate) => { |
| 156 | + updateConfig(values); |
| 157 | + setIsUpdateConfig(false); |
| 158 | + }) |
| 159 | + .catch((errorInfo: unknown) => { |
| 160 | + return errorInfo; |
| 161 | + }); |
| 162 | + }} |
| 163 | + > |
| 164 | + <Button type='primary' htmlType='submit' disabled={!isUpdateConfig}> |
| 165 | + <CheckCircleOutlined /> Submit |
| 166 | + </Button> |
| 167 | + </Popconfirm> |
| 168 | + |
| 169 | + <Button |
| 170 | + type='primary' |
| 171 | + onClick={() => { |
| 172 | + form.resetFields(); |
| 173 | + setIsUpdateConfig(false); |
| 174 | + }} |
| 175 | + disabled={!isUpdateConfig} |
| 176 | + > |
| 177 | + <CloseCircleOutlined /> Cancel |
| 178 | + </Button> |
| 179 | + </> |
| 180 | + </div> |
| 181 | + </Row> |
| 182 | + </Form> |
| 183 | + </div> |
| 184 | + </> |
| 185 | + ); |
| 186 | +}; |
0 commit comments