Skip to content

Commit 95ae64d

Browse files
refactor(pci-instances): isolate ssh selector from react hook form
Signed-off-by: tsiorifamonjena <[email protected]>
1 parent 63ea005 commit 95ae64d

File tree

4 files changed

+57
-58
lines changed

4 files changed

+57
-58
lines changed

packages/manager/apps/pci-instances/src/pages/instances/create/CreateInstance.page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const instanceCreationSchema = z.object({
4040
distributionImageType: distributionImageTypeSchema,
4141
distributionImageName: distributionImageNameSchema,
4242
sshKeyId: sshKeyIdSchema,
43-
sshPublicKey: sshPublicKeySchema.nullable(),
43+
newSshPublicKey: sshPublicKeySchema.nullable(),
4444
});
4545

4646
const CreateInstance: FC = () => {

packages/manager/apps/pci-instances/src/pages/instances/create/components/SshKey.component.tsx

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useMemo, useState } from 'react';
1+
import { useEffect, useMemo, useState, useCallback } from 'react';
22
import { Button, Divider, Icon, Text } from '@ovhcloud/ods-react';
33
import { useTranslation } from 'react-i18next';
44
import { SshKeyHelper } from './sshKey/SshKeyHelper.component';
@@ -7,12 +7,13 @@ import { deps } from '@/deps/deps';
77
import { useProjectId } from '@/hooks/project/useProjectId';
88
import { selectSshKeys } from '../view-models/sshKeysViewModel';
99
import AddSshKey from './sshKey/AddSshKey.component';
10-
import { SubmitHandler } from 'react-hook-form';
10+
import { SubmitHandler, useFormContext, useWatch } from 'react-hook-form';
1111
import { TAddSshKeyForm } from '../CreateInstance.schema';
1212
import Banner from '@/components/banner/Banner.component';
1313
import SshKeySelector, {
1414
TCustomSelectSshKeyData,
1515
} from './sshKey/SshKeySelector.component';
16+
import { TInstanceCreationForm } from '../CreateInstance.page';
1617

