|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { useForm } from 'react-hook-form'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import { useNavigate } from 'react-router-dom'; |
| 5 | +import { isNil } from 'lodash'; |
| 6 | +import * as yup from 'yup'; |
| 7 | +import { WizardProps } from '@cloudscape-design/components'; |
| 8 | + |
| 9 | +import { Container, FormSelect, KeyValuePairs, SpaceBetween, Wizard } from 'components'; |
| 10 | + |
| 11 | +import { useBreadcrumbs, useConfirmationDialog, useNotifications } from 'hooks'; |
| 12 | +import { useProjectFilter } from 'hooks/useProjectFilter'; |
| 13 | +import { ROUTES } from 'routes'; |
| 14 | +import { useApplyFleetMutation } from 'services/fleet'; |
| 15 | + |
| 16 | +import { useYupValidationResolver } from 'pages/Project/hooks/useYupValidationResolver'; |
| 17 | + |
| 18 | +import { getMaxInstancesValidator, getMinInstancesValidator, idleDurationValidator } from './FleetFormFields/constants'; |
| 19 | +import { FleetFormFields } from './FleetFormFields'; |
| 20 | + |
| 21 | +import { IFleetWizardForm } from './types'; |
| 22 | + |
| 23 | +const requiredFieldError = 'This is required field'; |
| 24 | +const namesFieldError = 'Only latin characters, dashes, underscores, and digits'; |
| 25 | + |
| 26 | +const fleetStepIndex = 1; |
| 27 | + |
| 28 | +const fleetValidationSchema = yup.object({ |
| 29 | + project_name: yup |
| 30 | + .string() |
| 31 | + .required(requiredFieldError) |
| 32 | + .matches(/^[a-zA-Z0-9-_]+$/, namesFieldError), |
| 33 | + min_instances: getMinInstancesValidator('max_instances'), |
| 34 | + max_instances: getMaxInstancesValidator('min_instances'), |
| 35 | + idle_duration: idleDurationValidator, |
| 36 | +}); |
| 37 | + |
| 38 | +export const FleetAdd: React.FC = () => { |
| 39 | + const { t } = useTranslation(); |
| 40 | + const navigate = useNavigate(); |
| 41 | + const [pushNotification] = useNotifications(); |
| 42 | + const [openConfirmationDialog] = useConfirmationDialog(); |
| 43 | + const [applyFleet, { isLoading: isApplyingFleet }] = useApplyFleetMutation(); |
| 44 | + const [activeStepIndex, setActiveStepIndex] = useState(0); |
| 45 | + const resolver = useYupValidationResolver(fleetValidationSchema); |
| 46 | + const { projectOptions } = useProjectFilter({ localStorePrefix: 'add-fleet-page' }); |
| 47 | + |
| 48 | + const loading = isApplyingFleet; |
| 49 | + |
| 50 | + const formMethods = useForm<IFleetWizardForm>({ |
| 51 | + resolver, |
| 52 | + defaultValues: { |
| 53 | + min_instances: 0, |
| 54 | + idle_duration: '5m', |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + const { handleSubmit, control, clearErrors, trigger, watch, getValues } = formMethods; |
| 59 | + const formValues = watch(); |
| 60 | + |
| 61 | + const getFormValuesForFleetApplying = (): IApplyFleetPlanRequestRequest => { |
| 62 | + const { min_instances, max_instances, idle_duration, name } = getValues(); |
| 63 | + |
| 64 | + return { |
| 65 | + plan: { |
| 66 | + spec: { |
| 67 | + configuration: { |
| 68 | + ...(name ? { name } : {}), |
| 69 | + nodes: { |
| 70 | + min: min_instances, |
| 71 | + ...(max_instances ? { max: max_instances } : {}), |
| 72 | + }, |
| 73 | + ...(idle_duration ? { idle_duration } : {}), |
| 74 | + }, |
| 75 | + profile: {}, |
| 76 | + }, |
| 77 | + }, |
| 78 | + force: false, |
| 79 | + }; |
| 80 | + }; |
| 81 | + |
| 82 | + useBreadcrumbs([ |
| 83 | + { |
| 84 | + text: t('navigation.fleets'), |
| 85 | + href: ROUTES.FLEETS.LIST, |
| 86 | + }, |
| 87 | + |
| 88 | + { |
| 89 | + text: t('common.create_wit_text', { text: t('navigation.fleet') }), |
| 90 | + href: ROUTES.FLEETS.ADD, |
| 91 | + }, |
| 92 | + ]); |
| 93 | + |
| 94 | + const validateName = async () => { |
| 95 | + return await trigger(['project_name']); |
| 96 | + }; |
| 97 | + |
| 98 | + const validateFleet = async () => { |
| 99 | + return await trigger(['min_instances', 'max_instances', 'idle_duration']); |
| 100 | + }; |
| 101 | + |
| 102 | + const emptyValidator = async () => Promise.resolve(true); |
| 103 | + |
| 104 | + const onNavigate = ({ |
| 105 | + requestedStepIndex, |
| 106 | + reason, |
| 107 | + }: { |
| 108 | + requestedStepIndex: number; |
| 109 | + reason: WizardProps.NavigationReason; |
| 110 | + }) => { |
| 111 | + const stepValidators = [validateName, validateFleet, emptyValidator]; |
| 112 | + |
| 113 | + if (reason === 'next') { |
| 114 | + stepValidators[activeStepIndex]?.().then((isValid) => { |
| 115 | + if (isValid) { |
| 116 | + if (activeStepIndex === fleetStepIndex && formValues?.['min_instances'] > 0) { |
| 117 | + openConfirmationDialog({ |
| 118 | + title: 'Are sure want to set min instances above than 0?', |
| 119 | + content: null, |
| 120 | + onConfirm: () => setActiveStepIndex(requestedStepIndex), |
| 121 | + }); |
| 122 | + } else { |
| 123 | + setActiveStepIndex(requestedStepIndex); |
| 124 | + } |
| 125 | + } |
| 126 | + }); |
| 127 | + } else { |
| 128 | + setActiveStepIndex(requestedStepIndex); |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + const onNavigateHandler: WizardProps['onNavigate'] = ({ detail: { requestedStepIndex, reason } }) => { |
| 133 | + onNavigate({ requestedStepIndex, reason }); |
| 134 | + }; |
| 135 | + |
| 136 | + const onCancelHandler = () => { |
| 137 | + navigate(ROUTES.FLEETS.LIST); |
| 138 | + }; |
| 139 | + |
| 140 | + const onSubmitWizard = async () => { |
| 141 | + const isValid = await trigger(); |
| 142 | + |
| 143 | + const { project_name } = getValues(); |
| 144 | + |
| 145 | + if (!isValid) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + clearErrors(); |
| 150 | + |
| 151 | + const request = applyFleet({ |
| 152 | + projectName: project_name, |
| 153 | + ...getFormValuesForFleetApplying(), |
| 154 | + }).unwrap(); |
| 155 | + |
| 156 | + request |
| 157 | + .then(async (data) => { |
| 158 | + pushNotification({ |
| 159 | + type: 'success', |
| 160 | + content: t('fleets.create.success_notification'), |
| 161 | + }); |
| 162 | + |
| 163 | + navigate(ROUTES.FLEETS.DETAILS.FORMAT(data.project_name, data.id)); |
| 164 | + }) |
| 165 | + .catch((error) => { |
| 166 | + pushNotification({ |
| 167 | + type: 'error', |
| 168 | + content: t('common.server_error', { error: error?.error ?? error }), |
| 169 | + }); |
| 170 | + }); |
| 171 | + }; |
| 172 | + |
| 173 | + const onSubmit = () => { |
| 174 | + if (activeStepIndex < 2) { |
| 175 | + onNavigate({ requestedStepIndex: activeStepIndex + 1, reason: 'next' }); |
| 176 | + } else { |
| 177 | + onSubmitWizard().catch(console.log); |
| 178 | + } |
| 179 | + }; |
| 180 | + |
| 181 | + const getDefaultFleetSummary = () => { |
| 182 | + const summaryFields: Array<keyof IFleetWizardForm> = ['name', 'min_instances', 'max_instances', 'idle_duration']; |
| 183 | + |
| 184 | + const result: string[] = []; |
| 185 | + |
| 186 | + summaryFields.forEach((fieldName) => { |
| 187 | + if (!isNil(formValues?.[fieldName])) { |
| 188 | + result.push(`${t(`fleets.edit.${fieldName}`)}: ${formValues?.[fieldName]}`); |
| 189 | + } |
| 190 | + }); |
| 191 | + |
| 192 | + return result.join(', '); |
| 193 | + }; |
| 194 | + |
| 195 | + return ( |
| 196 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 197 | + <Wizard |
| 198 | + activeStepIndex={activeStepIndex} |
| 199 | + onNavigate={onNavigateHandler} |
| 200 | + onSubmit={onSubmitWizard} |
| 201 | + i18nStrings={{ |
| 202 | + stepNumberLabel: (stepNumber) => `Step ${stepNumber}`, |
| 203 | + navigationAriaLabel: 'Steps', |
| 204 | + cancelButton: t('common.cancel'), |
| 205 | + previousButton: t('common.previous'), |
| 206 | + nextButton: t('common.next'), |
| 207 | + optional: 'optional', |
| 208 | + }} |
| 209 | + onCancel={onCancelHandler} |
| 210 | + submitButtonText={t('projects.wizard.submit')} |
| 211 | + steps={[ |
| 212 | + { |
| 213 | + title: 'Project', |
| 214 | + content: ( |
| 215 | + <Container> |
| 216 | + <SpaceBetween direction="vertical" size="l"> |
| 217 | + <FormSelect |
| 218 | + label={t('projects.edit.project_name')} |
| 219 | + control={control} |
| 220 | + name="project_name" |
| 221 | + options={projectOptions} |
| 222 | + disabled={loading} |
| 223 | + /> |
| 224 | + </SpaceBetween> |
| 225 | + </Container> |
| 226 | + ), |
| 227 | + }, |
| 228 | + { |
| 229 | + title: 'Fleets', |
| 230 | + content: ( |
| 231 | + <Container> |
| 232 | + <SpaceBetween direction="vertical" size="l"> |
| 233 | + <FleetFormFields<IFleetWizardForm> control={control} disabledAllFields={loading} /> |
| 234 | + </SpaceBetween> |
| 235 | + </Container> |
| 236 | + ), |
| 237 | + }, |
| 238 | + { |
| 239 | + title: 'Summary', |
| 240 | + content: ( |
| 241 | + <Container> |
| 242 | + <KeyValuePairs |
| 243 | + items={[ |
| 244 | + { |
| 245 | + label: t('projects.edit.project_name'), |
| 246 | + value: formValues['project_name'], |
| 247 | + }, |
| 248 | + { |
| 249 | + label: 'Default fleet', |
| 250 | + value: getDefaultFleetSummary(), |
| 251 | + }, |
| 252 | + ]} |
| 253 | + /> |
| 254 | + </Container> |
| 255 | + ), |
| 256 | + }, |
| 257 | + ]} |
| 258 | + /> |
| 259 | + </form> |
| 260 | + ); |
| 261 | +}; |
0 commit comments