Skip to content

Commit f4797da

Browse files
authored
Merge pull request #427 from metrico/fix/default_api_url
feat: origin as datasource by default at datasources page
2 parents cf1ede8 + fd5e377 commit f4797da

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

Diff for: packages/main/views/DataSources/components/DataSourcesFiller.tsx

+50-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Switch } from "@mui/material";
2-
import { useState } from "react";
2+
import { useEffect, useState } from "react";
33
import { css, cx } from "@emotion/css";
44
import { useDispatch, useSelector } from "react-redux";
55
import setDataSources from "../store/setDataSources";
@@ -51,9 +51,14 @@ const ForAllButton = css`
5151
flex: 1;
5252
`;
5353

54+
// set a global url for all datasources
55+
5456
export const urlSchema = z.string().url()
5557

58+
const DEFAULT_URL = window.location.origin
59+
5660
export const DataSourcesFiller = (props: any) => {
61+
5762
const [url, setUrl] = useState("");
5863
const [user, setUser] = useState("");
5964
const [password, setPassword] = useState("");
@@ -65,34 +70,12 @@ export const DataSourcesFiller = (props: any) => {
6570
const submitMessage = "Save";
6671
const theme = useTheme();
6772

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
9275
const prevDs = JSON.parse(JSON.stringify(dataSources));
9376
const newDs = prevDs?.map((m: any) => ({
9477
...m,
95-
url,
78+
url:changedUrl,
9679
auth: {
9780
...m.auth,
9881
basicAuth: { ...m.auth.basicAuth, value: basicAuth },
@@ -114,6 +97,47 @@ export const DataSourcesFiller = (props: any) => {
11497
dispatch(setDataSources(newDs));
11598
};
11699

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+
117141
return (
118142
<div className={cx(InlineFlex(theme))}>
119143
<div className={cx(oneForAllStyle)}>

Diff for: packages/main/views/DataSources/store/setDataSources.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const setDataSources = (dataSources: any) => (dispatch: Function) => {
1+
const setDataSources = (dataSources: any) => (dispatch: any) => {
22
dispatch({
33
type: 'SET_DATA_SOURCES',
44
dataSources

0 commit comments

Comments
 (0)