From a4043779ca1decead8fb3fe03fc6a678c4af5cb6 Mon Sep 17 00:00:00 2001 From: Ricardo Silverio Date: Sat, 21 Sep 2024 21:41:51 -0300 Subject: [PATCH] draftRequests parameter --- .../App/ConfirmAppClose/SaveRequestsModal.js | 45 +++++-------------- .../providers/App/ConfirmAppClose/index.js | 28 +++++++++++- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/packages/bruno-app/src/providers/App/ConfirmAppClose/SaveRequestsModal.js b/packages/bruno-app/src/providers/App/ConfirmAppClose/SaveRequestsModal.js index 9cdca4db..2561aa5d 100644 --- a/packages/bruno-app/src/providers/App/ConfirmAppClose/SaveRequestsModal.js +++ b/packages/bruno-app/src/providers/App/ConfirmAppClose/SaveRequestsModal.js @@ -1,42 +1,19 @@ import React, { useEffect } from 'react'; -import each from 'lodash/each'; -import filter from 'lodash/filter'; -import groupBy from 'lodash/groupBy'; -import { useSelector } from 'react-redux'; import { useDispatch } from 'react-redux'; -import { findCollectionByUid, flattenItems, isItemARequest } from 'utils/collections'; import { pluralizeWord } from 'utils/common'; import { saveMultipleRequests } from 'providers/ReduxStore/slices/collections/actions'; import { IconAlertTriangle } from '@tabler/icons'; import Modal from 'components/Modal'; -const SaveRequestsModal = ({ onConfirm, onClose }) => { +const SaveRequestsModal = ({ onConfirm, onClose, draftRequests = [] }) => { const MAX_UNSAVED_REQUESTS_TO_SHOW = 5; - const currentDrafts = []; - const collections = useSelector((state) => state.collections.collections); - const tabs = useSelector((state) => state.tabs.tabs); const dispatch = useDispatch(); - const tabsByCollection = groupBy(tabs, (t) => t.collectionUid); - Object.keys(tabsByCollection).forEach((collectionUid) => { - const collection = findCollectionByUid(collections, collectionUid); - if (collection) { - const items = flattenItems(collection.items); - const drafts = filter(items, (item) => isItemARequest(item) && item.draft); - each(drafts, (draft) => { - currentDrafts.push({ - ...draft, - collectionUid: collectionUid - }); - }); - } - }); - useEffect(() => { - if (currentDrafts.length === 0) { + if (draftRequests.length === 0) { return dispatch(onConfirm()); } - }, [currentDrafts, dispatch]); + }, [draftRequests, dispatch]); const closeWithoutSave = () => { dispatch(onConfirm()); @@ -44,12 +21,12 @@ const SaveRequestsModal = ({ onConfirm, onClose }) => { }; const closeWithSave = () => { - dispatch(saveMultipleRequests(currentDrafts)) + dispatch(saveMultipleRequests(draftRequests)) .then(() => dispatch(onConfirm())) .then(() => onClose()); }; - if (!currentDrafts.length) { + if (!draftRequests.length) { return null; } @@ -71,11 +48,11 @@ const SaveRequestsModal = ({ onConfirm, onClose }) => {

Do you want to save the changes you made to the following{' '} - {currentDrafts.length} {pluralizeWord('request', currentDrafts.length)}? + {draftRequests.length} {pluralizeWord('request', draftRequests.length)}?

- {currentDrafts.length > MAX_UNSAVED_REQUESTS_TO_SHOW && ( + {draftRequests.length > MAX_UNSAVED_REQUESTS_TO_SHOW && (

- ...{currentDrafts.length - MAX_UNSAVED_REQUESTS_TO_SHOW} additional{' '} - {pluralizeWord('request', currentDrafts.length - MAX_UNSAVED_REQUESTS_TO_SHOW)} not shown + ...{draftRequests.length - MAX_UNSAVED_REQUESTS_TO_SHOW} additional{' '} + {pluralizeWord('request', draftRequests.length - MAX_UNSAVED_REQUESTS_TO_SHOW)} not shown

)} @@ -102,7 +79,7 @@ const SaveRequestsModal = ({ onConfirm, onClose }) => { Cancel diff --git a/packages/bruno-app/src/providers/App/ConfirmAppClose/index.js b/packages/bruno-app/src/providers/App/ConfirmAppClose/index.js index bde28551..31130654 100644 --- a/packages/bruno-app/src/providers/App/ConfirmAppClose/index.js +++ b/packages/bruno-app/src/providers/App/ConfirmAppClose/index.js @@ -1,5 +1,10 @@ import React, { useState, useEffect } from 'react'; import { useDispatch } from 'react-redux'; +import { useSelector } from 'react-redux'; +import { findCollectionByUid, flattenItems, isItemARequest } from 'utils/collections'; +import each from 'lodash/each'; +import filter from 'lodash/filter'; +import groupBy from 'lodash/groupBy'; import SaveRequestsModal from './SaveRequestsModal'; import { isElectron } from 'utils/common/platform'; import { completeQuitFlow } from 'providers/ReduxStore/slices/app'; @@ -8,6 +13,8 @@ const ConfirmAppClose = () => { const { ipcRenderer } = window; const [showConfirmClose, setShowConfirmClose] = useState(false); const dispatch = useDispatch(); + const collections = useSelector((state) => state.collections.collections); + const tabs = useSelector((state) => state.tabs.tabs); useEffect(() => { if (!isElectron()) { @@ -27,7 +34,26 @@ const ConfirmAppClose = () => { return null; } - return setShowConfirmClose(false)} />; + const getAllDraftRequests = () => { + const draftRequests = []; + const tabsByCollection = groupBy(tabs, (t) => t.collectionUid); + Object.keys(tabsByCollection).forEach((collectionUid) => { + const collection = findCollectionByUid(collections, collectionUid); + if (collection) { + const items = flattenItems(collection.items); + const drafts = filter(items, (item) => isItemARequest(item) && item.draft); + each(drafts, (draft) => { + draftRequests.push({ + ...draft, + collectionUid: collectionUid + }); + }); + } + }); + return draftRequests + } + + return setShowConfirmClose(false)} />; }; export default ConfirmAppClose;