fix: Simplify modal code

This commit is contained in:
blessedcoolant
2023-07-13 20:34:23 +12:00
parent 91c4e4c9b9
commit 6861499697
2 changed files with 23 additions and 19 deletions

View File

@ -1,4 +1,3 @@
import { useDisclosure } from '@chakra-ui/react';
import { import {
AlertDialog, AlertDialog,
AlertDialogBody, AlertDialogBody,
@ -6,27 +5,27 @@ import {
AlertDialogFooter, AlertDialogFooter,
AlertDialogHeader, AlertDialogHeader,
AlertDialogOverlay, AlertDialogOverlay,
Divider,
Text,
Button, Button,
Text,
useDisclosure,
} from '@chakra-ui/react'; } from '@chakra-ui/react';
import { useAppDispatch } from 'app/store/storeHooks';
import { clearNodes } from 'features/nodes/store/nodesSlice';
import { makeToast } from 'app/components/Toaster'; import { makeToast } from 'app/components/Toaster';
import { RootState } from 'app/store/store';
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
import IAIIconButton from 'common/components/IAIIconButton';
import { clearNodes } from 'features/nodes/store/nodesSlice';
import { addToast } from 'features/system/store/systemSlice'; import { addToast } from 'features/system/store/systemSlice';
import { memo } from 'react'; import { memo, useRef } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { FaTrash } from 'react-icons/fa'; import { FaTrash } from 'react-icons/fa';
import IAIIconButton from 'common/components/IAIIconButton';
const ClearNodesButton = () => { const ClearNodesButton = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { isOpen, onOpen, onClose } = useDisclosure(); const { isOpen, onOpen, onClose } = useDisclosure();
const cancelRef = useRef<HTMLButtonElement | null>(null);
const handleClearNodes = () => { const nodes = useAppSelector((state: RootState) => state.nodes.nodes);
onOpen();
};
const handleConfirmClear = () => { const handleConfirmClear = () => {
dispatch(clearNodes()); dispatch(clearNodes());
@ -43,20 +42,22 @@ const ClearNodesButton = () => {
onClose(); onClose();
}; };
const handleCancelClear = () => {
onClose();
};
return ( return (
<> <>
<IAIIconButton <IAIIconButton
icon={<FaTrash />} icon={<FaTrash />}
tooltip={t('nodes.clearNodes')} tooltip={t('nodes.clearNodes')}
aria-label={t('nodes.clearNodes')} aria-label={t('nodes.clearNodes')}
onClick={handleClearNodes} onClick={onOpen}
isDisabled={nodes.length === 0}
/> />
<AlertDialog isOpen={isOpen} onClose={handleCancelClear} isCentered> <AlertDialog
isOpen={isOpen}
onClose={onClose}
leastDestructiveRef={cancelRef}
isCentered
>
<AlertDialogOverlay /> <AlertDialogOverlay />
<AlertDialogContent> <AlertDialogContent>
@ -68,10 +69,10 @@ const ClearNodesButton = () => {
<Text>{t('common.clearNodes')}</Text> <Text>{t('common.clearNodes')}</Text>
</AlertDialogBody> </AlertDialogBody>
<Divider />
<AlertDialogFooter> <AlertDialogFooter>
<Button onClick={handleCancelClear}>{t('common.cancel')}</Button> <Button ref={cancelRef} onClick={onClose}>
{t('common.cancel')}
</Button>
<Button colorScheme="red" ml={3} onClick={handleConfirmClear}> <Button colorScheme="red" ml={3} onClick={handleConfirmClear}>
{t('common.accept')} {t('common.accept')}
</Button> </Button>

View File

@ -12,6 +12,8 @@ const SaveNodesButton = () => {
(state: RootState) => state.nodes.editorInstance (state: RootState) => state.nodes.editorInstance
); );
const nodes = useAppSelector((state: RootState) => state.nodes.nodes);
const saveEditorToJSON = useCallback(() => { const saveEditorToJSON = useCallback(() => {
if (editorInstance) { if (editorInstance) {
const editorState = editorInstance.toObject(); const editorState = editorInstance.toObject();
@ -38,6 +40,7 @@ const SaveNodesButton = () => {
tooltip={t('nodes.saveNodes')} tooltip={t('nodes.saveNodes')}
aria-label={t('nodes.saveNodes')} aria-label={t('nodes.saveNodes')}
onClick={saveEditorToJSON} onClick={saveEditorToJSON}
isDisabled={nodes.length === 0}
/> />
); );
}; };