Skip to content

Commit

Permalink
fix(db-connection-ui): Additional Query Parameters render (#15150)
Browse files Browse the repository at this point in the history
* working query params

* move condition out before update or create

* fix type script issues
  • Loading branch information
hughhhh authored Jun 14, 2021
1 parent 461f64b commit ad706d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ function dbReducer(
const trimmedState = {
...(state || {}),
};
let query = '';

switch (action.type) {
case ActionType.inputChange:
Expand Down Expand Up @@ -169,10 +170,20 @@ function dbReducer(
[action.payload.name]: action.payload.value,
};
case ActionType.fetched:
if (action.payload?.parameters?.query) {
// convert query into URI params string
query = new URLSearchParams(
action.payload.parameters.query as string,
).toString();
}
return {
engine: trimmedState.engine,
configuration_method: trimmedState.configuration_method,
...action.payload,
parameters: {
...action.payload.parameters,
query,
},
};
case ActionType.dbSelected:
return {
Expand Down Expand Up @@ -265,6 +276,19 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
const onSave = async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { id, ...update } = db || {};

if (update?.parameters?.query) {
// convert query params into dictionary
update.parameters.query = JSON.parse(
`{"${decodeURI((update?.parameters?.query as string) || '')
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"')}"}`,
);
} else if (update.parameters) {
update.parameters.query = {};
}

if (db?.id) {
const result = await updateResource(
db.id as number,
Expand All @@ -287,7 +311,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
if (update?.parameters?.query) {
// convert query params into dictionary
update.parameters.query = JSON.parse(
`{"${decodeURI(db.parameters?.query || '')
`{"${decodeURI((db.parameters?.query as string) || '')
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"')}"}`,
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/views/CRUD/data/database/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type DatabaseObject = {
username?: string;
password?: string;
encryption?: boolean;
query?: string;
query?: string | object;
};
configuration_method: CONFIGURATION_METHOD;
engine?: string;
Expand Down

0 comments on commit ad706d2

Please sign in to comment.