From 7bd7fba1c3bbf6f3013138fb3cdc615b2f9c8de4 Mon Sep 17 00:00:00 2001 From: amhsirak Date: Tue, 25 Nov 2025 16:06:29 +0530 Subject: [PATCH] chore: move PairEditForm to legacy --- legacy/src/LeftSidePanelContent.tsx | 2 +- legacy/src/PairEditForm.tsx | 161 ++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 legacy/src/PairEditForm.tsx diff --git a/legacy/src/LeftSidePanelContent.tsx b/legacy/src/LeftSidePanelContent.tsx index 2efad5df..6cbc8854 100644 --- a/legacy/src/LeftSidePanelContent.tsx +++ b/legacy/src/LeftSidePanelContent.tsx @@ -6,7 +6,7 @@ import { Socket } from "socket.io-client"; import { AddButton } from "../../src/components/ui/buttons/AddButton"; import { AddPair } from "../../src/api/workflow"; import { GenericModal } from "../../src/components/ui/GenericModal"; -import { PairEditForm } from "../../src/components/recorder/PairEditForm"; +import { PairEditForm } from "./PairEditForm"; import { Tooltip } from "@mui/material"; interface LeftSidePanelContentProps { diff --git a/legacy/src/PairEditForm.tsx b/legacy/src/PairEditForm.tsx new file mode 100644 index 00000000..aa3354ec --- /dev/null +++ b/legacy/src/PairEditForm.tsx @@ -0,0 +1,161 @@ +import { Button, TextField, Typography } from "@mui/material"; +import React, { FC } from "react"; +import { Preprocessor, WhereWhatPair } from "maxun-core"; + +interface PairProps { + index: string; + id?: string; + where: string | null; + what: string | null; +} + +interface PairEditFormProps { + onSubmitOfPair: (value: WhereWhatPair, index: number) => void; + numberOfPairs: number; + index?: string; + where?: string; + what?: string; + id?: string; +} + +export const PairEditForm: FC = ( + { + onSubmitOfPair, + numberOfPairs, + index, + where, + what, + id, + }) => { + const [pairProps, setPairProps] = React.useState({ + where: where || null, + what: what || null, + index: index || "1", + id: id || '', + }); + const [errors, setErrors] = React.useState({ + where: null, + what: null, + index: '', + }); + + const handleInputChange = (event: React.ChangeEvent) => { + const { id, value } = event.target; + if (id === 'index') { + if (parseInt(value, 10) < 1) { + setErrors({ ...errors, index: 'Index must be greater than 0' }); + return; + } else { + setErrors({ ...errors, index: '' }); + } + } + setPairProps({ ...pairProps, [id]: value }); + }; + + const validateAndSubmit = (event: React.SyntheticEvent) => { + event.preventDefault(); + let whereFromPair, whatFromPair; + // validate where + whereFromPair = { + where: pairProps.where && pairProps.where !== '{"url":"","selectors":[""] }' + ? JSON.parse(pairProps.where) + : {}, + what: [], + }; + const validationError = Preprocessor.validateWorkflow({ workflow: [whereFromPair] }); + setErrors({ ...errors, where: null }); + if (validationError) { + setErrors({ ...errors, where: validationError.message }); + return; + } + // validate what + whatFromPair = { + where: {}, + what: pairProps.what && pairProps.what !== '[{"action":"","args":[""] }]' + ? JSON.parse(pairProps.what) : [], + }; + const validationErrorWhat = Preprocessor.validateWorkflow({ workflow: [whatFromPair] }); + setErrors({ ...errors, "what": null }); + if (validationErrorWhat) { + setErrors({ ...errors, what: validationErrorWhat.message }); + return; + } + //validate index + const index = parseInt(pairProps?.index, 10); + if (index > (numberOfPairs + 1)) { + if (numberOfPairs === 0) { + setErrors(prevState => ({ + ...prevState, + index: 'Index of the first pair must be 1' + })); + return; + } else { + setErrors(prevState => ({ + ...prevState, + index: `Index must be in the range 1-${numberOfPairs + 1}` + })); + return; + } + } else { + setErrors({ ...errors, index: '' }); + } + // submit the pair + onSubmitOfPair(pairProps.id + ? { + id: pairProps.id, + where: whereFromPair?.where || {}, + what: whatFromPair?.what || [], + } + : { + where: whereFromPair?.where || {}, + what: whatFromPair?.what || [], + } + , index); + }; + + return ( +
+ Raw pair edit form: + + + + + + + ); +};