|
1 | | -import React from 'react'; |
| 1 | +import React, { useState } from 'react'; |
2 | 2 | import { XMarkIcon } from '@heroicons/react/24/outline'; |
3 | 3 | import { useTabStore } from 'stores/TabStore'; |
| 4 | +import ConfirmActionModal from 'components/molecules/modals/ConfirmActionModal'; |
4 | 5 |
|
5 | 6 | const Tabs = () => { |
6 | 7 | const tabs = useTabStore((state) => state.tabs); |
7 | 8 | const setFocusTab = useTabStore((state) => state.setFocusTab); |
8 | 9 | const focusTabId = useTabStore((state) => state.focusTabId); |
9 | 10 | const focusTab = tabs.find((t) => t.id === focusTabId); |
| 11 | + const [confirmActionModalOpen, setConfirmActionModalOpen] = useState(false); |
10 | 12 | const closeTab = useTabStore((state) => state.closeTab); |
| 13 | + const [closingTabId, setClosingTabId] = useState(''); |
| 14 | + const [closingCollectionId, setClosingCollectionId] = useState(''); |
11 | 15 | const activeTabStyles = |
12 | 16 | 'before:absolute before:h-[0.25rem] before:w-full before:bg-slate-300 before:content-[""] before:bottom-0 before:left-0'; |
13 | 17 | const tabCommonStyles = |
14 | 18 | 'tab flex items-center gap-x-2 border-r border-neutral-300 bg-transparent pr-0 tracking-[0.15em] transition duration-500 ease-in text-sm'; |
| 19 | + const messageForConfirmActionModal = 'You have unsaved changes in the flowtest, are you sure you want to close it?'; |
| 20 | + const handleCloseTab = (event) => { |
| 21 | + event.stopPropagation(); |
| 22 | + event.preventDefault(); |
| 23 | + const tabId = event.currentTarget.dataset.tabId; |
| 24 | + const { isDirty, collectionId } = tabs.find((tab) => { |
| 25 | + if (tab.id === tabId) return tab; |
| 26 | + }); |
| 27 | + setClosingTabId(tabId); |
| 28 | + setClosingCollectionId(collectionId); |
| 29 | + |
| 30 | + if (isDirty) { |
| 31 | + console.log(`\n \n BHUT HI SAHIIII tabId: ${tabId} : collectionId: ${collectionId}\n \n`); |
| 32 | + setConfirmActionModalOpen(true); |
| 33 | + return; |
| 34 | + } |
| 35 | + closeTab(tabId, collectionId); |
| 36 | + }; |
15 | 37 | return ( |
16 | 38 | <div role='tablist' className='tabs tabs-lg'> |
17 | 39 | {tabs |
18 | 40 | // tabs belonging to one collection will be shown at a time |
19 | 41 | .filter((t) => t.collectionId === focusTab.collectionId) |
20 | 42 | .map((tab, index) => { |
21 | 43 | return ( |
22 | | - <> |
23 | | - <a |
24 | | - role='tab' |
25 | | - className={`${tabCommonStyles} ${focusTabId === tab.id ? activeTabStyles : ''}`} |
26 | | - key={index} |
27 | | - data-id={tab.id} |
28 | | - data-collection-id={tab.collectionId} |
29 | | - onClick={() => { |
30 | | - setFocusTab(tab.id); |
31 | | - console.log(`CLICKED THE ${tab.id}`); |
32 | | - }} |
33 | | - > |
34 | | - {tab.name} |
35 | | - </a> |
| 44 | + <div |
| 45 | + key={index} |
| 46 | + className={`${tabCommonStyles} ${focusTabId === tab.id ? activeTabStyles : ''}`} |
| 47 | + role='tab' |
| 48 | + onClick={() => { |
| 49 | + setFocusTab(tab.id); |
| 50 | + console.log(`CLICKED THE ${tab.id}`); |
| 51 | + }} |
| 52 | + data-id={tab.id} |
| 53 | + data-collection-id={tab.collectionId} |
| 54 | + > |
| 55 | + <a>{tab.name}</a> |
36 | 56 | {/* close needs to be a separate clickable component other wise it gets confused with above */} |
37 | 57 | <div |
38 | | - className='flex h-full items-center px-2 hover:rounded hover:rounded-l-none hover:bg-slate-200' |
39 | | - onClick={() => { |
40 | | - closeTab(tab.id, tab.collectionId); |
41 | | - }} |
| 58 | + className='flex items-center h-full px-2 hover:rounded hover:rounded-l-none hover:bg-slate-200' |
| 59 | + data-tab-id={tab.id} |
| 60 | + onClick={handleCloseTab} |
42 | 61 | > |
43 | | - <XMarkIcon className='h-4 w-4' /> |
| 62 | + <XMarkIcon className='w-4 h-4' /> |
44 | 63 | </div> |
45 | | - </> |
| 64 | + </div> |
46 | 65 | ); |
47 | 66 | })} |
| 67 | + <ConfirmActionModal |
| 68 | + closeFn={() => setConfirmActionModalOpen(false)} |
| 69 | + open={confirmActionModalOpen} |
| 70 | + message={messageForConfirmActionModal} |
| 71 | + actionFn={() => { |
| 72 | + closeTab(closingTabId, closingCollectionId); |
| 73 | + setConfirmActionModalOpen(false); |
| 74 | + }} |
| 75 | + /> |
48 | 76 | </div> |
49 | 77 | ); |
50 | 78 | }; |
|
0 commit comments