1
1
'use client'
2
- import React , { useState } from 'react' ;
2
+ import React , { useEffect , useState } from 'react' ;
3
3
import { TestResults } from '@/components/TestResults' ;
4
4
import { RegexEngine } from '@/engines/RegexEngine' ;
5
5
import OptionsInput from './OptionsInput' ;
6
- import { runTest as runBrowserTest , type TestInput , type TestOutput } from '@regexplanet/common' ;
6
+ import { type TestInput , type TestOutput } from '@regexplanet/common' ;
7
7
import { useRouter } from 'next/navigation' ;
8
8
import { formDataToTestInput } from '@/functions/formDataToTestInput' ;
9
+ import { runTestPage } from './runTestPage' ;
9
10
10
11
type TestFormProps = {
11
12
engine : RegexEngine ;
12
- testUrl ? : string ; // override for use during engine development
13
+ testUrl : string ;
13
14
testInput : TestInput ;
15
+ testOutput : TestOutput | null ;
14
16
}
15
17
16
- function setTestInput ( testInput : TestInput ) {
18
+ const pendingTestOutput : TestOutput = {
19
+ success : true ,
20
+ html : `<p><img src="/images/spinner.gif" alt="spinner" /> Running, please wait...</p>` ,
21
+ } ;
22
+
23
+ function setTestInput ( testInput : TestInput ) : string {
17
24
const searchParams = new URLSearchParams ( ) ;
18
25
searchParams . set ( 'regex' , testInput . regex ) ;
19
26
searchParams . set ( 'replacement' , testInput . replacement ) ;
@@ -22,49 +29,13 @@ function setTestInput(testInput: TestInput) {
22
29
23
30
const url = new URL ( window . location . href ) ;
24
31
url . search = searchParams . toString ( ) ;
25
- window . history . pushState ( { } , '' , url . toString ( ) ) ;
26
- }
27
-
28
- async function runTest ( test_url :string , testInput : TestInput ) : Promise < TestOutput > {
29
-
30
- console . log ( "running test" , testInput ) ;
31
- if ( ! testInput || ! testInput . regex ) {
32
- return {
33
- success : false ,
34
- message : "Please enter a regular expression to test" ,
35
- } ;
36
- }
37
-
38
- if ( test_url === 'javascript:runBrowserTest' ) {
39
- return runBrowserTest ( testInput ) ;
40
- }
41
-
42
- const postData =
43
- `regex=${ encodeURIComponent ( testInput . regex ) } ` +
44
- `&replacement=${ encodeURIComponent ( testInput . replacement ) } ` +
45
- `&${ testInput . options . map ( ( option ) => `option=${ encodeURIComponent ( option ) } ` ) . join ( "&" ) } ` +
46
- `&${ testInput . inputs . map ( ( input ) => `input=${ encodeURIComponent ( input ) } ` ) . join ( "&" ) } ` ;
47
-
48
- console . log ( "posting" , test_url , postData ) ;
49
32
50
- const response = await fetch ( test_url , {
51
- method : "POST" ,
52
- body : postData ,
53
- headers : {
54
- "Content-Type" : "application/x-www-form-urlencoded" ,
55
- } ,
56
- //mode: "no-cors",
57
- } ) ;
58
- console . log ( "response" , response ) ;
59
- const data = await response . json ( ) ;
60
-
61
- console . log ( "test results" , data ) ;
62
-
63
- return data as TestOutput ;
33
+ //window.history.pushState({}, '', url.toString());
34
+ return url . toString ( ) ;
64
35
}
65
36
66
37
export default function TestForm ( props : TestFormProps ) {
67
- const [ testOutput , setTestOutput ] = useState < TestOutput | null > ( ) ;
38
+ const [ testOutput , setTestOutput ] = useState < TestOutput | null > ( props . testOutput ) ;
68
39
const router = useRouter ( )
69
40
//const [testInput, setTestInput] = useState<TestInput>(props.testInput);
70
41
const testInput = props . testInput ;
@@ -79,22 +50,36 @@ export default function TestForm(props: TestFormProps) {
79
50
const onClearResults = ( ) => {
80
51
setTestOutput ( null ) ;
81
52
} ;
82
-
83
53
const onSubmit = async ( event : React . FormEvent < HTMLFormElement > ) => {
84
54
event . preventDefault ( ) ;
85
55
const form = event . currentTarget ;
86
56
const formData = new FormData ( form ) ;
87
57
const localInput = formDataToTestInput ( props . engine . handle , formData ) ;
88
- const testUrl = props . testUrl || props . engine . test_url ;
89
- console . log ( testUrl , localInput ) ;
58
+ console . log ( props . testUrl , localInput ) ;
90
59
setTestInput ( localInput ) ;
91
- setTestOutput ( { success : true , message : "Loading..." } ) ;
92
- if ( testUrl ) {
93
- const newTestOutput = await runTest ( testUrl , localInput ) ;
94
- console . log ( "newTestOutput" , newTestOutput ) ;
95
- setTestOutput ( newTestOutput ) ;
96
- }
60
+ setTestOutput ( pendingTestOutput ) ;
61
+ const newTestOutput = await runTestPage ( props . testUrl , localInput ) ;
62
+ console . log ( "newTestOutput" , newTestOutput ) ;
63
+ setTestOutput ( newTestOutput ) ;
97
64
} ;
65
+ /*
66
+ const onSubmit = () => {
67
+ setTestOutput(pendingTestOutput);
68
+ }
69
+ */
70
+
71
+ useEffect ( ( ) => {
72
+ async function runTestEffect ( ) {
73
+ if ( props . testInput . regex ) {
74
+ const testUrl = props . testUrl || props . engine . test_url ;
75
+ if ( testUrl ) {
76
+ const newTestOutput = await runTestPage ( testUrl , props . testInput ) ;
77
+ setTestOutput ( newTestOutput ) ;
78
+ }
79
+ } }
80
+ if ( props . testInput . regex ) { setTestOutput ( pendingTestOutput ) } ;
81
+ runTestEffect ( ) ;
82
+ } , [ props . testInput , props . testUrl , props . engine . test_url ] ) ;
98
83
99
84
const onMoreInputs = ( event : React . MouseEvent < HTMLButtonElement > ) => {
100
85
event . preventDefault ( ) ;
@@ -111,7 +96,7 @@ export default function TestForm(props: TestFormProps) {
111
96
}
112
97
console . log ( "after more" , localInput . inputs ) ;
113
98
114
- setTestInput ( localInput ) ;
99
+ router . push ( setTestInput ( localInput ) ) ;
115
100
}
116
101
117
102
const onFewerInputs = ( event : React . MouseEvent < HTMLButtonElement > ) => {
@@ -128,8 +113,8 @@ export default function TestForm(props: TestFormProps) {
128
113
while ( localInput . inputs . length < 5 ) {
129
114
localInput . inputs . push ( '' ) ;
130
115
}
131
- setTestInput ( localInput ) ;
132
116
console . log ( "after fewer" , localInput . inputs ) ;
117
+ router . push ( setTestInput ( localInput ) ) ;
133
118
} ;
134
119
135
120
const onSwitchEngines = ( event : React . MouseEvent < HTMLButtonElement > ) => {
@@ -156,7 +141,7 @@ export default function TestForm(props: TestFormProps) {
156
141
return (
157
142
< >
158
143
{
159
- props . testUrl ? < div className = "alert alert-warning" > Testing against { props . testUrl } !</ div > : < > </ >
144
+ props . testUrl != props . engine . test_url ? < div className = "alert alert-warning" > Testing against { props . testUrl } !</ div > : < > </ >
160
145
}
161
146
< h2 > Expression to test</ h2 >
162
147
< form method = "get" action = "index.html" onSubmit = { onSubmit } >
@@ -173,7 +158,6 @@ export default function TestForm(props: TestFormProps) {
173
158
< button type = "submit" className = "btn btn-primary" > Test</ button >
174
159
{
175
160
( testOutput ? < TestResults onClear = { onClearResults } testOutput = { testOutput } /> : < > </ > )
176
-
177
161
}
178
162
< h2 className = "pt-3" > Inputs</ h2 >
179
163
{ inputRows }
0 commit comments