From fdf5f66430e318bcd6a45a3d69849b7bd83d286f Mon Sep 17 00:00:00 2001 From: Jarod Gowgiel Date: Sun, 1 Dec 2024 17:57:31 -0700 Subject: [PATCH 1/2] Add a new UI check for whether an existing collection should be updated Introduces a new modal with two simple options that sets a new flag in the collection import flow. This will be connected in future commits to trigger alternative import logic that will update an existing collection instead of creating a net-new collection. --- .../ConfirmCollectionImportUpdate/index.js | 24 +++ .../src/components/Sidebar/TitleBar/index.js | 56 ++++++- .../bruno-app/src/components/Welcome/index.js | 56 ++++++- .../ReduxStore/slices/collections/actions.js | 91 +++++----- packages/bruno-electron/src/ipc/collection.js | 155 +++++++++--------- 5 files changed, 254 insertions(+), 128 deletions(-) create mode 100644 packages/bruno-app/src/components/ConfirmCollectionImportUpdate/index.js 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}