From b934e6ee96e673b512e17b5c033cd0310ee131c2 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Thu, 3 Dec 2020 19:13:27 -0800 Subject: [PATCH] Use new hook --- .../chart/components/ImportModal/index.tsx | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/superset-frontend/src/chart/components/ImportModal/index.tsx b/superset-frontend/src/chart/components/ImportModal/index.tsx index 9340dc9676ae1..328af1885f603 100644 --- a/superset-frontend/src/chart/components/ImportModal/index.tsx +++ b/superset-frontend/src/chart/components/ImportModal/index.tsx @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -import React, { FunctionComponent, useRef, useState } from 'react'; +import React, { FunctionComponent, useEffect, useRef, useState } from 'react'; import { t } from '@superset-ui/core'; import Modal from 'src/common/components/Modal'; @@ -24,7 +24,7 @@ import { StyledIcon, StyledInputContainer, } from 'src/views/CRUD/data/database/DatabaseModal'; -import { useSingleViewResource } from 'src/views/CRUD/hooks'; +import { useImportResource } from 'src/views/CRUD/hooks'; import { ChartObject } from 'src/views/CRUD/chart/types'; export interface ImportChartModalProps { @@ -51,18 +51,6 @@ const ImportChartModal: FunctionComponent = ({ const [passwords, setPasswords] = useState>({}); const fileInputRef = useRef(null); - const { importResource } = useSingleViewResource( - 'chart', - t('chart'), - addDangerToast, - ); - - // Functions - const hide = () => { - setIsHidden(true); - onHide(); - }; - const clearModal = () => { setUploadFile(null); setPasswordFields([]); @@ -72,23 +60,36 @@ const ImportChartModal: FunctionComponent = ({ } }; + const handleErrorMsg = (msg: string) => { + clearModal(); + addDangerToast(msg); + }; + + const { + state: { passwordsNeeded }, + importResource, + } = useImportResource('chart', t('chart'), handleErrorMsg); + + useEffect(() => { + setPasswordFields(passwordsNeeded); + }, [passwordsNeeded]); + + // Functions + const hide = () => { + setIsHidden(true); + onHide(); + }; + const onUpload = () => { if (uploadFile === null) { return; } importResource(uploadFile, passwords).then(result => { - if (result === true) { - // Success + if (result) { addSuccessToast(t('The charts have been imported')); clearModal(); onChartImport(); - } else if (result) { - // Need passwords - setPasswordFields(result); - } else { - // Failure - clearModal(); } }); }; @@ -125,6 +126,7 @@ const ImportChartModal: FunctionComponent = ({