eschutho commented on a change in pull request #11884:
URL:
https://github.com/apache/incubator-superset/pull/11884#discussion_r534613826
##########
File path: superset-frontend/src/views/CRUD/hooks.ts
##########
@@ -294,6 +305,55 @@ export function useSingleViewResource<D extends object =
any>(
});
}, []);
+ const importResource = useCallback(
+ (bundle: File, databasePasswords: Record<string, string> = {}) => {
+ // Set loading state
+ updateState({
+ loading: true,
+ });
+
+ const formData = new FormData();
+ formData.append('formData', bundle);
+
+ /* The import bundle never contains database passwords; if required
+ * they should be provided by the user during import.
+ */
+ if (databasePasswords) {
+ formData.append('passwords', JSON.stringify(databasePasswords));
+ }
+
+ return SupersetClient.post({
+ endpoint: `/api/v1/${resourceName}/import/`,
+ body: formData,
+ })
+ .then(() => true)
+ .catch(response =>
+ getClientErrorObject(response).then(error => {
+ /* When importing a bundle, if all validation errors are because
+ * the databases need passwords we return a list of the database
+ * files so that the user can type in the passwords and resubmit
+ * the file.
+ */
+ const errMsg = error.message || error.error;
+ if (typeof errMsg !== 'string' && needsPassword(errMsg)) {
+ return Object.keys(errMsg);
Review comment:
it looks to me that the common usage is not to return something, but
rather update state. In this case, you could have a state of errMsg for
example, and then check to see that there are no errors.. so something like:
```
.then(
() => {
updateState({
errMsg: [],
});
},
response => {
getClientErrorObject(response).then(error => {
const errMsg = error.message || error.error;
if (typeof errMsg !== 'string' && needsPassword(errMsg)) {
updateState({ errMsg });
return;
}
handleErrorMsg( // you can update this callback to clear the
modal too
t(
'An error occurred while importing %%s: %s',
resourceLabel,
JSON.stringify(errMsg),
),
);
});
},
)
```
and then..
```
importResource(uploadFile, passwords).then(() => {
if (!errMsg.length) {
// Success
addSuccessToast(t('The databases have been imported'));
clearModal();
onDatabaseImport();
return;
if (errMsg.length) {
// Need passwords
setPasswordFields(errMsg);
}
});
```
just some ideas.. :)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]