|
| 1 | +import { FormProvider, SubmitHandler, useForm } from 'react-hook-form' |
| 2 | +import { useTranslation } from 'react-i18next' |
| 3 | +import { useEServiceCreateContext } from '../EServiceCreateContext' |
| 4 | +import { EServiceMutations } from '@/api/eservice' |
| 5 | +import { compareObjects } from '@/utils/common.utils' |
| 6 | +import { |
| 7 | + UpdateEServiceDescriptorSeed, |
| 8 | + UpdateEServiceDescriptorTemplateInstanceSeed, |
| 9 | +} from '@/api/api.generatedTypes' |
| 10 | +import { remapDescriptorAttributesToDescriptorAttributesSeed } from '@/utils/attribute.utils' |
| 11 | +import { Box } from '@mui/material' |
| 12 | +import { SectionContainer } from '@/components/layout/containers' |
| 13 | + |
| 14 | +type EServiceCreateStepVersionFormValues = { |
| 15 | + description: string |
| 16 | + agreementApprovalPolicy: boolean |
| 17 | +} |
| 18 | + |
| 19 | +export const EServiceCreateStepInfoVersion: React.FC = () => { |
| 20 | + const { t } = useTranslation('eservice', { keyPrefix: 'create' }) |
| 21 | + |
| 22 | + const { descriptor, forward, back } = useEServiceCreateContext() |
| 23 | + |
| 24 | + const { mutate: updateVersionDraft } = EServiceMutations.useUpdateVersionDraft({ |
| 25 | + suppressSuccessToast: true, |
| 26 | + }) |
| 27 | + |
| 28 | + const { mutate: updateInstanceVersionDraft } = EServiceMutations.useUpdateInstanceVersionDraft({ |
| 29 | + suppressSuccessToast: true, |
| 30 | + }) |
| 31 | + |
| 32 | + const formMethods = useForm<EServiceCreateStepVersionFormValues>({ |
| 33 | + defaultValues: { |
| 34 | + description: descriptor?.description ?? '', |
| 35 | + agreementApprovalPolicy: descriptor ? descriptor.agreementApprovalPolicy === 'MANUAL' : false, |
| 36 | + }, |
| 37 | + }) |
| 38 | + |
| 39 | + const isEServiceCreatedFromTemplate = Boolean(descriptor?.templateRef?.templateVersionId) |
| 40 | + |
| 41 | + const onSubmit: SubmitHandler<EServiceCreateStepVersionFormValues> = (values) => { |
| 42 | + if (!descriptor) return |
| 43 | + |
| 44 | + const newDescriptorData = { |
| 45 | + ...descriptor, |
| 46 | + description: values.description, |
| 47 | + agreementApprovalPolicy: values.agreementApprovalPolicy |
| 48 | + ? ('MANUAL' as const) |
| 49 | + : ('AUTOMATIC' as const), |
| 50 | + } |
| 51 | + |
| 52 | + // If nothing has changed skip the update call |
| 53 | + const areDescriptorsEquals = compareObjects(newDescriptorData, descriptor) |
| 54 | + if (areDescriptorsEquals) { |
| 55 | + forward() |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + if (isEServiceCreatedFromTemplate) { |
| 60 | + const payload: UpdateEServiceDescriptorTemplateInstanceSeed = { |
| 61 | + agreementApprovalPolicy: newDescriptorData.agreementApprovalPolicy, |
| 62 | + audience: descriptor.audience, |
| 63 | + dailyCallsPerConsumer: descriptor.dailyCallsPerConsumer, |
| 64 | + dailyCallsTotal: descriptor.dailyCallsTotal, |
| 65 | + } |
| 66 | + |
| 67 | + updateInstanceVersionDraft( |
| 68 | + { |
| 69 | + ...payload, |
| 70 | + eserviceId: descriptor.eservice.id, |
| 71 | + descriptorId: descriptor.id, |
| 72 | + }, |
| 73 | + { onSuccess: forward } |
| 74 | + ) |
| 75 | + } else { |
| 76 | + const payload: UpdateEServiceDescriptorSeed & { |
| 77 | + eserviceId: string |
| 78 | + descriptorId: string |
| 79 | + } = { |
| 80 | + audience: descriptor.audience, |
| 81 | + voucherLifespan: descriptor.voucherLifespan, |
| 82 | + dailyCallsPerConsumer: descriptor.dailyCallsPerConsumer, |
| 83 | + dailyCallsTotal: descriptor.dailyCallsTotal, |
| 84 | + agreementApprovalPolicy: newDescriptorData.agreementApprovalPolicy, |
| 85 | + description: newDescriptorData.description, |
| 86 | + attributes: remapDescriptorAttributesToDescriptorAttributesSeed(descriptor.attributes), |
| 87 | + eserviceId: descriptor.eservice.id, |
| 88 | + descriptorId: descriptor.id, |
| 89 | + } |
| 90 | + updateVersionDraft( |
| 91 | + { |
| 92 | + ...payload, |
| 93 | + descriptorId: descriptor.id, |
| 94 | + }, |
| 95 | + { onSuccess: forward } |
| 96 | + ) |
| 97 | + } |
| 98 | + } |
| 99 | + return ( |
| 100 | + <FormProvider {...formMethods}> |
| 101 | + <Box component="form" noValidate onSubmit={formMethods.handleSubmit(onSubmit)}></Box> |
| 102 | + </FormProvider> |
| 103 | + ) |
| 104 | +} |
0 commit comments