1
1
'use client'
2
2
import React , { useState } from 'react' ;
3
- import { runTest } from '@/engines' ;
4
3
import { TestInput } from '@/types/TestInput' ;
5
4
import { TestOutput } from '@/types/TestOutput' ;
6
5
import { TestResults } from '@/components/TestResults' ;
@@ -9,61 +8,127 @@ import OptionsInput from './OptionsInput';
9
8
10
9
type TestFormProps = {
11
10
engine : RegexEngine ;
11
+ testInput : TestInput ;
12
12
}
13
13
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 ( ) ;
17
30
18
- const inputs = [ "" , "" , "" , "" , "" ] ;
31
+ console . log ( "test results" , data ) ;
19
32
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 ) => (
21
41
< div className = "mb-2" key = { `key${ index } ` } >
22
42
{ index <= 0 ? < label htmlFor = { `row${ index } ` } className = "col-sm-2 col-form-label" > Inputs</ label > : < > </ > }
23
43
< input type = "text" className = "form-control" id = { `input${ index } ` } name = "input" defaultValue = { input } />
24
44
</ div >
25
45
) ) ;
46
+ console . log ( "render" , testInput . inputs ) ;
47
+
26
48
27
49
const onSubmit = async ( event : React . FormEvent < HTMLFormElement > ) => {
28
50
event . preventDefault ( ) ;
29
51
const form = event . currentTarget ;
30
52
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 ) ;
39
55
setTestInput ( localInput ) ;
40
56
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
+ }
44
60
} ;
45
61
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
+
46
98
return (
47
99
< >
48
- { ( testInput ?
100
+ { ( testInput . regex ?
49
101
( testOutput ? < TestResults testOutput = { testOutput } /> : < > < h2 > Results</ h2 > < p > Loading...</ p > </ > )
50
102
: < > </ > )
51
103
}
52
104
< h2 > Expression to test</ h2 >
53
105
< form method = "post" action = "results.html" onSubmit = { onSubmit } >
54
106
< div className = "mb-3" >
55
107
< 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 } />
57
109
</ div >
58
110
< div className = "mb-3" >
59
111
< 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 } />
61
113
</ div >
62
- { engine . options . length > 0 ? < OptionsInput engine = { engine } options = { [ ] } /> : < > </ > }
114
+ { props . engine . options . length > 0 ? < OptionsInput engine = { props . engine } options = { testInput . option } /> : < > </ > }
63
115
< button type = "submit" className = "btn btn-primary" > Test</ button >
64
116
{ inputRows }
65
117
< 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 }
66
120
</ form >
67
121
</ >
68
122
) ;
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 ; ;
69
134
}
0 commit comments