From 05d957994985cfa43a6bf7fd552b803ffc594d6f Mon Sep 17 00:00:00 2001 From: Andrew Marcuse Date: Mon, 28 Oct 2024 19:59:24 -0400 Subject: [PATCH] Button and page to switch engines --- .../advanced/[engine]/index.html/TestForm.tsx | 37 ++++++++---- src/app/advanced/select.html/page.tsx | 58 +++++++++++++++++++ src/app/share/index.html/PreviewRegex.tsx | 1 - src/functions/formDataToTestInput.ts | 15 +++++ src/functions/testInputToSearchParams.ts | 11 ++++ 5 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 src/app/advanced/select.html/page.tsx create mode 100644 src/functions/formDataToTestInput.ts create mode 100644 src/functions/testInputToSearchParams.ts diff --git a/src/app/advanced/[engine]/index.html/TestForm.tsx b/src/app/advanced/[engine]/index.html/TestForm.tsx index 75deb5b..5eff390 100644 --- a/src/app/advanced/[engine]/index.html/TestForm.tsx +++ b/src/app/advanced/[engine]/index.html/TestForm.tsx @@ -4,6 +4,8 @@ import { TestResults } from '@/components/TestResults'; import { RegexEngine } from '@/engines/RegexEngine'; import OptionsInput from './OptionsInput'; import { runTest as runBrowserTest, type TestInput, type TestOutput } from '@regexplanet/common'; +import { useRouter } from 'next/navigation'; +import { formDataToTestInput } from '@/functions/formDataToTestInput'; type TestFormProps = { engine: RegexEngine; @@ -63,6 +65,7 @@ async function runTest(test_url:string, testInput: TestInput): Promise(); + const router = useRouter() //const [testInput, setTestInput] = useState(props.testInput); const testInput = props.testInput; @@ -127,7 +130,28 @@ export default function TestForm(props: TestFormProps) { } setTestInput(localInput); console.log("after fewer", localInput.inputs); - } + }; + + const onSwitchEngines = (event: React.MouseEvent) => { + event.preventDefault(); + const form = event.currentTarget.form; + if (!form) { + return; + } + const formData = new FormData(form); + const localInput = formDataToTestInput(props.engine.handle, formData); + + const searchParams = new URLSearchParams(); + searchParams.set('engine', props.engine.handle); + searchParams.set('regex', localInput.regex); + searchParams.set('replacement', localInput.replacement); + localInput.options.forEach(option => searchParams.append('option', option)); + localInput.inputs.forEach(input => searchParams.append('input', input)); + + const url = new URL('/advanced/select.html', window.location.href); + url.search = searchParams.toString(); + router.push(url.toString()); + }; return ( <> @@ -156,18 +180,9 @@ export default function TestForm(props: TestFormProps) { { testInput.inputs.length > 5 ? : null } + ); } -function formDataToTestInput(engineHandle:string, formData: FormData): TestInput { - const retVal: TestInput = { - engine: engineHandle, - regex: formData.get('regex') as string, - replacement: formData.get('replacement') as string, - options: formData.getAll('option') as string[], - inputs: formData.getAll('input') as string[] - }; - return retVal; -} \ No newline at end of file diff --git a/src/app/advanced/select.html/page.tsx b/src/app/advanced/select.html/page.tsx new file mode 100644 index 0000000..d4ce4fa --- /dev/null +++ b/src/app/advanced/select.html/page.tsx @@ -0,0 +1,58 @@ +'use client'; +import { type TestInput } from "@regexplanet/common"; +import { getWorkingEngines } from '@/engines'; +import { cleanupSearchParam } from "@/functions/cleanupSearchParam"; +import { cleanupSearchParamArray } from "@/functions/cleanupSearchParamArray"; +import { formDataToTestInput } from "@/functions/formDataToTestInput"; +import { testInputToSearchParams } from "@/functions/testInputToSearchParams"; +import { useRouter } from "next/navigation"; + +export default function SelectEnginePage({ searchParams }: { searchParams: { [key: string]: string | string[] | undefined } }) { + const router = useRouter(); + + const testInput: TestInput = { + engine: cleanupSearchParam(searchParams["engine"], 'nodejs'), + regex: cleanupSearchParam(searchParams["regex"]), + replacement: cleanupSearchParam(searchParams["replacement"]), + options: cleanupSearchParamArray(searchParams["option"]), + inputs: cleanupSearchParamArray(searchParams["input"]), + } + + const onSubmit = (event: React.FormEvent) => { + event.preventDefault(); + const formData = new FormData(event.currentTarget); + const testInput = formDataToTestInput(formData.get('engine') as string, formData); + const url = `/advanced/${testInput.engine}/index.html?${testInputToSearchParams(testInput).toString()}`; + console.log('router.push', url); + router.push(url); + }; + + const options = testInput.options.map((option, index) => { return (); }); + const inputs = testInput.inputs.map((input, index) => { return () }); + return ( + <> +

Select Engine

+
+ + +
+
+ + +
+
+ + { getWorkingEngines().map((theEngine, index) => { return ( +
+ + + + {options} + {inputs} + +
) })} +
+
Raw data
{JSON.stringify(testInput, null, 2)}
+ + ) +} diff --git a/src/app/share/index.html/PreviewRegex.tsx b/src/app/share/index.html/PreviewRegex.tsx index 1e14b53..98fee06 100644 --- a/src/app/share/index.html/PreviewRegex.tsx +++ b/src/app/share/index.html/PreviewRegex.tsx @@ -1,5 +1,4 @@ import { type TestInput } from "@regexplanet/common"; -import { SubmitButton } from '@/components/SubmitButton'; import { getWorkingEngines } from '@/engines'; type PreviewRegexProps = { diff --git a/src/functions/formDataToTestInput.ts b/src/functions/formDataToTestInput.ts new file mode 100644 index 0000000..50058a6 --- /dev/null +++ b/src/functions/formDataToTestInput.ts @@ -0,0 +1,15 @@ +import { TestInput } from "@regexplanet/common"; + +export function formDataToTestInput( + engineHandle: string, + formData: FormData +): TestInput { + const retVal: TestInput = { + engine: engineHandle, + regex: formData.get("regex") as string, + replacement: formData.get("replacement") as string, + options: formData.getAll("option") as string[], + inputs: formData.getAll("input") as string[], + }; + return retVal; +} diff --git a/src/functions/testInputToSearchParams.ts b/src/functions/testInputToSearchParams.ts new file mode 100644 index 0000000..191928c --- /dev/null +++ b/src/functions/testInputToSearchParams.ts @@ -0,0 +1,11 @@ +import { TestInput } from "@regexplanet/common"; + +export function testInputToSearchParams(testInput: TestInput): URLSearchParams { + const searchParams = new URLSearchParams(); + searchParams.set("engine", testInput.engine); + searchParams.set("regex", testInput.regex); + searchParams.set("replacement", testInput.replacement); + testInput.options.forEach((option) => searchParams.append("option", option)); + testInput.inputs.forEach((input) => searchParams.append("input", input)); + return searchParams; +} \ No newline at end of file