1
1
import { Switch } from "@mui/material" ;
2
- import { useState } from "react" ;
2
+ import { useEffect , useState } from "react" ;
3
3
import { css , cx } from "@emotion/css" ;
4
4
import { useDispatch , useSelector } from "react-redux" ;
5
5
import setDataSources from "../store/setDataSources" ;
@@ -51,9 +51,14 @@ const ForAllButton = css`
51
51
flex : 1 ;
52
52
` ;
53
53
54
+ // set a global url for all datasources
55
+
54
56
export const urlSchema = z . string ( ) . url ( )
55
57
58
+ const DEFAULT_URL = window . location . origin
59
+
56
60
export const DataSourcesFiller = ( props : any ) => {
61
+
57
62
const [ url , setUrl ] = useState ( "" ) ;
58
63
const [ user , setUser ] = useState ( "" ) ;
59
64
const [ password , setPassword ] = useState ( "" ) ;
@@ -65,34 +70,12 @@ export const DataSourcesFiller = (props: any) => {
65
70
const submitMessage = "Save" ;
66
71
const theme = useTheme ( ) ;
67
72
68
- const urlChange = ( e : any ) => {
69
- const value = e ?. target ?. value || "" ;
70
- const message = urlSchema . safeParse ( value )
71
-
72
- setUrlValid ( ( ) => message . success )
73
-
74
- setUrl ( ( ) => value ) ;
75
- } ;
76
- const userChange = ( e : any ) => {
77
- setUser ( ( ) => e . target . value ) ;
78
- } ;
79
- const passwordChange = ( e : any ) => {
80
- setPassword ( ( ) => e . target . value ) ;
81
- } ;
82
-
83
- const onSwitchChange = ( e : any ) => {
84
- setOneForAll ( ( ) => e . target . checked ) ;
85
- } ;
86
-
87
- const onBasicAuthChange = ( e : any ) => {
88
- setBasicAuth ( ( ) => e . target . checked ) ;
89
- } ;
90
-
91
- const onUseForAll = ( e : any ) => {
73
+ const onUseForAll = ( defaultUrl = "" ) => {
74
+ const changedUrl = defaultUrl === "" ? url : defaultUrl
92
75
const prevDs = JSON . parse ( JSON . stringify ( dataSources ) ) ;
93
76
const newDs = prevDs ?. map ( ( m : any ) => ( {
94
77
...m ,
95
- url,
78
+ url : changedUrl ,
96
79
auth : {
97
80
...m . auth ,
98
81
basicAuth : { ...m . auth . basicAuth , value : basicAuth } ,
@@ -114,6 +97,47 @@ export const DataSourcesFiller = (props: any) => {
114
97
dispatch ( setDataSources ( newDs ) ) ;
115
98
} ;
116
99
100
+ useEffect ( ( ) => {
101
+ if ( url === "" ) {
102
+
103
+ if ( dataSources [ 0 ] ?. url === "" && dataSources [ 1 ] ?. url === "" ) {
104
+ setUrl ( DEFAULT_URL )
105
+ onUseForAll ( DEFAULT_URL )
106
+ } else {
107
+ setUrl ( dataSources [ 0 ] ?. url )
108
+ }
109
+
110
+ setOneForAll ( true )
111
+ setUrlValid ( true )
112
+ }
113
+
114
+ } , [ url ] )
115
+
116
+ const urlChange = ( e : any ) => {
117
+ const value = e ?. target ?. value || "" ;
118
+ const message = urlSchema . safeParse ( value )
119
+
120
+ setUrlValid ( ( ) => message . success )
121
+
122
+ setUrl ( ( ) => value ) ;
123
+ } ;
124
+ const userChange = ( e : any ) => {
125
+ setUser ( ( ) => e . target . value ) ;
126
+ } ;
127
+ const passwordChange = ( e : any ) => {
128
+ setPassword ( ( ) => e . target . value ) ;
129
+ } ;
130
+
131
+ const onSwitchChange = ( e : any ) => {
132
+ setOneForAll ( ( ) => e . target . checked ) ;
133
+ } ;
134
+
135
+ const onBasicAuthChange = ( e : any ) => {
136
+ setBasicAuth ( ( ) => e . target . checked ) ;
137
+ } ;
138
+
139
+
140
+
117
141
return (
118
142
< div className = { cx ( InlineFlex ( theme ) ) } >
119
143
< div className = { cx ( oneForAllStyle ) } >
0 commit comments