Skip to content

Commit f25c8a4

Browse files
fix(pci-instances): move hook usage into useFlavorCommon
Signed-off-by: Lauren Olivier <[email protected]>
1 parent 872a822 commit f25c8a4

File tree

5 files changed

+71
-36
lines changed

5 files changed

+71
-36
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const ContinentSelection = () => {
5050
</FormFieldLabel>
5151
<Select
5252
items={continentOptions}
53-
value={[field.value]}
53+
value={field.value ? [...field.value] : []}
5454
onValueChange={handleContinentChange}
5555
>
5656
<SelectControl />

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ export const DeploymentModeSelection = () => {
3636
field: ControllerRenderProps<TDeploymentModeSelection, 'deploymentModes'>,
3737
selectedMode: TDeploymentMode,
3838
) => () => {
39-
const isSelected = field.value.includes(selectedMode);
39+
const values = field.value ?? [];
40+
const isSelected = values.includes(selectedMode);
4041

4142
const selection = isSelected
42-
? field.value.filter((value) => value !== selectedMode)
43-
: [...field.value, selectedMode];
43+
? values.filter((value) => value !== selectedMode)
44+
: [...values, selectedMode];
4445

4546
field.onChange(selection);
4647

@@ -69,15 +70,15 @@ export const DeploymentModeSelection = () => {
6970
{deploymentModes.map(({ mode, title, description, Image }) => (
7071
<PciCard
7172
selectable
72-
selected={field.value.includes(mode)}
73+
selected={(field.value ?? []).includes(mode)}
7374
className="h-full"
7475
key={mode}
7576
onClick={handleSelect(field, mode)}
7677
>
7778
<PciCard.Header>
7879
<Checkbox
7980
className="w-full"
80-
checked={field.value.includes(mode)}
81+
checked={(field.value ?? []).includes(mode)}
8182
>
8283
<CheckboxControl />
8384
<CheckboxLabel className="font-bold text-lg text-[--ods-color-heading]">

packages/manager/apps/pci-instances/src/pages/instances/create/components/flavor/FlavorRowUtils.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import {
1313
} from '@ovhcloud/ods-react';
1414
import { useTranslation } from 'react-i18next';
1515
import { ReactNode } from 'react';
16+
import { useCatalogPrice } from '@ovh-ux/manager-react-components';
1617

1718
export function useFlavorCommon() {
1819
const { t } = useTranslation('creation');
20+
const { getTextPrice } = useCatalogPrice(4);
1921

2022
const renderName = (flavor: {
2123
name: string;
@@ -68,5 +70,31 @@ export function useFlavorCommon() {
6870
</Radio>
6971
);
7072

71-
return { renderName, renderRadio };
73+
const renderHourlyPrice = (value: number | null) => {
74+
const minimumHourlyPrice = value !== null ? getTextPrice(value) : '-';
75+
return (
76+
<Text preset={TEXT_PRESET.span} className="font-semibold">
77+
{minimumHourlyPrice}
78+
</Text>
79+
);
80+
};
81+
82+
const renderMonthlyPrice = (
83+
realMinimumMonthlyPrice: number | null,
84+
estimatedMinimumMonthlyPrice: number | null,
85+
) => {
86+
const minimumMonthlyPrice =
87+
realMinimumMonthlyPrice !== null
88+
? getTextPrice(realMinimumMonthlyPrice)
89+
: estimatedMinimumMonthlyPrice !== null
90+
? `~ ${getTextPrice(estimatedMinimumMonthlyPrice)}`
91+
: '-';
92+
return (
93+
<Text preset={TEXT_PRESET.span} className="font-semibold">
94+
{minimumMonthlyPrice}
95+
</Text>
96+
);
97+
};
98+
99+
return { renderName, renderRadio, renderHourlyPrice, renderMonthlyPrice };
72100
}

packages/manager/apps/pci-instances/src/pages/instances/create/components/flavor/FlavorRowsBuilder.tsx

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,33 @@ import { TableRow } from '@/components/flavorsTable/FlavorsTable.component';
33
import { DeploymentModeBadge } from '@/components/deploymentModeBadge/DeploymentModeBadge.component';
44
import { TFlavorDataForTable } from '@/pages/instances/create/view-models/flavorsViewModel';
55
import { ReactNode } from 'react';
6-
import { useCatalogPrice } from '@ovh-ux/manager-react-components';
76

87
type FlavorRenderers = {
98
renderName: (flavor: TFlavorDataForTable) => ReactNode;
109
renderRadio: (id: string, disabled?: boolean) => ReactNode;
10+
renderHourlyPrice: (value: number | null) => ReactNode;
11+
renderMonthlyPrice: (
12+
realMinimumMonthlyPrice: number | null,
13+
estimatedMinimumMonthlyPrice: number | null,
14+
) => ReactNode;
1115
};
1216

13-
export function FlavorRowsBuilder(
17+
export const FlavorRowsBuilder = (
1418
flavors: TFlavorDataForTable[],
15-
{ renderName, renderRadio }: FlavorRenderers,
19+
{
20+
renderName,
21+
renderRadio,
22+
renderHourlyPrice,
23+
renderMonthlyPrice,
24+
}: FlavorRenderers,
1625
withUnavailable: boolean,
17-
): TableRow[] {
18-
const { getTextPrice } = useCatalogPrice(4);
19-
26+
): TableRow[] => {
2027
return flavors
2128
.filter(
2229
({ unavailable, unavailableQuota }) =>
2330
withUnavailable || !(unavailable || unavailableQuota),
2431
)
2532
.map((flavor) => {
26-
const minimumHourlyPrice = flavor.realMinimumHourlyPrice
27-
? getTextPrice(flavor.realMinimumHourlyPrice)
28-
: '-';
29-
30-
const minimumMonthlyPrice = flavor.realMinimumMonthlyPrice
31-
? getTextPrice(flavor.realMinimumMonthlyPrice)
32-
: flavor.estimatedMinimumMonthlyPrice
33-
? `~ ${getTextPrice(flavor.estimatedMinimumMonthlyPrice)}`
34-
: '-';
35-
3633
return {
3734
id: flavor.id,
3835
disabled: flavor.unavailableQuota,
@@ -48,16 +45,11 @@ export function FlavorRowsBuilder(
4845
-
4946
</Text>
5047
),
51-
hourlyPrice: (
52-
<Text preset={TEXT_PRESET.span} className="font-semibold">
53-
{minimumHourlyPrice}
54-
</Text>
55-
),
56-
monthlyPrice: (
57-
<Text preset={TEXT_PRESET.span} className="font-semibold">
58-
{minimumMonthlyPrice}
59-
</Text>
48+
hourlyPrice: renderHourlyPrice(flavor.realMinimumHourlyPrice),
49+
monthlyPrice: renderMonthlyPrice(
50+
flavor.realMinimumMonthlyPrice,
51+
flavor.estimatedMinimumMonthlyPrice,
6052
),
6153
};
6254
});
63-
}
55+
};

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ export const FlavorSelection: FC<{ withUnavailable: boolean }> = ({
4343
name: ['flavorCategory', 'flavorType', 'microRegion', 'flavorId'],
4444
});
4545

46-
const { renderName, renderRadio } = useFlavorCommon();
46+
const {
47+
renderName,
48+
renderRadio,
49+
renderHourlyPrice,
50+
renderMonthlyPrice,
51+
} = useFlavorCommon();
4752
const [unavailableFlavor, setUnavailableFlavor] = useState<string | null>(
4853
null,
4954
);
@@ -78,11 +83,20 @@ export const FlavorSelection: FC<{ withUnavailable: boolean }> = ({
7883
columns: FlavorColumnsBuilder(t),
7984
rows: FlavorRowsBuilder(
8085
flavors,
81-
{ renderName, renderRadio },
86+
{ renderName, renderRadio, renderHourlyPrice, renderMonthlyPrice },
8287
withUnavailable,
8388
),
8489
};
85-
}, [flavorCategory, flavors, renderName, renderRadio, t, withUnavailable]);
90+
}, [
91+
flavorCategory,
92+
flavors,
93+
renderName,
94+
renderRadio,
95+
renderHourlyPrice,
96+
renderMonthlyPrice,
97+
t,
98+
withUnavailable,
99+
]);
86100

87101
const availableRegions = useMemo(
88102
() =>

0 commit comments

Comments
 (0)