11'use client'
22import React , { useState } from 'react' ;
3- import { runTest } from '@/engines' ;
43import { TestInput } from '@/types/TestInput' ;
54import { TestOutput } from '@/types/TestOutput' ;
65import { TestResults } from '@/components/TestResults' ;
@@ -9,61 +8,127 @@ import OptionsInput from './OptionsInput';
98
109type TestFormProps = {
1110 engine : RegexEngine ;
11+ testInput : TestInput ;
1212}
1313
14- export default function TestForm ( { engine } : TestFormProps ) {
15- const [ testOutput , setTestOutput ] = useState < TestOutput | null > ( ) ;
16- const [ testInput , setTestInput ] = useState < TestInput | null > ( ) ;
14+ async function runTest ( test_url :string , testInput : TestInput ) : Promise < TestOutput > {
15+
16+ const postData =
17+ `regex=${ encodeURIComponent ( testInput . regex ) } ` +
18+ `&replacement=${ encodeURIComponent ( testInput . replacement ) } ` +
19+ `&${ testInput . option . map ( ( option ) => `option=${ option } ` ) . join ( "&" ) } ` +
20+ `&${ testInput . inputs . map ( ( input ) => `input=${ input } ` ) . join ( "&" ) } ` ;
21+
22+ const response = await fetch ( test_url , {
23+ method : "POST" ,
24+ body : postData ,
25+ headers : {
26+ "Content-Type" : "application/x-www-form-urlencoded" ,
27+ } ,
28+ } ) ;
29+ const data = await response . json ( ) ;
1730
18- const inputs = [ "" , "" , "" , "" , "" ] ;
31+ console . log ( "test results" , data ) ;
1932
20- const inputRows = inputs . map ( ( input , index ) => (
33+ return data as TestOutput ;
34+ }
35+
36+ export default function TestForm ( props : TestFormProps ) {
37+ const [ testOutput , setTestOutput ] = useState < TestOutput | null > ( ) ;
38+ const [ testInput , setTestInput ] = useState < TestInput > ( props . testInput ) ;
39+
40+ const inputRows = testInput . inputs . map ( ( input , index ) => (
2141 < div className = "mb-2" key = { `key${ index } ` } >
2242 { index <= 0 ? < label htmlFor = { `row${ index } ` } className = "col-sm-2 col-form-label" > Inputs</ label > : < > </ > }
2343 < input type = "text" className = "form-control" id = { `input${ index } ` } name = "input" defaultValue = { input } />
2444 </ div >
2545 ) ) ;
46+ console . log ( "render" , testInput . inputs ) ;
47+
2648
2749 const onSubmit = async ( event : React . FormEvent < HTMLFormElement > ) => {
2850 event . preventDefault ( ) ;
2951 const form = event . currentTarget ;
3052 const formData = new FormData ( form ) ;
31- const localInput : TestInput = {
32- engine : engine . handle ,
33- regex : formData . get ( 'regex' ) as string ,
34- replacement : formData . get ( 'replacement' ) as string ,
35- option : formData . getAll ( 'option' ) as string [ ] ,
36- inputs : formData . getAll ( 'input' ) as string [ ]
37- } ;
38- console . log ( localInput ) ;
53+ const localInput = formDataToTestInput ( props . engine . handle , formData ) ;
54+ console . log ( props . engine . test_url , localInput ) ;
3955 setTestInput ( localInput ) ;
4056 setTestOutput ( null ) ;
41- setTestOutput ( await runTest ( engine , formData ) ) ;
42-
43- //window.history.pushState(null, "", `/advanced/${engine.handle}/results.html`);
57+ if ( props . engine . test_url ) {
58+ setTestOutput ( await runTest ( props . engine . test_url , localInput ) ) ;
59+ }
4460 } ;
4561
62+ const onMoreInputs = ( event : React . MouseEvent < HTMLButtonElement > ) => {
63+ event . preventDefault ( ) ;
64+ console . log ( event ) ;
65+ const form = event . currentTarget . form ;
66+ if ( ! form ) {
67+ return ;
68+ }
69+ const formData = new FormData ( form ) ;
70+ const localInput = formDataToTestInput ( props . engine . handle , formData ) ;
71+ console . log ( "before more" , localInput . inputs ) ;
72+ for ( let i = 0 ; i < 3 ; i ++ ) {
73+ localInput . inputs . push ( '' ) ;
74+ }
75+ console . log ( "after more" , localInput . inputs ) ;
76+
77+ setTestInput ( localInput ) ;
78+ }
79+
80+ const onFewerInputs = ( event : React . MouseEvent < HTMLButtonElement > ) => {
81+ event . preventDefault ( ) ;
82+ console . log ( event ) ;
83+ const form = event . currentTarget . form ;
84+ if ( ! form ) {
85+ return ;
86+ }
87+ const formData = new FormData ( form ) ;
88+ const localInput = formDataToTestInput ( props . engine . handle , formData ) ;
89+ console . log ( "before fewer" , localInput . inputs ) ;
90+ localInput . inputs = localInput . inputs . filter ( x => x . length > 0 ) ;
91+ while ( localInput . inputs . length < 5 ) {
92+ localInput . inputs . push ( '' ) ;
93+ }
94+ setTestInput ( localInput ) ;
95+ console . log ( "after fewer" , localInput . inputs ) ;
96+ }
97+
4698 return (
4799 < >
48- { ( testInput ?
100+ { ( testInput . regex ?
49101 ( testOutput ? < TestResults testOutput = { testOutput } /> : < > < h2 > Results</ h2 > < p > Loading...</ p > </ > )
50102 : < > </ > )
51103 }
52104 < h2 > Expression to test</ h2 >
53105 < form method = "post" action = "results.html" onSubmit = { onSubmit } >
54106 < div className = "mb-3" >
55107 < label htmlFor = "regex" className = "form-label" > Regular Expression</ label >
56- < input type = "text" className = "form-control" id = "regex" name = "regex" />
108+ < input type = "text" className = "form-control" id = "regex" name = "regex" defaultValue = { testInput . regex } />
57109 </ div >
58110 < div className = "mb-3" >
59111 < label htmlFor = "replacement" className = "form-label" > Replacement</ label >
60- < input type = "text" className = "form-control" id = "replacement" name = "replacement" />
112+ < input type = "text" className = "form-control" id = "replacement" name = "replacement" defaultValue = { testInput . replacement } />
61113 </ div >
62- { engine . options . length > 0 ? < OptionsInput engine = { engine } options = { [ ] } /> : < > </ > }
114+ { props . engine . options . length > 0 ? < OptionsInput engine = { props . engine } options = { testInput . option } /> : < > </ > }
63115 < button type = "submit" className = "btn btn-primary" > Test</ button >
64116 { inputRows }
65117 < button type = "submit" className = "btn btn-primary" > Test</ button >
118+ < button className = "ms-3 btn btn-outline-primary" onClick = { onMoreInputs } > More inputs</ button >
119+ { testInput . inputs . length > 5 ? < button className = "ms-3 btn btn-outline-primary" onClick = { onFewerInputs } > Fewer inputs</ button > : null }
66120 </ form >
67121 </ >
68122 ) ;
123+ }
124+
125+ function formDataToTestInput ( engineHandle :string , formData : FormData ) : TestInput {
126+ const retVal : TestInput = {
127+ engine : engineHandle ,
128+ regex : formData . get ( 'regex' ) as string ,
129+ replacement : formData . get ( 'replacement' ) as string ,
130+ option : formData . getAll ( 'option' ) as string [ ] ,
131+ inputs : formData . getAll ( 'input' ) as string [ ]
132+ } ;
133+ return retVal ; ;
69134}
0 commit comments