1718
type TSshKeyProps = {
1819
microRegion: string;
@@ -21,9 +22,10 @@ type TSshKeyProps = {
2122
const SshKey = ({ microRegion }: TSshKeyProps) => {
2223
const projectId = useProjectId();
2324
const { t } = useTranslation('creation');
25+
2426
const [openSshKeyform, setOpenSshKeyForm] = useState<boolean>(false);
2527
const [newSshKeys, setNewSshKeys] = useState<TCustomSelectSshKeyData[]>([]);
26-
const [openSshKeyAddedMessage, setopenSshKeyAddedMessage] = useState<boolean>(
28+
const [openSshKeyAddedMessage, setOpenSshKeyAddedMessage] = useState<boolean>(
2729
false,
2830
);
2931

@@ -34,16 +36,31 @@ const SshKey = ({ microRegion }: TSshKeyProps) => {
3436
[isLoading, microRegion, projectId],
3537
);
3638

39+
const { control, setValue } = useFormContext<TInstanceCreationForm>();
40+
const selectedSshKeyId = useWatch({
41+
control,
42+
name: 'sshKeyId',
43+
});
44+
3745
const unavailableSshKeyIds = useMemo(
3846
() => [...newSshKeys, ...sshKeys].map(({ value }) => value),
3947
[newSshKeys, sshKeys],
4048
);
4149

50+
const sshKeyItems = useMemo(() => {
51+
const existingSshKeys = sshKeys.map((item) => ({
52+
...item,
53+
newSshPublicKey: null,
54+
}));
55+
56+
return [...newSshKeys, ...existingSshKeys];
57+
}, [newSshKeys, sshKeys]);
58+
4259
const handleOpenSshKeyForm = () => setOpenSshKeyForm(true);
4360

4461
const handleCloseSshKeyForm = () => setOpenSshKeyForm(false);
4562

46-
const handleCloseSshKeyAddedMessage = () => setopenSshKeyAddedMessage(false);
63+
const handleCloseSshKeyAddedMessage = () => setOpenSshKeyAddedMessage(false);
4764

4865
const handleAddSshKey: SubmitHandler<TAddSshKeyForm> = ({
4966
sshKeyId,
@@ -53,24 +70,43 @@ const SshKey = ({ microRegion }: TSshKeyProps) => {
5370
{
5471
label: sshKeyId,
5572
value: sshKeyId,
56-
sshPublicKey,
73+
newSshPublicKey: sshPublicKey,
5774
},
5875
...prevSshKeys,
5976
]);
6077

6178
handleCloseSshKeyForm();
62-
setopenSshKeyAddedMessage(true);
79+
setOpenSshKeyAddedMessage(true);
6380
};
6481

6582
const handleCancelAddSshKey = () => {
6683
if (sshKeys.length === 0 && newSshKeys.length === 0) return;
6784
handleCloseSshKeyForm();
6885
};
6986

87+
const updateSshKeyFields = useCallback(
88+
(sshKey: TCustomSelectSshKeyData) => {
89+
setValue('sshKeyId', sshKey.value);
90+
setValue('newSshPublicKey', sshKey.newSshPublicKey);
91+
},
92+
[setValue],
93+
);
94+
95+
const handleSelectSshKey = (sshKey: TCustomSelectSshKeyData) => {
96+
updateSshKeyFields(sshKey);
97+
handleCloseSshKeyAddedMessage();
98+
};
99+
70100
useEffect(() => {
71101
if (!isLoading && sshKeys.length === 0) setOpenSshKeyForm(true);
72102
}, [isLoading, sshKeys]);
73103

104+
useEffect(() => {
105+
const selectedSshKey = sshKeyItems[0];
106+
107+
if (selectedSshKey) updateSshKeyFields(selectedSshKey);
108+
}, [sshKeyItems, updateSshKeyFields]);
109+
74110
return (
75111
<section>
76112
<Divider spacing="64" />
@@ -114,9 +150,9 @@ const SshKey = ({ microRegion }: TSshKeyProps) => {
114150
) : (
115151
<>
116152
<SshKeySelector
117-
sshKeysItems={sshKeys}
118-
newSshKeys={newSshKeys}
119-
onValueChange={handleCloseSshKeyAddedMessage}
153+
items={sshKeyItems}
154+
value={selectedSshKeyId ? [selectedSshKeyId] : []}
155+
onValueChange={handleSelectSshKey}
120156
/>
121157
<Button variant="ghost" onClick={handleOpenSshKeyForm}>
122158
<Icon name="plus" />

packages/manager/apps/pci-instances/src/pages/instances/create/components/sshKey/SshKeySelector.component.tsx

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, useCallback, useEffect, useMemo } from 'react';
1+
import { FC } from 'react';
22
import {
33
FormField,
44
FormFieldLabel,
@@ -9,72 +9,35 @@ import {
99
} from '@ovhcloud/ods-react';
1010
import { useTranslation } from 'react-i18next';
1111
import { TSshKeyData } from '../../view-models/sshKeysViewModel';
12-
import { useFormContext, useWatch } from 'react-hook-form';
13-
import { TInstanceCreationForm } from '../../CreateInstance.page';
1412

1513
export type TCustomSelectSshKeyData = TSshKeyData & {
16-
sshPublicKey: string | null;
14+
newSshPublicKey: string | null;
1715
};
1816

1917
type TSshKeySelectorProps = {
20-
sshKeysItems: TSshKeyData[];
21-
newSshKeys: TCustomSelectSshKeyData[];
22-
onValueChange: () => void;
18+
items: TCustomSelectSshKeyData[];
19+
value: string[];
20+
onValueChange: (sshKey: TCustomSelectSshKeyData) => void;
2321
};
2422

2523
const SshKeySelector: FC<TSshKeySelectorProps> = ({
26-
sshKeysItems,
27-
newSshKeys,
24+
items,
25+
value,
2826
onValueChange,
2927
}) => {
3028
const { t } = useTranslation('creation');
31-
const { control, setValue } = useFormContext<TInstanceCreationForm>();
32-
const selectedSshKeyId = useWatch({
33-
control,
34-
name: 'sshKeyId',
35-
});
36-
37-
const updateSshKeyFields = useCallback(
38-
(sshKey: TCustomSelectSshKeyData) => {
39-
setValue('sshKeyId', sshKey.value);
40-
setValue('sshPublicKey', sshKey.sshPublicKey);
41-
},
42-
[setValue],
43-
);
4429

4530
const handleSelectSshKey = ({ items }: SelectValueChangeDetail) => {
4631
const selectedSshKey = (items as TCustomSelectSshKeyData[])[0];
47-
if (selectedSshKey) {
48-
updateSshKeyFields(selectedSshKey);
49-
onValueChange();
50-
}
32+
if (selectedSshKey) onValueChange(selectedSshKey);
5133
};
5234

53-
const items = useMemo(() => {
54-
const existingSshKeys = sshKeysItems.map((item) => ({
55-
...item,
56-
sshPublicKey: null,
57-
}));
58-
59-
return [...newSshKeys, ...existingSshKeys];
60-
}, [newSshKeys, sshKeysItems]);
61-
62-
useEffect(() => {
63-
const selectedSshKey = items[0];
64-
65-
if (selectedSshKey) updateSshKeyFields(selectedSshKey);
66-
}, [items, updateSshKeyFields]);
67-
6835
return (
6936
<FormField className="max-w-[32%] my-4">
7037
<FormFieldLabel>
7138
{t('creation:pci_instance_creation_select_sshKey_dropdown_label')}
7239
</FormFieldLabel>
73-
<Select
74-
items={items}
75-
value={selectedSshKeyId ? [selectedSshKeyId] : []}
76-
onValueChange={handleSelectSshKey}
77-
>
40+
<Select items={items} value={value} onValueChange={handleSelectSshKey}>
7841
<SelectControl />
7942
<SelectContent />
8043
</Select>

packages/manager/apps/pci-instances/src/pages/instances/create/hooks/useForm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const useForm = (projectId: string) => {
7171

7272
const sshKeyIdDefaultValue = null;
7373

74-
const sshPublicKeyDefaultValue = null;
74+
const newSshPublicKeyDefaultValue = null;
7575

7676
const formMethods = useReactHookForm({
7777
resolver: zodResolver(instanceCreationSchema),
@@ -89,7 +89,7 @@ export const useForm = (projectId: string) => {
8989
distributionImageType: distributionImageTypeDefaultValue,
9090
distributionImageName: distributionImageNameDefaultValue,
9191
sshKeyId: sshKeyIdDefaultValue,
92-
sshPublicKey: sshPublicKeyDefaultValue,
92+
newSshPublicKey: newSshPublicKeyDefaultValue,
9393
},
9494
mode: 'onChange',
9595
});

0 commit comments

Comments
 (0)