fix(striker-ui): correct add, update, delete submission for UPS(es)

main
Tsu-ba-me 2 years ago
parent 830e07d30c
commit 7414553dae
  1. 159
      striker-ui/components/ManageUps/ManageUpsPanel.tsx

@ -23,65 +23,73 @@ import { Panel, PanelHeader } from '../Panels';
import periodicFetch from '../../lib/fetchers/periodicFetch'; import periodicFetch from '../../lib/fetchers/periodicFetch';
import Spinner from '../Spinner'; import Spinner from '../Spinner';
import { BodyText, HeaderText, InlineMonoText, MonoText } from '../Text'; import { BodyText, HeaderText, InlineMonoText, MonoText } from '../Text';
import useChecklist from '../../hooks/useChecklist';
import useConfirmDialogProps from '../../hooks/useConfirmDialogProps'; import useConfirmDialogProps from '../../hooks/useConfirmDialogProps';
import useFormUtils from '../../hooks/useFormUtils'; import useFormUtils from '../../hooks/useFormUtils';
import useIsFirstRender from '../../hooks/useIsFirstRender'; import useIsFirstRender from '../../hooks/useIsFirstRender';
import useProtectedState from '../../hooks/useProtectedState'; import useProtectedState from '../../hooks/useProtectedState';
type UpsFormData = { type UpsFormData = {
upsAgent: string; agent: string;
upsBrand: string; brand: string;
upsIPAddress: string; ipAddress: string;
upsName: string; name: string;
upsTypeId: string; typeId: string;
upsUUID: string; uuid: string;
}; };
const getUpsFormData = ( const getFormData = (
upsTemplate: APIUpsTemplate, upsTemplate: APIUpsTemplate,
...[{ target }]: Parameters<FormEventHandler<HTMLDivElement>> ...[{ target }]: Parameters<FormEventHandler<HTMLDivElement>>
): UpsFormData => { ): UpsFormData => {
const { elements } = target as HTMLFormElement; const { elements } = target as HTMLFormElement;
const { value: upsName } = elements.namedItem( const { value: name } = elements.namedItem(
INPUT_ID_UPS_NAME, INPUT_ID_UPS_NAME,
) as HTMLInputElement; ) as HTMLInputElement;
const { value: upsIPAddress } = elements.namedItem( const { value: ipAddress } = elements.namedItem(
INPUT_ID_UPS_IP, INPUT_ID_UPS_IP,
) as HTMLInputElement; ) as HTMLInputElement;
const inputUpsTypeId = elements.namedItem(INPUT_ID_UPS_TYPE); const inputUpsTypeId = elements.namedItem(INPUT_ID_UPS_TYPE);
let upsAgent = ''; let agent = '';
let upsBrand = ''; let brand = '';
let upsTypeId = ''; let typeId = '';
if (inputUpsTypeId) { if (inputUpsTypeId) {
({ value: upsTypeId } = inputUpsTypeId as HTMLInputElement); ({ value: typeId } = inputUpsTypeId as HTMLInputElement);
({ agent: upsAgent, brand: upsBrand } = upsTemplate[upsTypeId]); ({ agent, brand } = upsTemplate[typeId]);
} }
const inputUpsUUID = elements.namedItem(INPUT_ID_UPS_UUID); const inputUpsUUID = elements.namedItem(INPUT_ID_UPS_UUID);
let upsUUID = ''; let uuid = '';
if (inputUpsUUID) { if (inputUpsUUID) {
({ value: upsUUID } = inputUpsUUID as HTMLInputElement); ({ value: uuid } = inputUpsUUID as HTMLInputElement);
} }
return { upsAgent, upsBrand, upsIPAddress, upsName, upsTypeId, upsUUID }; return {
agent,
brand,
ipAddress,
name,
typeId,
uuid,
};
}; };
const buildConfirmUpsFormData = ({ const buildConfirmUpsFormData = ({
upsBrand, brand,
upsIPAddress, ipAddress,
upsName, name,
upsUUID, uuid,
}: UpsFormData) => { }: UpsFormData) => {
const listItems: Record<string, { label: string; value: string }> = { const listItems: Record<string, { label: string; value: string }> = {
'ups-brand': { label: 'Brand', value: upsBrand }, 'ups-brand': { label: 'Brand', value: brand },
'ups-name': { label: 'Host name', value: upsName }, 'ups-name': { label: 'Host name', value: name },
'ups-ip-address': { label: 'IP address', value: upsIPAddress }, 'ups-ip-address': { label: 'IP address', value: ipAddress },
}; };
return ( return (
@ -89,7 +97,7 @@ const buildConfirmUpsFormData = ({
listItems={listItems} listItems={listItems}
listItemProps={{ sx: { padding: 0 } }} listItemProps={{ sx: { padding: 0 } }}
renderListItem={(part, { label, value }) => ( renderListItem={(part, { label, value }) => (
<FlexBox fullWidth growFirst key={`confirm-ups-${upsUUID}-${part}`} row> <FlexBox fullWidth growFirst key={`confirm-ups-${uuid}-${part}`} row>
<BodyText>{label}</BodyText> <BodyText>{label}</BodyText>
<MonoText>{value}</MonoText> <MonoText>{value}</MonoText>
</FlexBox> </FlexBox>
@ -123,7 +131,12 @@ const ManageUpsPanel: FC = () => {
[INPUT_ID_UPS_IP, INPUT_ID_UPS_NAME, INPUT_ID_UPS_TYPE], [INPUT_ID_UPS_IP, INPUT_ID_UPS_NAME, INPUT_ID_UPS_TYPE],
messageGroupRef, messageGroupRef,
); );
const { isFormInvalid } = formUtils; const { isFormInvalid, isFormSubmitting, submitForm } = formUtils;
const { buildDeleteDialogProps, checks, getCheck, hasChecks, setCheck } =
useChecklist({
list: upsOverviews,
});
const buildEditUpsFormDialogProps = useCallback< const buildEditUpsFormDialogProps = useCallback<
(args: APIUpsOverview[string]) => ConfirmDialogProps (args: APIUpsOverview[string]) => ConfirmDialogProps
@ -155,12 +168,23 @@ const ManageUpsPanel: FC = () => {
return; return;
} }
const editData = getUpsFormData(upsTemplate, event); const editData = getFormData(upsTemplate, event);
const { upsName: newUpsName } = editData; const { name: newUpsName } = editData;
setConfirmDialogProps({ setConfirmDialogProps({
actionProceedText: 'Update', actionProceedText: 'Update',
content: buildConfirmUpsFormData(editData), content: buildConfirmUpsFormData(editData),
onProceedAppend: () => {
submitForm({
body: editData,
getErrorMsg: (parentMsg) => (
<>Failed to update UPS. {parentMsg}</>
),
method: 'put',
successMsg: `Successfully updated UPS ${upsName}`,
url: `/ups/${upsUUID}`,
});
},
titleText: ( titleText: (
<HeaderText> <HeaderText>
Update{' '} Update{' '}
@ -180,7 +204,7 @@ const ManageUpsPanel: FC = () => {
), ),
}; };
}, },
[formUtils, setConfirmDialogProps, upsTemplate], [formUtils, setConfirmDialogProps, submitForm, upsTemplate],
); );
const addUpsFormDialogProps = useMemo<ConfirmDialogProps>( const addUpsFormDialogProps = useMemo<ConfirmDialogProps>(
@ -194,12 +218,21 @@ const ManageUpsPanel: FC = () => {
return; return;
} }
const addData = getUpsFormData(upsTemplate, event); const addData = getFormData(upsTemplate, event);
const { upsBrand } = addData; const { brand: upsBrand, name: upsName } = addData;
setConfirmDialogProps({ setConfirmDialogProps({
actionProceedText: 'Add', actionProceedText: 'Add',
content: buildConfirmUpsFormData(addData), content: buildConfirmUpsFormData(addData),
onProceedAppend: () => {
submitForm({
body: addData,
getErrorMsg: (parentMsg) => <>Failed to add UPS. {parentMsg}</>,
method: 'post',
successMsg: `Successfully added UPS ${upsName}`,
url: '/ups',
});
},
titleText: ( titleText: (
<HeaderText> <HeaderText>
Add a{' '} Add a{' '}
@ -213,7 +246,7 @@ const ManageUpsPanel: FC = () => {
}, },
titleText: 'Add a UPS', titleText: 'Add a UPS',
}), }),
[formUtils, setConfirmDialogProps, upsTemplate], [formUtils, setConfirmDialogProps, submitForm, upsTemplate],
); );
const listElement = useMemo( const listElement = useMemo(
@ -221,6 +254,7 @@ const ManageUpsPanel: FC = () => {
<List <List
allowEdit allowEdit
allowItemButton={isEditUpses} allowItemButton={isEditUpses}
disableDelete={!hasChecks}
edit={isEditUpses} edit={isEditUpses}
header header
listEmpty="No Ups(es) registered." listEmpty="No Ups(es) registered."
@ -229,13 +263,39 @@ const ManageUpsPanel: FC = () => {
setFormDialogProps(addUpsFormDialogProps); setFormDialogProps(addUpsFormDialogProps);
formDialogRef.current.setOpen?.call(null, true); formDialogRef.current.setOpen?.call(null, true);
}} }}
onDelete={() => {
setConfirmDialogProps(
buildDeleteDialogProps({
getConfirmDialogTitle: (count) => `Delete ${count} UPSes?`,
onProceedAppend: () => {
submitForm({
body: { uuids: checks },
getErrorMsg: (parentMsg) => (
<>Failed to delete UPS(es). {parentMsg}</>
),
method: 'delete',
url: '/ups',
});
},
renderEntry: (key) => (
<BodyText>{upsOverviews?.[key].upsName}</BodyText>
),
}),
);
confirmDialogRef.current.setOpen?.call(null, true);
}}
onEdit={() => { onEdit={() => {
setIsEditUpses((previous) => !previous); setIsEditUpses((previous) => !previous);
}} }}
onItemCheckboxChange={(key, event, checked) => {
setCheck(key, checked);
}}
onItemClick={(value) => { onItemClick={(value) => {
setFormDialogProps(buildEditUpsFormDialogProps(value)); setFormDialogProps(buildEditUpsFormDialogProps(value));
formDialogRef.current.setOpen?.call(null, true); formDialogRef.current.setOpen?.call(null, true);
}} }}
renderListItemCheckboxState={(key) => getCheck(key)}
renderListItem={(upsUUID, { upsAgent, upsIPAddress, upsName }) => ( renderListItem={(upsUUID, { upsAgent, upsIPAddress, upsName }) => (
<FlexBox fullWidth row> <FlexBox fullWidth row>
<BodyText>{upsName}</BodyText> <BodyText>{upsName}</BodyText>
@ -247,9 +307,16 @@ const ManageUpsPanel: FC = () => {
), ),
[ [
addUpsFormDialogProps, addUpsFormDialogProps,
buildDeleteDialogProps,
buildEditUpsFormDialogProps, buildEditUpsFormDialogProps,
checks,
getCheck,
hasChecks,
isEditUpses, isEditUpses,
setCheck,
setConfirmDialogProps,
setFormDialogProps, setFormDialogProps,
submitForm,
upsOverviews, upsOverviews,
], ],
); );
@ -259,6 +326,17 @@ const ManageUpsPanel: FC = () => {
[isLoadingUpsTemplate, isUpsOverviewLoading, listElement], [isLoadingUpsTemplate, isUpsOverviewLoading, listElement],
); );
const messageArea = useMemo(
() => (
<MessageGroup
count={1}
defaultMessageType="warning"
ref={messageGroupRef}
/>
),
[],
);
if (isFirstRender) { if (isFirstRender) {
api api
.get<APIUpsTemplate>('/ups/template') .get<APIUpsTemplate>('/ups/template')
@ -283,17 +361,16 @@ const ManageUpsPanel: FC = () => {
</Panel> </Panel>
<FormDialog <FormDialog
{...formDialogProps} {...formDialogProps}
disableProceed={isFormInvalid}
loadingAction={isFormSubmitting}
preActionArea={messageArea}
ref={formDialogRef} ref={formDialogRef}
preActionArea={
<MessageGroup
count={1}
defaultMessageType="warning"
ref={messageGroupRef}
/>
}
proceedButtonProps={{ disabled: isFormInvalid }}
/> />
<ConfirmDialog {...confirmDialogProps} ref={confirmDialogRef} /> <ConfirmDialog
closeOnProceed
{...confirmDialogProps}
ref={confirmDialogRef}
/>
</> </>
); );
}; };

Loading…
Cancel
Save