hughhhh commented on a change in pull request #11884: URL: https://github.com/apache/incubator-superset/pull/11884#discussion_r537716743
########## File path: superset-frontend/src/database/components/ImportModal/index.tsx ########## @@ -0,0 +1,191 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import React, { FunctionComponent, useEffect, useRef, useState } from 'react'; +import { t } from '@superset-ui/core'; + +import Modal from 'src/common/components/Modal'; +import { + StyledIcon, + StyledInputContainer, +} from 'src/views/CRUD/data/database/DatabaseModal'; +import { useImportResource } from 'src/views/CRUD/hooks'; +import { DatabaseObject } from 'src/views/CRUD/data/database/types'; + +export interface ImportDatabaseModalProps { + addDangerToast: (msg: string) => void; + addSuccessToast: (msg: string) => void; + onDatabaseImport: () => void; + show: boolean; + onHide: () => void; + passwordFields?: string[]; + setPasswordFields?: (passwordFields: string[]) => void; +} + +const ImportDatabaseModal: FunctionComponent<ImportDatabaseModalProps> = ({ + addDangerToast, + addSuccessToast, + onDatabaseImport, + show, + onHide, + passwordFields = [], + setPasswordFields = () => {}, +}) => { + const [uploadFile, setUploadFile] = useState<File | null>(null); + const [isHidden, setIsHidden] = useState<boolean>(true); + const [passwords, setPasswords] = useState<Record<string, string>>({}); + const fileInputRef = useRef<HTMLInputElement>(null); + + const clearModal = () => { + setUploadFile(null); + setPasswords({}); + setPasswordFields([]); + if (fileInputRef && fileInputRef.current) { + fileInputRef.current.value = ''; + } + }; + + const handleErrorMsg = (msg: string) => { + clearModal(); + addDangerToast(msg); + }; + + const { + state: { passwordsNeeded }, + importResource, + } = useImportResource<DatabaseObject>( + 'database', + t('database'), + handleErrorMsg, + ); + + useEffect(() => { + setPasswordFields(passwordsNeeded); + }, [passwordsNeeded]); + + // Functions + const hide = () => { + setIsHidden(true); + onHide(); + }; + + const onUpload = () => { + if (uploadFile === null) { + return; + } + + importResource(uploadFile, passwords).then(result => { + if (result) { + addSuccessToast(t('The databases have been imported')); + clearModal(); + onDatabaseImport(); + } + }); + }; + + const changeFile = (event: React.ChangeEvent<HTMLInputElement>) => { + const { files } = event.target as HTMLInputElement; + setUploadFile((files && files[0]) || null); + }; + + const renderPasswordFields = () => { + if (passwordFields.length === 0) { + return null; + } + + return ( + <> + <h5>Passwords</h5> + <StyledInputContainer> + <div className="helper"> + {t('Please provide the password for the databases below')} + </div> + </StyledInputContainer> + {passwordFields.map(fileName => ( + <StyledInputContainer key={`password-for-${fileName}`}> + <div className="control-label"> + {fileName} + <span className="required">*</span> + </div> + <input + name={`password-${fileName}`} + autoComplete="off" + type="password" + value={passwords[fileName]} + onChange={event => + setPasswords({ ...passwords, [fileName]: event.target.value }) Review comment: nit: put this in a handleStyleContainerInputChange function ---------------------------------------------------------------- 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]
