From 9e71148d190560a4d87246c4ebfb51c263bc22a7 Mon Sep 17 00:00:00 2001 From: Andrew Baldwin Date: Wed, 9 Oct 2024 15:13:49 +0200 Subject: [PATCH 1/2] Allow alerts and errors on new and edit form --- locust/webui/src/components/Modal/Modal.tsx | 8 +- .../components/StateButtons/EditButton.tsx | 6 +- .../components/StateButtons/NewTestButton.tsx | 6 +- .../components/SwarmForm/SwarmEditForm.tsx | 61 ------------- .../src/components/SwarmForm/SwarmForm.tsx | 89 +++++++++++-------- .../SwarmForm/tests/SwarmEditForm.test.tsx | 52 ----------- locust/webui/src/lib.tsx | 1 + 7 files changed, 65 insertions(+), 158 deletions(-) delete mode 100644 locust/webui/src/components/SwarmForm/SwarmEditForm.tsx delete mode 100644 locust/webui/src/components/SwarmForm/tests/SwarmEditForm.test.tsx diff --git a/locust/webui/src/components/Modal/Modal.tsx b/locust/webui/src/components/Modal/Modal.tsx index a07e549aa7..d7416eca6f 100644 --- a/locust/webui/src/components/Modal/Modal.tsx +++ b/locust/webui/src/components/Modal/Modal.tsx @@ -1,5 +1,5 @@ import CloseIcon from '@mui/icons-material/Close'; -import { Box, IconButton, Modal as MuiModal } from '@mui/material'; +import { Container, IconButton, Modal as MuiModal } from '@mui/material'; interface IModal { open: boolean; @@ -10,13 +10,13 @@ interface IModal { export default function Modal({ open, onClose, children }: IModal) { return ( - {children} - + ); } diff --git a/locust/webui/src/components/StateButtons/EditButton.tsx b/locust/webui/src/components/StateButtons/EditButton.tsx index 2b9d6e5cb8..db88146b20 100644 --- a/locust/webui/src/components/StateButtons/EditButton.tsx +++ b/locust/webui/src/components/StateButtons/EditButton.tsx @@ -2,9 +2,9 @@ import { useState } from 'react'; import { Button } from '@mui/material'; import Modal from 'components/Modal/Modal'; -import SwarmEditForm from 'components/SwarmForm/SwarmEditForm'; +import SwarmForm, { ISwarmFormProps } from 'components/SwarmForm/SwarmForm'; -export default function EditButton() { +export default function EditButton(swarmFormProps: ISwarmFormProps) { const [open, setOpen] = useState(false); return ( @@ -13,7 +13,7 @@ export default function EditButton() { Edit setOpen(false)} open={open}> - setOpen(false)} /> + setOpen(false)} /> ); diff --git a/locust/webui/src/components/StateButtons/NewTestButton.tsx b/locust/webui/src/components/StateButtons/NewTestButton.tsx index 51b429b8e0..70b1a00107 100644 --- a/locust/webui/src/components/StateButtons/NewTestButton.tsx +++ b/locust/webui/src/components/StateButtons/NewTestButton.tsx @@ -2,9 +2,9 @@ import { useState } from 'react'; import { Button } from '@mui/material'; import Modal from 'components/Modal/Modal'; -import SwarmForm from 'components/SwarmForm/SwarmForm'; +import SwarmForm, { ISwarmFormProps } from 'components/SwarmForm/SwarmForm'; -export default function NewTestButton() { +export default function NewTestButton(swarmFormProps: ISwarmFormProps) { const [open, setOpen] = useState(false); return ( @@ -13,7 +13,7 @@ export default function NewTestButton() { New setOpen(false)} open={open}> - + ); diff --git a/locust/webui/src/components/SwarmForm/SwarmEditForm.tsx b/locust/webui/src/components/SwarmForm/SwarmEditForm.tsx deleted file mode 100644 index 739ceee861..0000000000 --- a/locust/webui/src/components/SwarmForm/SwarmEditForm.tsx +++ /dev/null @@ -1,61 +0,0 @@ -import { Box, Button, Container, TextField, Typography } from '@mui/material'; -import { connect } from 'react-redux'; - -import Form from 'components/Form/Form'; -import { useStartSwarmMutation } from 'redux/api/swarm'; -import { swarmActions } from 'redux/slice/swarm.slice'; -import { IRootState } from 'redux/store'; -import { ISwarmFormInput, ISwarmState } from 'types/swarm.types'; - -interface ISwarmForm extends ISwarmFormInput { - onSubmit: () => void; - setSwarm: (swarmPayload: Partial) => void; -} - -function SwarmEditForm({ onSubmit, userCount, spawnRate, setSwarm }: ISwarmForm) { - const [startSwarm] = useStartSwarmMutation(); - - const onEditSwarm = (inputData: ISwarmFormInput) => { - onSubmit(); - startSwarm(inputData); - setSwarm(inputData); - }; - - return ( - - - Edit running load test - - onSubmit={onEditSwarm}> - - - - - - - - - - ); -} - -const storeConnector = ({ swarm: { spawnRate, userCount } }: IRootState) => ({ - spawnRate, - userCount, -}); - -const actionCreator = { - setSwarm: swarmActions.setSwarm, -}; - -export default connect(storeConnector, actionCreator)(SwarmEditForm); diff --git a/locust/webui/src/components/SwarmForm/SwarmForm.tsx b/locust/webui/src/components/SwarmForm/SwarmForm.tsx index 9158227ef1..a7f8b979bc 100644 --- a/locust/webui/src/components/SwarmForm/SwarmForm.tsx +++ b/locust/webui/src/components/SwarmForm/SwarmForm.tsx @@ -29,6 +29,17 @@ interface IDispatchProps { setSwarm: (swarmPayload: Partial) => void; } +export interface ISwarmFormProps { + alert?: { + level?: AlertColor; + message: string; + }; + isDisabled?: boolean; + isEditSwarm?: boolean; + onFormChange?: (formData: React.ChangeEvent) => void; + onFormSubmit?: (inputData: ISwarmFormInput) => void; +} + interface ISwarmForm extends IDispatchProps, Pick< @@ -43,14 +54,9 @@ interface ISwarmForm | 'showUserclassPicker' | 'spawnRate' | 'numUsers' - > { - alert?: { - level?: AlertColor; - message: string; - }; - isDisabled?: boolean; - onFormChange?: (formData: React.ChangeEvent) => void; -} + | 'userCount' + >, + ISwarmFormProps {} function SwarmForm({ availableShapeClasses, @@ -59,6 +65,7 @@ function SwarmForm({ extraOptions, isShape, numUsers, + userCount, overrideHostWarning, runTime, setSwarm, @@ -66,7 +73,9 @@ function SwarmForm({ spawnRate, alert, isDisabled = false, + isEditSwarm = false, onFormChange, + onFormSubmit, }: ISwarmForm) { const [startSwarm] = useStartSwarmMutation(); const [errorMessage, setErrorMessage] = useState(''); @@ -84,11 +93,15 @@ function SwarmForm({ host: inputData.host || host, runTime: inputData.runTime, spawnRate: Number(inputData.spawnRate) || null, - numUsers: Number(inputData.userCount) || null, + userCount: Number(inputData.userCount), }); } else { setErrorMessage(data ? data.message : 'An unknown error occured.'); } + + if (onFormSubmit) { + onFormSubmit(inputData); + } }; const handleSwarmFormChange = (formEvent: React.ChangeEvent) => { @@ -104,9 +117,9 @@ function SwarmForm({ return ( - Start new load test + {isEditSwarm ? 'Edit running load test' : 'Start new load test'} - {showUserclassPicker && ( + {!isEditSwarm && showUserclassPicker && ( - {showUserclassPicker && ( + {!isEditSwarm && showUserclassPicker && (