Skip to content

Commit 86b504f

Browse files
authored
Feat: refactor IPPREFIX table #2143 (#1604)
* Feat: refactor IPPREFIX table * Create fast-starfishes-design.md * remove unused imports * return state for state management * prefix check lir/sub * update after review
1 parent 7634407 commit 86b504f

File tree

2 files changed

+26
-50
lines changed

2 files changed

+26
-50
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@orchestrator-ui/orchestrator-ui-components": patch
3+
---
4+
5+
Feat: refactor IPPREFIX table #2143

packages/orchestrator-ui-components/src/components/WfoForms/formFields/deprecated/IpNetworkField.tsx

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,29 @@
1414
*/
1515
import React, { useState } from 'react';
1616

17-
import { useTranslations } from 'next-intl';
1817
import { connectField, filterDOMProps } from 'uniforms';
1918

20-
import { EuiCallOut, EuiFormRow, EuiText } from '@elastic/eui';
19+
import { EuiFormRow, EuiText } from '@elastic/eui';
2120

2221
import { getCommonFormFieldStyles } from '@/components/WfoForms/formFields/commonStyles';
2322
import SplitPrefix from '@/components/WfoForms/formFields/deprecated/SplitPrefix';
2423
import { useWithOrchestratorTheme } from '@/hooks';
2524

2625
import { FieldProps } from '../types';
27-
import IpPrefixTableField from './IpPrefixTableField';
28-
import { IpBlock } from './types';
26+
27+
export function getUsedPrefixMin(
28+
netmask: string,
29+
prefixMin: number | undefined,
30+
name: string,
31+
) {
32+
const netMaskInt = parseInt(netmask, 10);
33+
return (
34+
prefixMin ??
35+
(netMaskInt < 32 && name === 'ip_sub_prefix'
36+
? netMaskInt + 1
37+
: netMaskInt)
38+
);
39+
}
2940

3041
export type IPvAnyNetworkFieldProps = FieldProps<
3142
string,
@@ -48,17 +59,13 @@ function IpNetwork({
4859
}: IPvAnyNetworkFieldProps) {
4960
const { formRowStyle } = useWithOrchestratorTheme(getCommonFormFieldStyles);
5061

51-
const t = useTranslations('pydanticForms');
52-
const [selectedPrefix, setSelectedPrefix] = useState<IpBlock | undefined>(
53-
undefined,
54-
);
55-
const [manualOverride, setManualOverride] = useState(false);
62+
// The state is needed in order to keep the selected prefix in the SplitPrefix component
63+
const [selectedPrefix] = useState<string | undefined>(value);
5664

57-
const usePrefix = selectedPrefix?.prefix ?? value;
65+
const usePrefix = selectedPrefix;
5866
const [subnet, netmask] = usePrefix?.split('/') ?? ['', ''];
59-
const usedPrefixMin =
60-
prefixMin ??
61-
parseInt(netmask, 10) + (selectedPrefix?.state === 0 ? 0 : 1);
67+
68+
const usedPrefixMin = getUsedPrefixMin(netmask, prefixMin, name);
6269

6370
return (
6471
<section {...filterDOMProps(props)}>
@@ -73,32 +80,7 @@ function IpNetwork({
7380
>
7481
<section className="ipblock-selector">
7582
<div id={id}>
76-
{!prefixMin && (
77-
<IpPrefixTableField
78-
id={id}
79-
name={name}
80-
onChange={(prefix: IpBlock) => {
81-
if (!readOnly) {
82-
if (
83-
prefix.state === 0 ||
84-
prefix.state === 1
85-
) {
86-
setSelectedPrefix(prefix);
87-
}
88-
setManualOverride(false);
89-
onChange(prefix.prefix);
90-
}
91-
}}
92-
onManualOverride={(prefixString: string) => {
93-
if (!readOnly) {
94-
setManualOverride(true);
95-
onChange(prefixString);
96-
}
97-
}}
98-
selected_prefix_id={selectedPrefix?.id}
99-
/>
100-
)}
101-
{usePrefix && !manualOverride && (
83+
{usePrefix && (
10284
<SplitPrefix
10385
id={id}
10486
name={name}
@@ -113,17 +95,6 @@ function IpNetwork({
11395
selectedSubnet={usePrefix}
11496
/>
11597
)}
116-
{usePrefix && manualOverride && (
117-
<EuiCallOut
118-
title={t(
119-
'widgets.ipvAnyNetworkField.manuallySelectedPrefix',
120-
)}
121-
color="primary"
122-
iconType="check"
123-
>
124-
<p>{value}</p>
125-
</EuiCallOut>
126-
)}
12798
</div>
12899
</section>
129100
</EuiFormRow>

0 commit comments

Comments
 (0)