Skip to content

Commit

Permalink
Feat: refactor IPPREFIX table
Browse files Browse the repository at this point in the history
  • Loading branch information
Igoranze committed Nov 28, 2024
1 parent 112170c commit 5acbbb8
Showing 1 changed file with 10 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@
*/
import React, { useState } from 'react';

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

import { EuiCallOut, EuiFormRow, EuiText } from '@elastic/eui';
import { EuiFormRow, EuiText } from '@elastic/eui';

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

import { FieldProps } from '../types';
import IpPrefixTableField from './IpPrefixTableField';
import { IpBlock } from './types';

export type IPvAnyNetworkFieldProps = FieldProps<
string,
Expand All @@ -48,17 +45,16 @@ function IpNetwork({
}: IPvAnyNetworkFieldProps) {
const { formRowStyle } = useWithOrchestratorTheme(getCommonFormFieldStyles);

const t = useTranslations('pydanticForms');
const [selectedPrefix, setSelectedPrefix] = useState<IpBlock | undefined>(
undefined,
);
const [manualOverride, setManualOverride] = useState(false);
const [selectedPrefix] = useState<string | undefined>(value);

const usePrefix = selectedPrefix?.prefix ?? value;
const usePrefix = selectedPrefix;
const [subnet, netmask] = usePrefix?.split('/') ?? ['', ''];
const usedPrefixMin =
prefixMin ??
parseInt(netmask, 10) + (selectedPrefix?.state === 0 ? 0 : 1);

let usedPrefixMin = prefixMin ?? parseInt(netmask, 10);

if (usedPrefixMin < 32) {
usedPrefixMin += 1;
}

return (
<section {...filterDOMProps(props)}>
Expand All @@ -73,32 +69,7 @@ function IpNetwork({
>
<section className="ipblock-selector">
<div id={id}>
{!prefixMin && (
<IpPrefixTableField
id={id}
name={name}
onChange={(prefix: IpBlock) => {
if (!readOnly) {
if (
prefix.state === 0 ||
prefix.state === 1
) {
setSelectedPrefix(prefix);
}
setManualOverride(false);
onChange(prefix.prefix);
}
}}
onManualOverride={(prefixString: string) => {
if (!readOnly) {
setManualOverride(true);
onChange(prefixString);
}
}}
selected_prefix_id={selectedPrefix?.id}
/>
)}
{usePrefix && !manualOverride && (
{usePrefix && (
<SplitPrefix
id={id}
name={name}
Expand All @@ -113,17 +84,6 @@ function IpNetwork({
selectedSubnet={usePrefix}
/>
)}
{usePrefix && manualOverride && (
<EuiCallOut
title={t(
'widgets.ipvAnyNetworkField.manuallySelectedPrefix',
)}
color="primary"
iconType="check"
>
<p>{value}</p>
</EuiCallOut>
)}
</div>
</section>
</EuiFormRow>
Expand Down

0 comments on commit 5acbbb8

Please sign in to comment.