diff --git a/packages/bruno-app/src/components/ConfirmCollectionImportUpdate/index.js b/packages/bruno-app/src/components/ConfirmCollectionImportUpdate/index.js new file mode 100644 index 0000000000..9639b7020c --- /dev/null +++ b/packages/bruno-app/src/components/ConfirmCollectionImportUpdate/index.js @@ -0,0 +1,24 @@ +import Modal from 'components/Modal'; +import Portal from 'components/Portal/index'; + +const ConfirmCollectionImportUpdate = ({ onConfirm, onCancel }) => { + return ( + + +
Would you like to add to the existing collection at this location?
+
+
+ ); +}; + +export default ConfirmCollectionImportUpdate; diff --git a/packages/bruno-app/src/components/Sidebar/TitleBar/index.js b/packages/bruno-app/src/components/Sidebar/TitleBar/index.js index bbd88fbcf2..0fad39502a 100644 --- a/packages/bruno-app/src/components/Sidebar/TitleBar/index.js +++ b/packages/bruno-app/src/components/Sidebar/TitleBar/index.js @@ -11,13 +11,16 @@ import { useDispatch } from 'react-redux'; import { showHomePage } from 'providers/ReduxStore/slices/app'; import { openCollection, importCollection } from 'providers/ReduxStore/slices/collections/actions'; import StyledWrapper from './StyledWrapper'; +import ConfirmCollectionImportUpdate from 'components/ConfirmCollectionImportUpdate/index'; const TitleBar = () => { const [importedCollection, setImportedCollection] = useState(null); + const [importedCollectionLocation, setImportedCollectionLocation] = useState(null); const [importedTranslationLog, setImportedTranslationLog] = useState({}); const [createCollectionModalOpen, setCreateCollectionModalOpen] = useState(false); const [importCollectionModalOpen, setImportCollectionModalOpen] = useState(false); const [importCollectionLocationModalOpen, setImportCollectionLocationModalOpen] = useState(false); + const [confirmCollectionImportUpdateModalOpen, setConfirmCollectionImportUpdateModalOpen] = useState(false); const dispatch = useDispatch(); const { ipcRenderer } = window; @@ -30,20 +33,55 @@ const TitleBar = () => { setImportCollectionLocationModalOpen(true); }; + const cleanupAfterSuccessfulImport = () => { + setImportCollectionLocationModalOpen(false); + setImportedCollection(null); + setImportedCollectionLocation(null); + toast.success('Collection imported successfully'); + }; + + const cleanupAndShowImportError = (err) => { + setImportCollectionLocationModalOpen(false); + setImportedCollection(null); + setImportedCollectionLocation(null); + console.error(err); + toast.error('An error occurred while importing the collection. Check the logs for more information.'); + }; + const handleImportCollectionLocation = (collectionLocation) => { + setImportedCollectionLocation(collectionLocation); + setImportCollectionLocationModalOpen(false); dispatch(importCollection(importedCollection, collectionLocation)) .then(() => { - setImportCollectionLocationModalOpen(false); - setImportedCollection(null); - toast.success('Collection imported successfully'); + cleanupAfterSuccessfulImport(); }) .catch((err) => { - setImportCollectionLocationModalOpen(false); - console.error(err); - toast.error('An error occurred while importing the collection. Check the logs for more information.'); + // Note: the string here must exactly match the start of the error thrown in + // `bruno-electron/src/ipc/collection.js` when the folder already exists + if (err instanceof Error && err.message.includes('collection already exists')) { + setConfirmCollectionImportUpdateModalOpen(true); + } else { + cleanupAndShowImportError(err); + } }); }; + const handleConfirmCollectionImportUpdate = (shouldUpdate) => { + setConfirmCollectionImportUpdateModalOpen(false); + setImportCollectionLocationModalOpen(false); + if (shouldUpdate) { + dispatch(importCollection(importedCollection, importedCollectionLocation, true)) + .then(() => { + cleanupAfterSuccessfulImport(); + }) + .catch((err) => { + cleanupAndShowImportError(err); + }); + } else { + cleanupAndShowImportError(); + } + }; + const menuDropdownTippyRef = useRef(); const onMenuDropdownCreate = (ref) => (menuDropdownTippyRef.current = ref); const MenuIcon = forwardRef((props, ref) => { @@ -80,6 +118,12 @@ const TitleBar = () => { handleSubmit={handleImportCollectionLocation} /> ) : null} + {confirmCollectionImportUpdateModalOpen ? ( + handleConfirmCollectionImportUpdate(true)} + onCancel={() => handleConfirmCollectionImportUpdate(false)} + /> + ) : null}