11'use client'
2- import React , { useState } from 'react' ;
2+ import React , { useEffect , useState } from 'react' ;
33import { TestResults } from '@/components/TestResults' ;
44import { RegexEngine } from '@/engines/RegexEngine' ;
55import OptionsInput from './OptionsInput' ;
6- import { runTest as runBrowserTest , type TestInput , type TestOutput } from '@regexplanet/common' ;
6+ import { type TestInput , type TestOutput } from '@regexplanet/common' ;
77import { useRouter } from 'next/navigation' ;
88import { formDataToTestInput } from '@/functions/formDataToTestInput' ;
9+ import { runTestPage } from './runTestPage' ;
910
1011type TestFormProps = {
1112 engine : RegexEngine ;
12- testUrl ? : string ; // override for use during engine development
13+ testUrl : string ;
1314 testInput : TestInput ;
15+ testOutput : TestOutput | null ;
1416}
1517
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 {
1724 const searchParams = new URLSearchParams ( ) ;
1825 searchParams . set ( 'regex' , testInput . regex ) ;
1926 searchParams . set ( 'replacement' , testInput . replacement ) ;
@@ -22,49 +29,13 @@ function setTestInput(testInput: TestInput) {
2229
2330 const url = new URL ( window . location . href ) ;
2431 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 ) ;
4932
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 ( ) ;
6435}
6536
6637export default function TestForm ( props : TestFormProps ) {
67- const [ testOutput , setTestOutput ] = useState < TestOutput | null > ( ) ;
38+ const [ testOutput , setTestOutput ] = useState < TestOutput | null > ( props . testOutput ) ;
6839 const router = useRouter ( )
6940 //const [testInput, setTestInput] = useState<TestInput>(props.testInput);
7041 const testInput = props . testInput ;
@@ -79,22 +50,36 @@ export default function TestForm(props: TestFormProps) {
7950 const onClearResults = ( ) => {
8051 setTestOutput ( null ) ;
8152 } ;
82-
8353 const onSubmit = async ( event : React . FormEvent < HTMLFormElement > ) => {
8454 event . preventDefault ( ) ;
8555 const form = event . currentTarget ;
8656 const formData = new FormData ( form ) ;
8757 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 ) ;
9059 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 ) ;
9764 } ;
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 ] ) ;
9883
9984 const onMoreInputs = ( event : React . MouseEvent < HTMLButtonElement > ) => {
10085 event . preventDefault ( ) ;
@@ -111,7 +96,7 @@ export default function TestForm(props: TestFormProps) {
11196 }
11297 console . log ( "after more" , localInput . inputs ) ;
11398
114- setTestInput ( localInput ) ;
99+ router . push ( setTestInput ( localInput ) ) ;
115100 }
116101
117102 const onFewerInputs = ( event : React . MouseEvent < HTMLButtonElement > ) => {
@@ -128,8 +113,8 @@ export default function TestForm(props: TestFormProps) {
128113 while ( localInput . inputs . length < 5 ) {
129114 localInput . inputs . push ( '' ) ;
130115 }
131- setTestInput ( localInput ) ;
132116 console . log ( "after fewer" , localInput . inputs ) ;
117+ router . push ( setTestInput ( localInput ) ) ;
133118 } ;
134119
135120 const onSwitchEngines = ( event : React . MouseEvent < HTMLButtonElement > ) => {
@@ -156,7 +141,7 @@ export default function TestForm(props: TestFormProps) {
156141 return (
157142 < >
158143 {
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 > : < > </ >
160145 }
161146 < h2 > Expression to test</ h2 >
162147 < form method = "get" action = "index.html" onSubmit = { onSubmit } >
@@ -173,7 +158,6 @@ export default function TestForm(props: TestFormProps) {
173158 < button type = "submit" className = "btn btn-primary" > Test</ button >
174159 {
175160 ( testOutput ? < TestResults onClear = { onClearResults } testOutput = { testOutput } /> : < > </ > )
176-
177161 }
178162 < h2 className = "pt-3" > Inputs</ h2 >
179163 { inputRows }
0 commit comments