fix(striker-ui): get form data, add confirm in prepare network

main
Tsu-ba-me 1 year ago
parent aa5fa7d3e6
commit 823c293012
  1. 211
      striker-ui/components/PrepareNetworkForm.tsx

@ -1,20 +1,93 @@
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { FC, useCallback, useEffect, useMemo } from 'react'; import { FC, useCallback, useEffect, useMemo, useRef } from 'react';
import api from '../lib/api'; import api from '../lib/api';
import ConfirmDialog from './ConfirmDialog';
import ContainedButton from './ContainedButton'; import ContainedButton from './ContainedButton';
import handleAPIError from '../lib/handleAPIError';
import FlexBox from './FlexBox'; import FlexBox from './FlexBox';
import FormSummary from './FormSummary';
import getQueryParam from '../lib/getQueryParam'; import getQueryParam from '../lib/getQueryParam';
import handleAPIError from '../lib/handleAPIError';
import InputWithRef from './InputWithRef'; import InputWithRef from './InputWithRef';
import MessageBox, { Message } from './MessageBox'; import MessageBox, { Message } from './MessageBox';
import NetworkInitForm from './NetworkInitForm'; import MessageGroup, { MessageGroupForwardedRefContent } from './MessageGroup';
import NetworkInitForm, {
NetworkInitFormForwardedRefContent,
} from './NetworkInitForm';
import OutlinedInputWithLabel from './OutlinedInputWithLabel'; import OutlinedInputWithLabel from './OutlinedInputWithLabel';
import { Panel, PanelHeader } from './Panels'; import { Panel, PanelHeader } from './Panels';
import Spinner from './Spinner'; import Spinner from './Spinner';
import { buildPeacefulStringTestBatch } from '../lib/test_input';
import { HeaderText } from './Text'; import { HeaderText } from './Text';
import useConfirmDialogProps from '../hooks/useConfirmDialogProps';
import useFormUtils from '../hooks/useFormUtils';
import useProtectedState from '../hooks/useProtectedState'; import useProtectedState from '../hooks/useProtectedState';
const INPUT_ID_PREP_NET_HOST_NAME = 'prepare-network-host-name-input';
const INPUT_GROUP_ID_PREP_NET_NETCONF = 'prepare-network-netconf-input-group';
const INPUT_LABEL_PREP_NET_HOST_NAME = 'Host name';
const getFormData = (
{
netconf,
}: {
netconf: NetworkInitFormForwardedRefContent;
},
...[{ target }]: DivFormEventHandlerParameters
) => {
const { elements } = target as HTMLFormElement;
const { value: hostName } = elements.namedItem(
INPUT_ID_PREP_NET_HOST_NAME,
) as HTMLInputElement;
const data = {
hostName,
...netconf.get?.call(null),
};
return data;
};
const toFormEntries = (body: ReturnType<typeof getFormData>): FormEntries => {
const { networks: nets = [], ...rest } = body;
const networks = nets.reduce<FormEntries>((previous, network) => {
const {
interfaces: ifaces,
ipAddress,
name = '',
type,
typeCount,
subnetMask,
} = network;
const networkId = `${type}${typeCount}`;
const interfaces = ifaces.reduce<FormEntries>((pIfaces, iface, index) => {
if (iface) {
const { networkInterfaceName } = iface;
const linkNumber = index + 1;
pIfaces[`link${linkNumber}`] = networkInterfaceName;
}
return pIfaces;
}, {});
previous[networkId] = {
name,
network: `${ipAddress}/${subnetMask}`,
...interfaces,
};
return previous;
}, {});
return { ...rest, networks };
};
const PrepareNetworkForm: FC<PrepareNetworkFormProps> = ({ const PrepareNetworkForm: FC<PrepareNetworkFormProps> = ({
expectUUID: isExpectExternalHostUUID = false, expectUUID: isExpectExternalHostUUID = false,
hostUUID, hostUUID,
@ -24,6 +97,15 @@ const PrepareNetworkForm: FC<PrepareNetworkFormProps> = ({
query: { host_uuid: queryHostUUID }, query: { host_uuid: queryHostUUID },
} = useRouter(); } = useRouter();
const confirmDialogRef = useRef<ConfirmDialogForwardedRefContent>({});
const netconfFormRef = useRef<NetworkInitFormForwardedRefContent>({});
const generalInputMessageGroupRef = useRef<MessageGroupForwardedRefContent>(
{},
);
const [confirmDialogProps, setConfirmDialogProps] = useConfirmDialogProps();
const [hostDetail, setHostDetail] = useProtectedState< const [hostDetail, setHostDetail] = useProtectedState<
APIHostDetail | undefined APIHostDetail | undefined
>(undefined); >(undefined);
@ -35,6 +117,17 @@ const PrepareNetworkForm: FC<PrepareNetworkFormProps> = ({
const [previousHostUUID, setPreviousHostUUID] = const [previousHostUUID, setPreviousHostUUID] =
useProtectedState<PrepareNetworkFormProps['hostUUID']>(undefined); useProtectedState<PrepareNetworkFormProps['hostUUID']>(undefined);
const {
buildFinishInputTestBatchFunction,
buildInputFirstRenderFunction,
isFormInvalid,
setMessage,
setValidity,
} = useFormUtils(
[INPUT_ID_PREP_NET_HOST_NAME, INPUT_GROUP_ID_PREP_NET_NETCONF],
generalInputMessageGroupRef,
);
const isDifferentHostUUID = useMemo( const isDifferentHostUUID = useMemo(
() => hostUUID !== previousHostUUID, () => hostUUID !== previousHostUUID,
[hostUUID, previousHostUUID], [hostUUID, previousHostUUID],
@ -52,6 +145,30 @@ const PrepareNetworkForm: FC<PrepareNetworkFormProps> = ({
), ),
[hostDetail], [hostDetail],
); );
const netconfForm = useMemo(
() => (
<NetworkInitForm
expectHostDetail
hostDetail={hostDetail}
ref={netconfFormRef}
toggleSubmitDisabled={(valid) => {
setValidity(INPUT_GROUP_ID_PREP_NET_NETCONF, valid);
}}
/>
),
[hostDetail, setValidity],
);
const generalInputMessageArea = useMemo(
() => (
<MessageGroup
count={1}
defaultMessageType="warning"
ref={generalInputMessageGroupRef}
/>
),
[],
);
const contentElement = useMemo(() => { const contentElement = useMemo(() => {
let result; let result;
@ -63,21 +180,72 @@ const PrepareNetworkForm: FC<PrepareNetworkFormProps> = ({
result = ( result = (
<> <>
{panelHeaderElement} {panelHeaderElement}
<FlexBox> <FlexBox
component="form"
onSubmit={(...args) => {
const [event] = args;
event.preventDefault();
const body = getFormData(
{ netconf: netconfFormRef.current },
...args,
);
setConfirmDialogProps({
actionProceedText: 'Prepare',
content: (
<FormSummary
entries={toFormEntries(body)}
getEntryLabel={({ cap, key }) =>
/^(dns|[a-z]+n\d+)/.test(key)
? key.toUpperCase()
: cap(key)
}
/>
),
titleText: `Prepare ${hostDetail?.shortHostName} network?`,
});
confirmDialogRef.current.setOpen?.call(null, true);
}}
>
<InputWithRef <InputWithRef
input={ input={
<OutlinedInputWithLabel <OutlinedInputWithLabel
formControlProps={{ sx: { maxWidth: '20em' } }} formControlProps={{ sx: { maxWidth: '20em' } }}
id="prepare-network-host-name" id={INPUT_ID_PREP_NET_HOST_NAME}
label="Host name" label={INPUT_LABEL_PREP_NET_HOST_NAME}
value={hostDetail?.hostName} value={hostDetail?.hostName}
/> />
} }
inputTestBatch={buildPeacefulStringTestBatch(
INPUT_LABEL_PREP_NET_HOST_NAME,
() => {
setMessage(INPUT_ID_PREP_NET_HOST_NAME);
},
{
onFinishBatch: buildFinishInputTestBatchFunction(
INPUT_ID_PREP_NET_HOST_NAME,
),
},
(message) => {
setMessage(INPUT_ID_PREP_NET_HOST_NAME, {
children: message,
});
},
)}
onFirstRender={buildInputFirstRenderFunction(
INPUT_ID_PREP_NET_HOST_NAME,
)}
required required
/> />
<NetworkInitForm expectHostDetail hostDetail={hostDetail} /> {generalInputMessageArea}
{netconfForm}
<FlexBox row justifyContent="flex-end"> <FlexBox row justifyContent="flex-end">
<ContainedButton>Prepare network</ContainedButton> <ContainedButton disabled={isFormInvalid} type="submit">
Prepare network
</ContainedButton>
</FlexBox> </FlexBox>
</FlexBox> </FlexBox>
</> </>
@ -85,7 +253,20 @@ const PrepareNetworkForm: FC<PrepareNetworkFormProps> = ({
} }
return result; return result;
}, [hostDetail, fatalErrorMessage, isLoadingHostDetail, panelHeaderElement]); }, [
isLoadingHostDetail,
fatalErrorMessage,
panelHeaderElement,
hostDetail?.hostName,
hostDetail?.shortHostName,
buildFinishInputTestBatchFunction,
buildInputFirstRenderFunction,
generalInputMessageArea,
netconfForm,
isFormInvalid,
setConfirmDialogProps,
setMessage,
]);
const getHostDetail = useCallback( const getHostDetail = useCallback(
(uuid: string) => { (uuid: string) => {
@ -150,7 +331,17 @@ const PrepareNetworkForm: FC<PrepareNetworkFormProps> = ({
isReloadHostDetail, isReloadHostDetail,
]); ]);
return <Panel>{contentElement}</Panel>; return (
<>
<Panel>{contentElement}</Panel>
<ConfirmDialog
closeOnProceed
scrollContent
{...confirmDialogProps}
ref={confirmDialogRef}
/>
</>
);
}; };
export default PrepareNetworkForm; export default PrepareNetworkForm;

Loading…
Cancel
Save