-
Notifications
You must be signed in to change notification settings - Fork 14.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: clear modal state after adding dataset #17044
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import { isEmpty, isNil } from 'lodash'; | |
import Modal from 'src/components/Modal'; | ||
import TableSelector from 'src/components/TableSelector'; | ||
import withToasts from 'src/components/MessageToasts/withToasts'; | ||
import { DatabaseObject } from 'src/components/DatabaseSelector'; | ||
|
||
type DatasetAddObject = { | ||
id: number; | ||
|
@@ -50,9 +51,11 @@ const DatasetModal: FunctionComponent<DatasetModalProps> = ({ | |
onHide, | ||
show, | ||
}) => { | ||
const [currentDatabase, setCurrentDatabase] = useState< | ||
DatabaseObject | undefined | ||
>(); | ||
const [currentSchema, setSchema] = useState<string | undefined>(''); | ||
const [currentTableName, setTableName] = useState(''); | ||
const [datasourceId, setDatasourceId] = useState<number>(0); | ||
const [disableSave, setDisableSave] = useState(true); | ||
const { createResource } = useSingleViewResource<Partial<DatasetAddObject>>( | ||
'dataset', | ||
|
@@ -61,15 +64,11 @@ const DatasetModal: FunctionComponent<DatasetModalProps> = ({ | |
); | ||
|
||
useEffect(() => { | ||
setDisableSave(isNil(datasourceId) || isEmpty(currentTableName)); | ||
}, [currentTableName, datasourceId]); | ||
setDisableSave(isNil(currentDatabase) || isEmpty(currentTableName)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can get rid of |
||
}, [currentTableName, currentDatabase]); | ||
|
||
const onDbChange = (db: { | ||
id: number; | ||
database_name: string; | ||
backend: string; | ||
}) => { | ||
setDatasourceId(db.id); | ||
const onDbChange = (db: DatabaseObject) => { | ||
setCurrentDatabase(db); | ||
}; | ||
|
||
const onSchemaChange = (schema?: string) => { | ||
|
@@ -80,9 +79,21 @@ const DatasetModal: FunctionComponent<DatasetModalProps> = ({ | |
setTableName(tableName); | ||
}; | ||
|
||
const clearModal = () => { | ||
setSchema(''); | ||
setTableName(''); | ||
setCurrentDatabase(undefined); | ||
setDisableSave(true); | ||
}; | ||
|
||
const hide = () => { | ||
clearModal(); | ||
onHide(); | ||
}; | ||
|
||
const onSave = () => { | ||
const data = { | ||
database: datasourceId, | ||
database: currentDatabase?.id || 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does setting the default to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me clean this up... here |
||
...(currentSchema ? { schema: currentSchema } : {}), | ||
table_name: currentTableName, | ||
}; | ||
|
@@ -94,30 +105,30 @@ const DatasetModal: FunctionComponent<DatasetModalProps> = ({ | |
onDatasetAdd({ id: response.id, ...response }); | ||
} | ||
addSuccessToast(t('The dataset has been saved')); | ||
onHide(); | ||
hide(); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Modal | ||
disablePrimaryButton={disableSave} | ||
onHandledPrimaryAction={onSave} | ||
onHide={onHide} | ||
onHide={hide} | ||
primaryButtonName={t('Add')} | ||
show={show} | ||
title={t('Add dataset')} | ||
> | ||
<TableSelectorContainer> | ||
<TableSelector | ||
clearable={false} | ||
dbId={datasourceId} | ||
formMode | ||
handleError={addDangerToast} | ||
database={currentDatabase} | ||
schema={currentSchema} | ||
tableName={currentTableName} | ||
onDbChange={onDbChange} | ||
onSchemaChange={onSchemaChange} | ||
onTableChange={onTableChange} | ||
schema={currentSchema} | ||
tableName={currentTableName} | ||
handleError={addDangerToast} | ||
/> | ||
</TableSelectorContainer> | ||
</Modal> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this ever be null? Do you want to check a more generic falsy value iow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I'll check for any falsy values.