@@ -4,6 +4,8 @@ import { TestResults } from '@/components/TestResults';
4
4
import { RegexEngine } from '@/engines/RegexEngine' ;
5
5
import OptionsInput from './OptionsInput' ;
6
6
import { runTest as runBrowserTest , type TestInput , type TestOutput } from '@regexplanet/common' ;
7
+ import { useRouter } from 'next/navigation' ;
8
+ import { formDataToTestInput } from '@/functions/formDataToTestInput' ;
7
9
8
10
type TestFormProps = {
9
11
engine : RegexEngine ;
@@ -63,6 +65,7 @@ async function runTest(test_url:string, testInput: TestInput): Promise<TestOutpu
63
65
64
66
export default function TestForm ( props : TestFormProps ) {
65
67
const [ testOutput , setTestOutput ] = useState < TestOutput | null > ( ) ;
68
+ const router = useRouter ( )
66
69
//const [testInput, setTestInput] = useState<TestInput>(props.testInput);
67
70
const testInput = props . testInput ;
68
71
@@ -127,7 +130,28 @@ export default function TestForm(props: TestFormProps) {
127
130
}
128
131
setTestInput ( localInput ) ;
129
132
console . log ( "after fewer" , localInput . inputs ) ;
130
- }
133
+ } ;
134
+
135
+ const onSwitchEngines = ( event : React . MouseEvent < HTMLButtonElement > ) => {
136
+ event . preventDefault ( ) ;
137
+ const form = event . currentTarget . form ;
138
+ if ( ! form ) {
139
+ return ;
140
+ }
141
+ const formData = new FormData ( form ) ;
142
+ const localInput = formDataToTestInput ( props . engine . handle , formData ) ;
143
+
144
+ const searchParams = new URLSearchParams ( ) ;
145
+ searchParams . set ( 'engine' , props . engine . handle ) ;
146
+ searchParams . set ( 'regex' , localInput . regex ) ;
147
+ searchParams . set ( 'replacement' , localInput . replacement ) ;
148
+ localInput . options . forEach ( option => searchParams . append ( 'option' , option ) ) ;
149
+ localInput . inputs . forEach ( input => searchParams . append ( 'input' , input ) ) ;
150
+
151
+ const url = new URL ( '/advanced/select.html' , window . location . href ) ;
152
+ url . search = searchParams . toString ( ) ;
153
+ router . push ( url . toString ( ) ) ;
154
+ } ;
131
155
132
156
return (
133
157
< >
@@ -156,18 +180,9 @@ export default function TestForm(props: TestFormProps) {
156
180
< button type = "submit" className = "btn btn-primary" > Test</ button >
157
181
< button className = "ms-3 btn btn-outline-primary" onClick = { onMoreInputs } > More inputs</ button >
158
182
{ testInput . inputs . length > 5 ? < button className = "ms-3 btn btn-outline-primary" onClick = { onFewerInputs } > Fewer inputs</ button > : null }
183
+ < button type = "submit" className = "btn btn-outline-primary float-end" onClick = { onSwitchEngines } > Switch Engines</ button >
159
184
</ form >
160
185
</ >
161
186
) ;
162
187
}
163
188
164
- function formDataToTestInput ( engineHandle :string , formData : FormData ) : TestInput {
165
- const retVal : TestInput = {
166
- engine : engineHandle ,
167
- regex : formData . get ( 'regex' ) as string ,
168
- replacement : formData . get ( 'replacement' ) as string ,
169
- options : formData . getAll ( 'option' ) as string [ ] ,
170
- inputs : formData . getAll ( 'input' ) as string [ ]
171
- } ;
172
- return retVal ;
173
- }
0 commit comments