draftRequests parameter

This commit is contained in:
Ricardo Silverio 2024-09-21 21:41:51 -03:00
parent 273f3e19ab
commit a4043779ca
2 changed files with 38 additions and 35 deletions

View File

@ -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 }) => {
</div>
<p className="mt-4">
Do you want to save the changes you made to the following{' '}
<span className="font-medium">{currentDrafts.length}</span> {pluralizeWord('request', currentDrafts.length)}?
<span className="font-medium">{draftRequests.length}</span> {pluralizeWord('request', draftRequests.length)}?
</p>
<ul className="mt-4">
{currentDrafts.slice(0, MAX_UNSAVED_REQUESTS_TO_SHOW).map((item) => {
{draftRequests.slice(0, MAX_UNSAVED_REQUESTS_TO_SHOW).map((item) => {
return (
<li key={item.uid} className="mt-1 text-xs">
{item.filename}
@ -84,10 +61,10 @@ const SaveRequestsModal = ({ onConfirm, onClose }) => {
})}
</ul>
{currentDrafts.length > MAX_UNSAVED_REQUESTS_TO_SHOW && (
{draftRequests.length > MAX_UNSAVED_REQUESTS_TO_SHOW && (
<p className="mt-1 text-xs">
...{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
</p>
)}
@ -102,7 +79,7 @@ const SaveRequestsModal = ({ onConfirm, onClose }) => {
Cancel
</button>
<button className="btn btn-secondary btn-sm" onClick={closeWithSave}>
{currentDrafts.length > 1 ? 'Save All' : 'Save'}
{draftRequests.length > 1 ? 'Save All' : 'Save'}
</button>
</div>
</div>

View File

@ -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 <SaveRequestsModal onConfirm={completeQuitFlow} onClose={() => 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 <SaveRequestsModal draftRequests={getAllDraftRequests()} onConfirm={completeQuitFlow} onClose={() => setShowConfirmClose(false)} />;
};
export default ConfirmAppClose;