import React, { useMemo } from 'react'; import filter from 'lodash/filter'; import { useDispatch, useSelector } from 'react-redux'; import { flattenItems, isItemARequest, hasRequestChanges, findCollectionByUid } from 'utils/collections'; import { pluralizeWord } from 'utils/common'; import { saveRequest, saveMultipleRequests } from 'providers/ReduxStore/slices/collections/actions'; import { deleteRequestDraft } from 'providers/ReduxStore/slices/collections'; import { removeCollection } from 'providers/ReduxStore/slices/collections/actions'; import { IconAlertTriangle, IconDeviceFloppy } from '@tabler/icons'; import Modal from 'components/Modal'; import toast from 'react-hot-toast'; import Button from 'ui/Button'; const MAX_UNSAVED_REQUESTS_TO_SHOW = 5; const ConfirmCollectionCloseDrafts = ({ onClose, collection, collectionUid }) => { const dispatch = useDispatch(); const latestCollection = useSelector((state) => findCollectionByUid(state.collections.collections, collectionUid)); const activeCollection = latestCollection || collection; const currentDrafts = useMemo(() => { if (!activeCollection) return []; const items = flattenItems(activeCollection.items); return items ?.filter((item) => isItemARequest(item) && hasRequestChanges(item) && !item.isTransient) .map((item) => { return { ...item, collectionUid: collectionUid }; }); }, [activeCollection, collectionUid]); const currentTransientDrafts = useMemo(() => { if (!activeCollection) return []; const items = flattenItems(activeCollection.items); return items ?.filter((item) => isItemARequest(item) && hasRequestChanges(item) && item.isTransient) .map((item) => { return { ...item, collectionUid: collectionUid }; }); }, [activeCollection, collectionUid]); const allDrafts = useMemo(() => { return [...currentDrafts, ...currentTransientDrafts]; }, [currentDrafts, currentTransientDrafts]); const handleSaveAll = () => { // If there are transient drafts, we can't proceed with batch save if (currentTransientDrafts.length > 0) { toast.error('Please save or discard transient requests first'); return; } // Save only non-transient drafts if (currentDrafts.length > 0) { dispatch(saveMultipleRequests(currentDrafts)) .then(() => { dispatch(removeCollection(collectionUid)) .then(() => { toast.success('Collection removed from workspace'); onClose(); }) .catch(() => toast.error('An error occurred while removing the collection')); }) .catch(() => { toast.error('Failed to save requests!'); }); } else { // No non-transient drafts, just remove the collection dispatch(removeCollection(collectionUid)) .then(() => { toast.success('Collection removed from workspace'); onClose(); }) .catch(() => toast.error('An error occurred while removing the collection')); } }; const handleDiscardAll = () => { // Discard all drafts (both regular and transient) allDrafts.forEach((draft) => { dispatch(deleteRequestDraft({ collectionUid: collectionUid, itemUid: draft.uid })); }); // Then remove the collection dispatch(removeCollection(collectionUid)) .then(() => { toast.success('Collection removed from workspace'); onClose(); }) .catch(() => toast.error('An error occurred while removing the collection')); }; const handleSaveTransient = (draft) => { dispatch(saveRequest(draft.uid, collectionUid)); }; if (!currentDrafts.length && !currentTransientDrafts.length) { return null; } return (

Hold on..

You have unsaved changes in {allDrafts.length}{' '} {pluralizeWord('request', allDrafts.length)}.

{/* Regular (saved) requests with changes */} {currentDrafts.length > 0 && (

Saved {pluralizeWord('Request', currentDrafts.length)} ({currentDrafts.length})

{currentDrafts.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

)}
)} {/* Transient (unsaved) requests */} {currentTransientDrafts.length > 0 && (

Transient {pluralizeWord('Request', currentTransientDrafts.length)} ({currentTransientDrafts.length})

These requests need to be saved individually before closing the collection.

{currentTransientDrafts.map((item) => { return (
{item.name}
); })}
)}
); }; export default ConfirmCollectionCloseDrafts;