@@ -15,30 +15,75 @@ import { useDirectories } from './hooks/useDirectories';
1515
1616export function DynamicForm ( ) {
1717 const serverAddress = "localhost:8100" ;
18-
19- // Use the custom hook to fetch directories
18+ const [ user , setUser ] = useState < string > ( "" ) ;
19+ const [ plan , setPlan ] = useState < string > ( "" ) ;
20+ const [ urlTestdata , setUrlTestdata ] = useState < string > ( "" ) ;
21+
22+ // Function to read URL parameters and update state
23+ const updateStateFromURL = ( ) => {
24+ const params = new URLSearchParams ( window . location . search ) ;
25+ const testdataParam = params . get ( 'testdata' ) ?? "conditional" ;
26+ const planParam = params . get ( 'plan' ) ;
27+ const userParam = params . get ( 'user' ) ;
28+
29+ setUrlTestdata ( testdataParam ) ;
30+ if ( userParam ) setUser ( userParam ) ;
31+ if ( planParam ) setPlan ( planParam ) ;
32+ } ;
33+
34+ // Read URL parameters on component mount
35+ useEffect ( ( ) => {
36+ updateStateFromURL ( ) ;
37+ } , [ ] ) ;
38+
2039 const {
2140 directories,
22- testdata,
23- setTestdata,
41+ // testdata,
42+ // setTestdata,
2443 isLoading,
2544 fetchError
26- } = useDirectories ( serverAddress , "conditional" ) ;
45+ } = useDirectories ( serverAddress , urlTestdata ) ;
2746
28- const [ user , setUser ] = useState < string > ( "" ) ;
29- const [ usePlan , setUsePlan ] = useState < boolean > ( false ) ;
47+ const handleTestdataChange = ( value : string ) => {
48+ const params = new URLSearchParams ( window . location . search ) ;
49+ params . set ( 'testdata' , value ) ;
50+ const newUrl = `${ window . location . pathname } ?${ params . toString ( ) } ` ;
51+ window . history . replaceState ( { } , '' , newUrl ) ;
52+ setUrlTestdata ( value ) ;
53+ setPlan ( "" ) ;
54+ setUser ( "" ) ;
55+ setResponse ( null ) ;
56+ } ;
3057
31- // Use the custom hook to fetch users
3258 const {
3359 users,
3460 isLoading : usersLoading ,
3561 fetchError : usersFetchError
36- } = useUsers ( serverAddress , testdata ) ;
62+ } = useUsers ( serverAddress , urlTestdata ) ;
3763
38- const planPath = "plan.json" ;
39- const wsUrl = `ws://${ serverAddress } /ws/${ encodeURIComponent ( testdata ) } ?${ usePlan ? `plan=${ encodeURIComponent ( planPath ) } &` : '' } ${ user ? `user=${ encodeURIComponent ( user ) } ` : '' } ` ;
64+ // Update URL when user or usePlan changes
65+ useEffect ( ( ) => {
66+ const params = new URLSearchParams ( window . location . search ) ;
67+
68+ if ( plan ) {
69+ params . set ( 'plan' , plan ) ;
70+ } else {
71+ params . delete ( 'plan' ) ;
72+ }
73+
74+ if ( user ) {
75+ params . set ( 'user' , user ) ;
76+ } else {
77+ params . delete ( 'user' ) ;
78+ }
79+
80+ const newUrl = `${ window . location . pathname } ?${ params . toString ( ) } ` ;
81+ window . history . replaceState ( { } , '' , newUrl ) ;
82+ } , [ user , plan ] ) ;
83+
84+ const wsUrl = `ws://${ serverAddress } /ws/${ encodeURIComponent ( urlTestdata ) } ?${ plan ? `plan=${ encodeURIComponent ( plan ) } &` : '' } ${ user ? `user=${ encodeURIComponent ( user ) } ` : '' } ` ;
4085
41- const { message : serverResponse , sendMessage, connectionStatus } = useWebSocket < Response > ( wsUrl ) ;
86+ const { message : serverResponse , sendMessage, connectionStatus } = useWebSocket < Response > ( wsUrl , urlTestdata ) ;
4287
4388 const [ response , setResponse ] = useState < Response | null > ( null ) ;
4489 const [ currentId , setCurrentId ] = useState < number > ( 0 ) ;
@@ -241,19 +286,14 @@ export function DynamicForm() {
241286 return < div > Loading form...</ div > ;
242287 }
243288
244- const sortedParams = [ ...response . parameters ] . sort ( ( a , b ) => a . order - b . order ) ;
289+ const sortedParams = response . parameters ? [ ...response . parameters ] . sort ( ( a , b ) => a . order - b . order ) : [ ] ;
245290
246291 return (
247292 < div className = "flex flex-col gap-12" >
248293 < div className = "flex flex-row gap-4" >
249294 < Select
250- onValueChange = { ( value ) => {
251- setTestdata ( value ) ;
252- // Reset response when changing testdata to avoid showing stale data
253- setResponse ( null ) ;
254- } }
255- value = { testdata }
256- defaultValue = { testdata }
295+ onValueChange = { handleTestdataChange }
296+ value = { urlTestdata }
257297 >
258298 < SelectTrigger className = "w-fit" >
259299 < SelectValue />
@@ -294,8 +334,8 @@ export function DynamicForm() {
294334 < span className = "flex flex-row gap-2 items-center" >
295335 Use Plan
296336 < Switch
297- checked = { usePlan }
298- onCheckedChange = { setUsePlan }
337+ checked = { plan !== "" }
338+ onCheckedChange = { ( ) => setPlan ( plan !== "" ? "" : "plan.json" ) }
299339 />
300340 </ span >
301341 </ div >
0 commit comments