feat: action button in menu

This commit is contained in:
cv
2021-05-06 23:03:36 +02:00
parent 1d0c819e5a
commit 3f598f3a7a
3 changed files with 44 additions and 17 deletions
@@ -1,5 +1,5 @@
import { useMutation, useQueryClient } from 'react-query'; import { useMutation, useQueryClient } from 'react-query';
import { useEffect } from 'react'; import { useCallback, useEffect } from 'react';
import { import {
eventsNamespace, eventsNamespace,
fetchAllEvents, fetchAllEvents,
@@ -14,7 +14,7 @@ import EventList from './EventList';
import EventListMenu from '../../menu/EventListMenu.jsx'; import EventListMenu from '../../menu/EventListMenu.jsx';
import { showErrorToast } from '../../../common/helpers/toastManager'; import { showErrorToast } from '../../../common/helpers/toastManager';
import { useFetch } from '../../../app/hooks/useFetch.js'; import { useFetch } from '../../../app/hooks/useFetch.js';
import EventListSkeleton from '../skeletons/EventListSkeleton.jsx'; import Empty from '../../../common/state/Empty';
export default function EventListWrapper() { export default function EventListWrapper() {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
@@ -164,7 +164,7 @@ export default function EventListWrapper() {
}, },
}); });
const reorderEvent = useMutation(requestReorder, { const reorderEvent = useMutation(requestReorder, {
// we optimistically update here // we optimistically update here
onMutate: async (data) => { onMutate: async (data) => {
// cancel ongoing queries // cancel ongoing queries
@@ -203,7 +203,7 @@ export default function EventListWrapper() {
}, [isError]); }, [isError]);
// Events API // Events API
const eventsHandler = async (action, payload) => { const eventsHandler = useCallback(async (action, payload) => {
switch (action) { switch (action) {
case 'add': case 'add':
try { try {
@@ -289,7 +289,7 @@ export default function EventListWrapper() {
showErrorToast('Unrecognised request', action); showErrorToast('Unrecognised request', action);
break; break;
} }
}; }, []);
return ( return (
<> <>
@@ -297,7 +297,7 @@ export default function EventListWrapper() {
{status === 'success' ? ( {status === 'success' ? (
<EventList events={data} eventsHandler={eventsHandler} /> <EventList events={data} eventsHandler={eventsHandler} />
) : ( ) : (
<EventListSkeleton /> <Empty text='Connecting to server' />
)} )}
</> </>
); );
@@ -1,18 +1,21 @@
import { Menu, MenuButton, MenuItem, MenuList } from '@chakra-ui/menu'; import { Menu, MenuButton, MenuItem, MenuList } from '@chakra-ui/menu';
import { IconButton } from '@chakra-ui/button'; import { IconButton } from '@chakra-ui/button';
import { import { FiZap, FiPlus, FiClock, FiMinusCircle } from 'react-icons/fi';
FiZap, import { useEffect } from 'react';
FiTrash2,
} from 'react-icons/fi';
export default function MenuActionButtons(props) { export default function MenuActionButtons(props) {
const { actionHandler } = props;
const menuStyle = { const menuStyle = {
color: '#000000', color: '#000000',
backgroundColor: 'rgba(255,255,255,1)', backgroundColor: 'rgba(255,255,255,1)',
}; };
useEffect(() =>{
console.log('debug action button render')
})
return ( return (
<Menu> <Menu isLazy lazyBehavior='unmount'>
<MenuButton <MenuButton
as={IconButton} as={IconButton}
aria-label='Options' aria-label='Options'
@@ -24,8 +27,20 @@ export default function MenuActionButtons(props) {
color={'orange.500'} color={'orange.500'}
/> />
<MenuList style={menuStyle}> <MenuList style={menuStyle}>
<MenuItem icon={<FiTrash2 />} onClick={props.deleteAllHandler}> {/* <MenuItem icon={<FiTrash2 />} onClick={props.deleteAllHandler}>
Delete All Delete All
</MenuItem> */}
<MenuItem icon={<FiPlus />} onClick={() => actionHandler('event')}>
Event first
</MenuItem>
<MenuItem icon={<FiClock />} onClick={() => actionHandler('delay')}>
Delay first
</MenuItem>
<MenuItem
icon={<FiMinusCircle />}
onClick={() => actionHandler('block')}
>
Block first
</MenuItem> </MenuItem>
</MenuList> </MenuList>
</Menu> </Menu>
+17 -5
View File
@@ -28,9 +28,22 @@ const EventListMenu = ({ eventsHandler }) => {
backgroundColor: 'rgba(255,255,255,0.67)', backgroundColor: 'rgba(255,255,255,0.67)',
}; };
const addHandler = useCallback(() => { const actionHandler = (action) => {
eventsHandler('add', { type: 'event', order: 0 }); console.log('debug action called', action);
}, [eventsHandler]); switch (action) {
case 'event':
eventsHandler('add', { type: action, order: 0 });
break;
case 'delay':
eventsHandler('add', { type: action, order: 0 });
break;
case 'block':
eventsHandler('add', { type: action, order: 0 });
break;
default:
break;
}
};
return ( return (
<div className={style.headerButtons}> <div className={style.headerButtons}>
@@ -58,8 +71,7 @@ const EventListMenu = ({ eventsHandler }) => {
<MenuItem>Download CSV</MenuItem> <MenuItem>Download CSV</MenuItem>
</MenuList> </MenuList>
</Menu> </Menu>
<AddIconBtn clickhandler={addHandler} size='sm' /> <MenuActionButtons actionHandler={actionHandler} size='sm' />
{/* <MenuActionButtons size='sm' /> */}
</div> </div>
); );
}; };