Skip to content

Commit

Permalink
refactor: serialize extra json in state (#21523)
Browse files Browse the repository at this point in the history
  • Loading branch information
eschutho authored Oct 14, 2022
1 parent df3b5a8 commit 196c367
Show file tree
Hide file tree
Showing 4 changed files with 534 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
antdCollapseStyles,
no_margin_bottom,
} from './styles';
import { DatabaseObject } from '../types';
import { DatabaseObject, ExtraJson } from '../types';

const ExtraOptions = ({
db,
Expand All @@ -50,6 +50,7 @@ const ExtraOptions = ({
const createAsOpen = !!(db?.allow_ctas || db?.allow_cvas);
const isFileUploadSupportedByEngine =
db?.engine_information?.supports_file_upload;
const extraJson: ExtraJson = JSON.parse(db?.extra || '{}');

return (
<Collapse
Expand Down Expand Up @@ -155,7 +156,7 @@ const ExtraOptions = ({
<IndeterminateCheckbox
id="cost_estimate_enabled"
indeterminate={false}
checked={!!db?.extra_json?.cost_estimate_enabled}
checked={!!extraJson?.cost_estimate_enabled}
onChange={onExtraInputChange}
labelText={t('Enable query cost estimation')}
/>
Expand All @@ -171,7 +172,7 @@ const ExtraOptions = ({
<IndeterminateCheckbox
id="allows_virtual_table_explore"
indeterminate={false}
checked={!!db?.extra_json?.allows_virtual_table_explore}
checked={!!extraJson?.allows_virtual_table_explore}
onChange={onExtraInputChange}
labelText={t('Allow this database to be explored')}
/>
Expand All @@ -187,7 +188,7 @@ const ExtraOptions = ({
<IndeterminateCheckbox
id="disable_data_preview"
indeterminate={false}
checked={!!db?.extra_json?.disable_data_preview}
checked={!!extraJson?.disable_data_preview}
onChange={onExtraInputChange}
labelText={t('Disable SQL Lab data preview queries')}
/>
Expand Down Expand Up @@ -240,8 +241,7 @@ const ExtraOptions = ({
type="number"
name="schema_cache_timeout"
value={
db?.extra_json?.metadata_cache_timeout?.schema_cache_timeout ||
''
extraJson?.metadata_cache_timeout?.schema_cache_timeout || ''
}
placeholder={t('Enter duration in seconds')}
onChange={onExtraInputChange}
Expand All @@ -262,8 +262,7 @@ const ExtraOptions = ({
type="number"
name="table_cache_timeout"
value={
db?.extra_json?.metadata_cache_timeout?.table_cache_timeout ||
''
extraJson?.metadata_cache_timeout?.table_cache_timeout || ''
}
placeholder={t('Enter duration in seconds')}
onChange={onExtraInputChange}
Expand Down Expand Up @@ -301,7 +300,7 @@ const ExtraOptions = ({
<IndeterminateCheckbox
id="cancel_query_on_windows_unload"
indeterminate={false}
checked={!!db?.extra_json?.cancel_query_on_windows_unload}
checked={!!extraJson?.cancel_query_on_windows_unload}
onChange={onExtraInputChange}
labelText={t('Cancel query on window unload event')}
/>
Expand Down Expand Up @@ -414,9 +413,9 @@ const ExtraOptions = ({
<input
type="text"
name="schemas_allowed_for_file_upload"
value={(
db?.extra_json?.schemas_allowed_for_file_upload || []
).join(',')}
value={(extraJson?.schemas_allowed_for_file_upload || []).join(
',',
)}
placeholder="schema1,schema2"
onChange={onExtraInputChange}
/>
Expand All @@ -443,7 +442,11 @@ const ExtraOptions = ({
<div className="input-container">
<StyledJsonEditor
name="metadata_params"
value={db?.extra_json?.metadata_params || ''}
value={
!Object.keys(extraJson?.metadata_params || {}).length
? ''
: extraJson?.metadata_params
}
placeholder={t('Metadata Parameters')}
onChange={(json: string) =>
onExtraEditorChange({ json, name: 'metadata_params' })
Expand All @@ -465,7 +468,11 @@ const ExtraOptions = ({
<div className="input-container">
<StyledJsonEditor
name="engine_params"
value={db?.extra_json?.engine_params || ''}
value={
!Object.keys(extraJson?.engine_params || {}).length
? ''
: JSON.stringify(extraJson?.engine_params)
}
placeholder={t('Engine Parameters')}
onChange={(json: string) =>
onExtraEditorChange({ json, name: 'engine_params' })
Expand All @@ -490,7 +497,7 @@ const ExtraOptions = ({
<input
type="number"
name="version"
value={db?.extra_json?.version || ''}
value={extraJson?.version || ''}
placeholder={t('Version number')}
onChange={onExtraInputChange}
/>
Expand Down
Loading

0 comments on commit 196c367

Please sign in to comment.