11import React , { useEffect , useRef , useState } from "react" ;
22import { useTranslation } from "react-i18next" ;
3- import cn from "classnames" ;
43import { useLocation } from "react-router" ;
54import TableFilters from "../shared/TableFilters" ;
65import Stats from "../shared/Stats" ;
@@ -36,14 +35,11 @@ import {
3635} from "../../slices/eventSlice" ;
3736import EventDetailsModal from "./partials/modals/EventDetailsModal" ;
3837import { showModal } from "../../selectors/eventDetailsSelectors" ;
39- import ButtonLikeAnchor from "../shared/ButtonLikeAnchor" ;
4038import { eventsLinks } from "./partials/EventsNavigation" ;
4139import { Modal , ModalHandle } from "../shared/modals/Modal" ;
40+ import TableActionDropdown from "../shared/TableActionDropdown" ;
4241import { resetTableProperties } from "../../slices/tableSlice" ;
4342
44- // References for detecting a click outside of the container of the dropdown menu
45- const containerAction = React . createRef < HTMLDivElement > ( ) ;
46-
4743/**
4844 * This component renders the table view of events
4945 */
@@ -53,7 +49,6 @@ const Events = () => {
5349
5450 const displayEventDetailsModal = useAppSelector ( state => showModal ( state ) ) ;
5551
56- const [ displayActionMenu , setActionMenu ] = useState ( false ) ;
5752 const [ displayNavigation , setNavigation ] = useState ( false ) ;
5853 const newEventModalRef = useRef < ModalHandle > ( null ) ;
5954 const startTaskModalRef = useRef < ModalHandle > ( null ) ;
@@ -96,35 +91,16 @@ const Events = () => {
9691 // call the function
9792 loadEvents ( ) ;
9893
99- // Function for handling clicks outside of an open dropdown menu
100- const handleClickOutside = ( e : MouseEvent ) => {
101- if (
102- containerAction . current &&
103- ! containerAction . current . contains ( e . target as Node )
104- ) {
105- setActionMenu ( false ) ;
106- }
107- } ;
108-
10994 // Fetch events every five seconds
11095 let fetchEventsInterval = setInterval ( ( ) => loadEvents ( ) , 5000 ) ;
11196
112- // Event listener for handle a click outside of dropdown menu
113- window . addEventListener ( "mousedown" , handleClickOutside ) ;
114-
11597 return ( ) => {
11698 allowLoadIntoTable = false ;
117- window . removeEventListener ( "mousedown" , handleClickOutside ) ;
11899 clearInterval ( fetchEventsInterval ) ;
119100 } ;
120101 // eslint-disable-next-line react-hooks/exhaustive-deps
121102 } , [ location . hash ] ) ;
122103
123- const handleActionMenu = ( e : React . MouseEvent ) => {
124- e . preventDefault ( ) ;
125- setActionMenu ( ! displayActionMenu ) ;
126- } ;
127-
128104 const onNewEventModal = async ( ) => {
129105 await dispatch ( fetchEventMetadata ( ) ) ;
130106 await dispatch ( fetchAssetUploadOptions ( ) ) ;
@@ -215,48 +191,31 @@ const Events = () => {
215191
216192 < div className = "controls-container" >
217193 < div className = "filters-container" >
218- < div
219- className = { cn ( "drop-down-container" , { disabled : ! showActions } ) }
220- onClick = { ( e ) => handleActionMenu ( e ) }
221- ref = { containerAction }
222- >
223- < span > { t ( "BULK_ACTIONS.CAPTION" ) } </ span >
224- { /* show dropdown if actions is clicked*/ }
225- { displayActionMenu && (
226- < ul className = "dropdown-ul" >
227- { hasAccess ( "ROLE_UI_EVENTS_DELETE" , user ) && (
228- < li >
229- < ButtonLikeAnchor onClick = { ( ) => deleteModalRef . current ?. open ( ) } >
230- { t ( "BULK_ACTIONS.DELETE.EVENTS.CAPTION" ) }
231- </ ButtonLikeAnchor >
232- </ li >
233- ) }
234- { hasAccess ( "ROLE_UI_TASKS_CREATE" , user ) && (
235- < li >
236- < ButtonLikeAnchor onClick = { ( ) => startTaskModalRef . current ?. open ( ) } >
237- { t ( "BULK_ACTIONS.SCHEDULE_TASK.CAPTION" ) }
238- </ ButtonLikeAnchor >
239- </ li >
240- ) }
241- { hasAccess ( "ROLE_UI_EVENTS_DETAILS_SCHEDULING_EDIT" , user ) &&
242- hasAccess ( "ROLE_UI_EVENTS_DETAILS_METADATA_EDIT" , user ) && (
243- < li >
244- < ButtonLikeAnchor onClick = { ( ) => editScheduledEventsModalRef . current ?. open ( ) } >
245- { t ( "BULK_ACTIONS.EDIT_EVENTS.CAPTION" ) }
246- </ ButtonLikeAnchor >
247- </ li >
248- ) }
249- { hasAccess ( "ROLE_UI_EVENTS_DETAILS_METADATA_EDIT" , user ) && (
250- < li >
251- < ButtonLikeAnchor onClick = { ( ) => editMetadataEventsModalRef . current ?. open ( ) } >
252- { t ( "BULK_ACTIONS.EDIT_EVENTS_METADATA.CAPTION" ) }
253- </ ButtonLikeAnchor >
254- </ li >
255- ) }
256- </ ul >
257- ) }
258- </ div >
259-
194+ < TableActionDropdown
195+ actions = { [
196+ {
197+ accessRole : [ "ROLE_UI_EVENTS_DELETE" ] ,
198+ handleOnClick : ( ) => deleteModalRef . current ?. open ( ) ,
199+ text : "BULK_ACTIONS.DELETE.EVENTS.CAPTION" ,
200+ } ,
201+ {
202+ accessRole : [ "ROLE_UI_TASKS_CREATE" ] ,
203+ handleOnClick : ( ) => startTaskModalRef . current ?. open ( ) ,
204+ text : "BULK_ACTIONS.SCHEDULE_TASK.CAPTION" ,
205+ } ,
206+ {
207+ accessRole : [ "ROLE_UI_EVENTS_DETAILS_SCHEDULING_EDIT" , "ROLE_UI_EVENTS_DETAILS_METADATA_EDIT" ] ,
208+ handleOnClick : ( ) => editScheduledEventsModalRef . current ?. open ( ) ,
209+ text : "BULK_ACTIONS.EDIT_EVENTS.CAPTION" ,
210+ } ,
211+ {
212+ accessRole : [ "ROLE_UI_EVENTS_DETAILS_METADATA_EDIT" ] ,
213+ handleOnClick : ( ) => editMetadataEventsModalRef . current ?. open ( ) ,
214+ text : "BULK_ACTIONS.EDIT_EVENTS_METADATA.CAPTION" ,
215+ }
216+ ] }
217+ disabled = { ! showActions }
218+ />
260219 { /* Include filters component*/ }
261220 < TableFilters
262221 loadResource = { fetchEvents }
0 commit comments