Skip to content

Commit

Permalink
fix(import): Show the error message with db install guide when exist …
Browse files Browse the repository at this point in the history
…the importing db issue (#20573)

* fix(import): make to update the error display when importing resurce

* fix(import): make to update the error message
  • Loading branch information
prosdev0107 authored Jul 6, 2022
1 parent db088e9 commit c992ff3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const ErrorAlert: FunctionComponent<IProps> = ({
<>
<br />
{t(
'Database driver for importing maybe not installed. Visit the Superset documentation page for installation instructions:',
'Database driver for importing maybe not installed. Visit the Superset documentation page for installation instructions: ',
)}
<a
href={DOCUMENTATION_LINK}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import IconButton from 'src/components/IconButton';
import InfoTooltip from 'src/components/InfoTooltip';
import withToasts from 'src/components/MessageToasts/withToasts';
import ValidatedInput from 'src/components/Form/LabeledErrorBoundInput';
import ErrorAlert from 'src/components/ImportModal/ErrorAlert';
import {
testDatabaseConnection,
useSingleViewResource,
Expand Down Expand Up @@ -452,6 +453,8 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
const [confirmedOverwrite, setConfirmedOverwrite] = useState<boolean>(false);
const [fileList, setFileList] = useState<UploadFile[]>([]);
const [importingModal, setImportingModal] = useState<boolean>(false);
const [importingErrorMessage, setImportingErrorMessage] = useState<string>();
const [passwordFields, setPasswordFields] = useState<string[]>([]);

const conf = useCommonConf();
const dbImages = getDatabaseImages();
Expand Down Expand Up @@ -522,6 +525,8 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
setEditNewDb(false);
setFileList([]);
setImportingModal(false);
setImportingErrorMessage('');
setPasswordFields([]);
setPasswords({});
setConfirmedOverwrite(false);
onHide();
Expand All @@ -537,8 +542,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
},
importResource,
} = useImportResource('database', t('database'), msg => {
addDangerToast(msg);
onClose();
setImportingErrorMessage(msg);
});

const onChange = (type: any, payload: any) => {
Expand Down Expand Up @@ -810,6 +814,12 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
const handleBackButtonOnConnect = () => {
if (editNewDb) setHasConnectedDb(false);
if (importingModal) setImportingModal(false);
if (importErrored) {
setImportingModal(false);
setImportingErrorMessage('');
setPasswordFields([]);
setPasswords({});
}
setDB({ type: ActionType.reset });
setFileList([]);
};
Expand Down Expand Up @@ -970,7 +980,14 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
}
}, [importingModal]);

useEffect(() => {
setPasswordFields([...passwordsNeeded]);
}, [passwordsNeeded]);

const onDbImport = async (info: UploadChangeParam) => {
setImportingErrorMessage('');
setPasswordFields([]);
setPasswords({});
setImportingModal(true);
setFileList([
{
Expand All @@ -989,9 +1006,9 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
};

const passwordNeededField = () => {
if (!passwordsNeeded.length) return null;
if (!passwordFields.length) return null;

return passwordsNeeded.map(database => (
return passwordFields.map(database => (
<>
<StyledAlertMargin>
<Alert
Expand Down Expand Up @@ -1022,6 +1039,19 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
));
};

const importingErrorAlert = () => {
if (!importingErrorMessage) return null;

return (
<StyledAlertMargin>
<ErrorAlert
errorMessage={importingErrorMessage}
showDbInstallInstructions={passwordFields.length > 0}
/>
</StyledAlertMargin>
);
};

const confirmOverwrite = (event: React.ChangeEvent<HTMLInputElement>) => {
const targetValue = (event.currentTarget?.value as string) ?? '';
setConfirmedOverwrite(targetValue.toUpperCase() === t('OVERWRITE'));
Expand Down Expand Up @@ -1225,7 +1255,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
);
};

if (fileList.length > 0 && (alreadyExists.length || passwordsNeeded.length)) {
if (fileList.length > 0 && (alreadyExists.length || passwordFields.length)) {
return (
<Modal
css={(theme: SupersetTheme) => [
Expand Down Expand Up @@ -1256,6 +1286,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
/>
{passwordNeededField()}
{confirmOverwriteField()}
{importingErrorAlert()}
</Modal>
);
}
Expand Down Expand Up @@ -1519,6 +1550,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
</Button>
</Upload>
</StyledUploadWrapper>
{importingErrorAlert()}
</SelectDatabaseStyles>
) : (
<>
Expand Down
4 changes: 2 additions & 2 deletions superset-frontend/src/views/CRUD/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ export function useImportResource(
resourceLabel,
[
...error.errors.map(payload => payload.message),
t('Please re-export your file and try importing again'),
].join('\n'),
t('Please re-export your file and try importing again.'),
].join('.\n'),
),
);
} else {
Expand Down

0 comments on commit c992ff3

Please sign in to comment.