|
1 | 1 | import React, { Suspense } from 'react'; |
2 | | -import z from 'zod'; |
3 | 2 | import { Drawer } from '@ovh-ux/manager-react-components'; |
4 | 3 | import { useTranslation } from 'react-i18next'; |
5 | 4 | import { useNavigate, useParams } from 'react-router-dom'; |
6 | | -import { useForm } from 'react-hook-form'; |
7 | 5 | import { OdsMessage } from '@ovhcloud/ods-components/react'; |
8 | | -import { useSecretDataSchema } from '@secret-manager/validation'; |
9 | | -import { NAMESPACES } from '@ovh-ux/manager-common-translations'; |
10 | | -import { zodResolver } from '@hookform/resolvers/zod/dist/zod'; |
11 | | -import { SecretWithData } from '@secret-manager/types/secret.type'; |
12 | 6 | import { useSecretWithData } from '@secret-manager/data/hooks/useSecret'; |
13 | 7 | import { useSecretSmartConfig } from '@secret-manager/hooks/useSecretSmartConfig'; |
14 | 8 | import { decodeSecretPath } from '@secret-manager/utils/secretPath'; |
15 | | -import { useCreateSecretVersion } from '@secret-manager/data/hooks/useCreateSecretVersion'; |
16 | 9 | import { LocationPathParams } from '@secret-manager/routes/routes.constants'; |
17 | | -import { SecretDataFormField } from '@secret-manager/components/form/SecretDataFormField.component'; |
18 | | -import { SecretSmartConfig } from '@secret-manager/utils/secretSmartConfig'; |
19 | | -import { addCurrentVersionToCas } from '@secret-manager/utils/cas'; |
20 | | -import { |
21 | | - DrawerContent, |
22 | | - DrawerFooter, |
23 | | -} from '@/common/components/drawer/DrawerInnerComponents.component'; |
24 | | -import { VersionStatusMessage } from './VersionStatusMessage.component'; |
| 10 | +import { CreateVersionDrawerForm } from './CreateVersionDrawerForm.component'; |
25 | 11 | import { CREATE_VERSION_DRAWER_TEST_IDS } from './CreateVersionDrawer.constants'; |
26 | 12 |
|
27 | | -type CreateVersionDrawerProps = { |
28 | | - secret: SecretWithData; |
29 | | - okmsId: string; |
30 | | - secretPath: string; |
31 | | - secretConfig: SecretSmartConfig; |
32 | | - onDismiss: () => void; |
33 | | -}; |
34 | | - |
35 | | -const CreateVersionDrawerForm = ({ |
36 | | - secret, |
37 | | - okmsId, |
38 | | - secretPath, |
39 | | - secretConfig, |
40 | | - onDismiss, |
41 | | -}: CreateVersionDrawerProps) => { |
42 | | - const { t } = useTranslation(['secret-manager', NAMESPACES.ACTIONS]); |
43 | | - |
44 | | - const { |
45 | | - mutateAsync: createSecretVersion, |
46 | | - isPending: isCreating, |
47 | | - error: createError, |
48 | | - } = useCreateSecretVersion(); |
49 | | - |
50 | | - const dataSchema = useSecretDataSchema(); |
51 | | - const formSchema = z.object({ data: dataSchema }); |
52 | | - type FormSchema = z.infer<typeof formSchema>; |
53 | | - const { |
54 | | - control, |
55 | | - handleSubmit, |
56 | | - formState: { isDirty, isValid }, |
57 | | - } = useForm({ |
58 | | - mode: 'onChange', // Validation on all changes to show errors immediately |
59 | | - resolver: zodResolver(formSchema), |
60 | | - defaultValues: { |
61 | | - data: JSON.stringify(secret?.version?.data), |
62 | | - }, |
63 | | - }); |
64 | | - |
65 | | - const handleSubmitForm = async (data: FormSchema) => { |
66 | | - try { |
67 | | - await createSecretVersion({ |
68 | | - okmsId, |
69 | | - path: decodeSecretPath(secretPath), |
70 | | - data: JSON.parse(data.data), |
71 | | - cas: addCurrentVersionToCas( |
72 | | - secret?.metadata?.currentVersion, |
73 | | - secretConfig.casRequired.value, |
74 | | - ), |
75 | | - }); |
76 | | - onDismiss(); |
77 | | - } catch { |
78 | | - // Error is handled by the useCreateSecretVersion hook |
79 | | - } |
80 | | - }; |
81 | | - |
82 | | - return ( |
83 | | - <div className="flex flex-col h-full"> |
84 | | - <DrawerContent> |
85 | | - <form> |
86 | | - {createError && ( |
87 | | - <OdsMessage color="danger" className="mb-4"> |
88 | | - {createError?.response?.data?.message || |
89 | | - t('add_new_version_error')} |
90 | | - </OdsMessage> |
91 | | - )} |
92 | | - <VersionStatusMessage state={secret.version.state} /> |
93 | | - <SecretDataFormField name="data" control={control} /> |
94 | | - </form> |
95 | | - </DrawerContent> |
96 | | - <DrawerFooter |
97 | | - primaryButtonLabel={t(`${NAMESPACES.ACTIONS}:add`)} |
98 | | - isPrimaryButtonDisabled={!isDirty || !isValid} |
99 | | - isPrimaryButtonLoading={isCreating} |
100 | | - onPrimaryButtonClick={handleSubmit(handleSubmitForm)} |
101 | | - secondaryButtonLabel={t(`${NAMESPACES.ACTIONS}:close`)} |
102 | | - onSecondaryButtonClick={onDismiss} |
103 | | - /> |
104 | | - </div> |
105 | | - ); |
106 | | -}; |
107 | | - |
108 | 13 | export default function CreateVersionDrawerPage() { |
109 | 14 | const navigate = useNavigate(); |
110 | 15 | const { t } = useTranslation('secret-manager'); |
|
0 commit comments