From 24aa436dd71273e1dca4ea0f0cf8f6729814ac5b Mon Sep 17 00:00:00 2001 From: batabana <36864084+batabana@users.noreply.github.com> Date: Fri, 31 Mar 2023 17:27:57 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20Modals?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/common/Modal.tsx | 82 ++++++++++++++++++ src/components/message/MessageMapModal.tsx | 85 ++++++------------- src/components/message/MessageModalDelete.tsx | 50 +++-------- .../settings/InactiveSettingsModalForm.tsx | 62 +++----------- .../settings/RefreshIntervalModalForm.tsx | 62 +++----------- src/components/ticker/MastodonModalForm.tsx | 54 +++--------- src/components/ticker/TelegramModalForm.tsx | 54 +++--------- src/components/ticker/TickerModalDelete.tsx | 49 +++-------- src/components/ticker/TickerModalForm.tsx | 79 +++++------------ src/components/ticker/TickerResetModal.tsx | 61 ++++--------- .../ticker/TickerUserModalDelete.tsx | 49 +++-------- src/components/ticker/TickerUsersModal.tsx | 60 ++++--------- src/components/user/UserModalDelete.tsx | 49 +++-------- src/components/user/UserModalForm.tsx | 54 +++--------- 14 files changed, 260 insertions(+), 590 deletions(-) create mode 100644 src/components/common/Modal.tsx diff --git a/src/components/common/Modal.tsx b/src/components/common/Modal.tsx new file mode 100644 index 00000000..58facc3c --- /dev/null +++ b/src/components/common/Modal.tsx @@ -0,0 +1,82 @@ +import React, { FC, ReactNode } from 'react' +import { + Breakpoint, + Button, + Dialog, + DialogActions, + DialogContent, + DialogTitle, + IconButton, + Stack, + SxProps, +} from '@mui/material' +import { Close } from '@mui/icons-material' + +interface Props { + children: ReactNode + dangerActionButtonText?: string + dialogContentSx?: SxProps + fullWidth?: boolean + maxWidth?: Breakpoint + onClose: () => void + onDangerAction?: () => void + onSubmitAction?: () => void + open: boolean + submitForm?: string + title?: string +} + +const Modal: FC = ({ + children, + dangerActionButtonText, + dialogContentSx, + fullWidth, + maxWidth = 'md', + onClose, + onDangerAction, + onSubmitAction, + open, + submitForm, + title, +}) => { + return ( + + + + {title} + + + + + + {children} + + {submitForm && ( + + )} + {onDangerAction && dangerActionButtonText && ( + + )} + + + + ) +} + +export default Modal diff --git a/src/components/message/MessageMapModal.tsx b/src/components/message/MessageMapModal.tsx index e9ade0a5..2fe22ac8 100644 --- a/src/components/message/MessageMapModal.tsx +++ b/src/components/message/MessageMapModal.tsx @@ -5,16 +5,7 @@ import { EditControl } from 'react-leaflet-draw' import { FeatureCollection, Geometry } from 'geojson' import { Ticker } from '../../api/Ticker' import { leafletOnDataAddFitToBounds } from '../../lib/leafletFitBoundsHelper' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' -import { Close } from '@mui/icons-material' +import Modal from '../common/Modal' interface Props { open: boolean @@ -35,10 +26,6 @@ const MessageMapModal: FC = ({ const position = latLng(ticker.location.lat, ticker.location.lon) const zoom = 7 - const handleClose = () => { - onClose() - } - const onFeatureGroupUpdate = (ref: FG) => { if (ref !== null) { setFeatureGroup(ref) @@ -54,52 +41,32 @@ const MessageMapModal: FC = ({ } return ( - - - - Edit Map - - - - - - - - - + + + + + - - - - - - - - - - + + + ) } diff --git a/src/components/message/MessageModalDelete.tsx b/src/components/message/MessageModalDelete.tsx index 48861231..e1dd0829 100644 --- a/src/components/message/MessageModalDelete.tsx +++ b/src/components/message/MessageModalDelete.tsx @@ -2,16 +2,7 @@ import React, { FC, useCallback } from 'react' import { useQueryClient } from '@tanstack/react-query' import { Message, useMessageApi } from '../../api/Message' import useAuth from '../useAuth' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' -import { Close } from '@mui/icons-material' +import Modal from '../common/Modal' interface Props { onClose: () => void @@ -23,10 +14,6 @@ const MessageModalDelete: FC = ({ message, onClose, open }) => { const { deleteMessage } = useMessageApi(token) const queryClient = useQueryClient() - const handleClose = () => { - onClose() - } - const handleDelete = useCallback(() => { deleteMessage(message).then(() => { queryClient.invalidateQueries(['messages', message.ticker]) @@ -35,31 +22,16 @@ const MessageModalDelete: FC = ({ message, onClose, open }) => { }, [deleteMessage, message, onClose, queryClient]) return ( - - - - Delete Message - - - - - - - Are you sure to delete the message? This action cannot be undone. - - - - - - + + Are you sure to delete the message? This action cannot be undone. + ) } diff --git a/src/components/settings/InactiveSettingsModalForm.tsx b/src/components/settings/InactiveSettingsModalForm.tsx index 74df2082..25228fd8 100644 --- a/src/components/settings/InactiveSettingsModalForm.tsx +++ b/src/components/settings/InactiveSettingsModalForm.tsx @@ -1,16 +1,7 @@ -import { Close } from '@mui/icons-material' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' import React, { FC } from 'react' import { InactiveSetting, Setting } from '../../api/Settings' import InactiveSettingsForm from './InactiveSettingsForm' +import Modal from '../common/Modal' interface Props { open: boolean @@ -19,45 +10,20 @@ interface Props { } const InactiveSettingsModalForm: FC = ({ open, onClose, setting }) => { - const handleClose = () => { - onClose() - } - return ( - - - - Edit Inactive Settings - - - - - - - - - - - - - + + + ) } diff --git a/src/components/settings/RefreshIntervalModalForm.tsx b/src/components/settings/RefreshIntervalModalForm.tsx index 65a0f189..d54af0ed 100644 --- a/src/components/settings/RefreshIntervalModalForm.tsx +++ b/src/components/settings/RefreshIntervalModalForm.tsx @@ -1,16 +1,7 @@ -import { Close } from '@mui/icons-material' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, -} from '@mui/material' -import { Stack } from '@mui/system' import React, { FC } from 'react' import { Setting } from '../../api/Settings' import RefreshIntervalForm from './RefreshIntervalForm' +import Modal from '../common/Modal' interface Props { open: boolean @@ -19,45 +10,20 @@ interface Props { } const RefreshIntervalModalForm: FC = ({ open, onClose, setting }) => { - const handleClose = () => { - onClose() - } - return ( - - - - Edit Refresh Interval - - - - - - - - - - - - - + + + ) } diff --git a/src/components/ticker/MastodonModalForm.tsx b/src/components/ticker/MastodonModalForm.tsx index 1a7c27c8..52115dac 100644 --- a/src/components/ticker/MastodonModalForm.tsx +++ b/src/components/ticker/MastodonModalForm.tsx @@ -1,16 +1,7 @@ import React, { FC } from 'react' -import { Close } from '@mui/icons-material' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' import { Ticker } from '../../api/Ticker' import MastodonForm from './MastodonForm' +import Modal from '../common/Modal' interface Props { onClose: () => void @@ -19,41 +10,16 @@ interface Props { } const MastodonModalForm: FC = ({ onClose, open, ticker }) => { - const handleClose = () => { - onClose() - } - return ( - - - - Configure Mastodon - - - - - - - - - - - - - + + + ) } diff --git a/src/components/ticker/TelegramModalForm.tsx b/src/components/ticker/TelegramModalForm.tsx index a84043b3..df27239c 100644 --- a/src/components/ticker/TelegramModalForm.tsx +++ b/src/components/ticker/TelegramModalForm.tsx @@ -1,16 +1,7 @@ import React, { FC } from 'react' -import { Close } from '@mui/icons-material' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' import { Ticker } from '../../api/Ticker' import TelegramForm from './TelegramForm' +import Modal from '../common/Modal' interface Props { onClose: () => void @@ -19,41 +10,16 @@ interface Props { } const TelegramModalForm: FC = ({ onClose, open, ticker }) => { - const handleClose = () => { - onClose() - } - return ( - - - - Configure Telegram - - - - - - - - - - - - - + + + ) } diff --git a/src/components/ticker/TickerModalDelete.tsx b/src/components/ticker/TickerModalDelete.tsx index 0d41984d..48092e6e 100644 --- a/src/components/ticker/TickerModalDelete.tsx +++ b/src/components/ticker/TickerModalDelete.tsx @@ -2,16 +2,7 @@ import React, { FC, useCallback } from 'react' import { useQueryClient } from '@tanstack/react-query' import { Ticker, useTickerApi } from '../../api/Ticker' import useAuth from '../useAuth' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' -import { Close } from '@mui/icons-material' +import Modal from '../common/Modal' interface Props { onClose: () => void @@ -24,10 +15,6 @@ const TickerModalDelete: FC = ({ open, onClose, ticker }) => { const { deleteTicker } = useTickerApi(token) const queryClient = useQueryClient() - const handleClose = () => { - onClose() - } - const handleDelete = useCallback(() => { deleteTicker(ticker).finally(() => { queryClient.invalidateQueries(['tickers']) @@ -35,31 +22,15 @@ const TickerModalDelete: FC = ({ open, onClose, ticker }) => { }, [deleteTicker, ticker, queryClient]) return ( - - - - Delete Ticker - - - - - - - Are you sure to delete the ticker? This action cannot be undone. - - - - - - + + Are you sure to delete the ticker? This action cannot be undone. + ) } diff --git a/src/components/ticker/TickerModalForm.tsx b/src/components/ticker/TickerModalForm.tsx index 3d3d2cdb..73391234 100644 --- a/src/components/ticker/TickerModalForm.tsx +++ b/src/components/ticker/TickerModalForm.tsx @@ -1,20 +1,10 @@ -import { Close } from '@mui/icons-material' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, - Tab, - Tabs, -} from '@mui/material' +import { Tab, Tabs } from '@mui/material' import React, { FC, useState } from 'react' import { Ticker } from '../../api/Ticker' import TabPanel from '../common/TabPanel' import TickerForm from './TickerForm' import TickerSocialConnections from './TickerSocialConnections' +import Modal from '../common/Modal' interface Props { onClose: () => void @@ -25,58 +15,31 @@ interface Props { const TickerModalForm: FC = ({ onClose, open, ticker }) => { const [tabValue, setTabValue] = useState(0) - const handleClose = () => { - onClose() - } - const handleTabChange = (e: React.SyntheticEvent, value: number) => { setTabValue(value) } return ( - - - - {ticker ? 'Configure Ticker' : 'Create Ticker'} - - - - - - - - - - - - + + + + + + + + + {ticker ? ( + + - {ticker ? ( - - - - ) : null} - - - {tabValue === 0 ? ( - - ) : null} - - - + ) : null} + ) } diff --git a/src/components/ticker/TickerResetModal.tsx b/src/components/ticker/TickerResetModal.tsx index 59026ecc..6a195567 100644 --- a/src/components/ticker/TickerResetModal.tsx +++ b/src/components/ticker/TickerResetModal.tsx @@ -2,16 +2,7 @@ import React, { FC, useCallback } from 'react' import { Ticker, useTickerApi } from '../../api/Ticker' import useAuth from '../useAuth' import { useQueryClient } from '@tanstack/react-query' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' -import { Close } from '@mui/icons-material' +import Modal from '../common/Modal' interface Props { onClose: () => void @@ -24,10 +15,6 @@ const TickerResetModal: FC = ({ onClose, open, ticker }) => { const { putTickerReset } = useTickerApi(token) const queryClient = useQueryClient() - const handleClose = () => { - onClose() - } - const handleReset = useCallback(() => { putTickerReset(ticker) .then(() => { @@ -41,37 +28,21 @@ const TickerResetModal: FC = ({ onClose, open, ticker }) => { }, [onClose, putTickerReset, queryClient, ticker]) return ( - - - - Reset Ticker - - - - - - -

- Are you sure you want to reset the ticker? -

-

- This will remove all messages, descriptions, the connection to twitter - and disable the ticker. -

-
- - - - -
+ +

+ Are you sure you want to reset the ticker? +

+

+ This will remove all messages, descriptions, the connection to twitter + and disable the ticker. +

+
) } diff --git a/src/components/ticker/TickerUserModalDelete.tsx b/src/components/ticker/TickerUserModalDelete.tsx index 76b69a78..8f59337b 100644 --- a/src/components/ticker/TickerUserModalDelete.tsx +++ b/src/components/ticker/TickerUserModalDelete.tsx @@ -3,16 +3,7 @@ import { useQueryClient } from '@tanstack/react-query' import { Ticker, useTickerApi } from '../../api/Ticker' import { User } from '../../api/User' import useAuth from '../useAuth' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' -import { Close } from '@mui/icons-material' +import Modal from '../common/Modal' interface Props { ticker: Ticker @@ -26,10 +17,6 @@ const TickerUserModalDelete: FC = ({ open, onClose, ticker, user }) => { const { deleteTickerUser } = useTickerApi(token) const queryClient = useQueryClient() - const handleClose = () => { - onClose() - } - const handleDelete = useCallback(() => { deleteTickerUser(ticker, user).finally(() => { queryClient.invalidateQueries(['tickerUsers', ticker.id]) @@ -38,31 +25,15 @@ const TickerUserModalDelete: FC = ({ open, onClose, ticker, user }) => { }, [deleteTickerUser, ticker, user, queryClient, onClose]) return ( - - - - Delete User from Ticker - - - - - - - Are you sure to remove {user.email} from this ticker? - - - - - - + + Are you sure to remove {user.email} from this ticker? + ) } diff --git a/src/components/ticker/TickerUsersModal.tsx b/src/components/ticker/TickerUsersModal.tsx index efa8e92a..69a5a80b 100644 --- a/src/components/ticker/TickerUsersModal.tsx +++ b/src/components/ticker/TickerUsersModal.tsx @@ -1,17 +1,8 @@ -import { Close } from '@mui/icons-material' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' import React, { FC } from 'react' import { Ticker } from '../../api/Ticker' import { User } from '../../api/User' import TickerUsersForm from './TickerUsersForm' +import Modal from '../common/Modal' interface Props { onClose: () => void @@ -21,41 +12,22 @@ interface Props { } const TickerUsersModal: FC = ({ onClose, open, ticker, users }) => { - const handleClose = () => { - onClose() - } - return ( - - - - Manage User Access - - - - - - - { - return user.id - })} - onSubmit={handleClose} - ticker={ticker} - /> - - - - - - - + + { + return user.id + })} + onSubmit={onClose} + ticker={ticker} + /> + ) } diff --git a/src/components/user/UserModalDelete.tsx b/src/components/user/UserModalDelete.tsx index bd939b70..9fe95491 100644 --- a/src/components/user/UserModalDelete.tsx +++ b/src/components/user/UserModalDelete.tsx @@ -2,16 +2,7 @@ import React, { FC, useCallback } from 'react' import { useQueryClient } from '@tanstack/react-query' import { User, useUserApi } from '../../api/User' import useAuth from '../useAuth' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' -import { Close } from '@mui/icons-material' +import Modal from '../common/Modal' interface Props { onClose: () => void @@ -24,10 +15,6 @@ const UserModalDelete: FC = ({ onClose, open, user }) => { const { deleteUser } = useUserApi(token) const queryClient = useQueryClient() - const handleClose = () => { - onClose() - } - const handleDelete = useCallback(() => { deleteUser(user).finally(() => { queryClient.invalidateQueries(['users']) @@ -36,31 +23,15 @@ const UserModalDelete: FC = ({ onClose, open, user }) => { }, [deleteUser, user, queryClient, onClose]) return ( - - - - Delete User - - - - - - - Are you sure to delete the user? This action cannot be undone. - - - - - - + + Are you sure to delete the user? This action cannot be undone. + ) } diff --git a/src/components/user/UserModalForm.tsx b/src/components/user/UserModalForm.tsx index edc2e72e..536c3131 100644 --- a/src/components/user/UserModalForm.tsx +++ b/src/components/user/UserModalForm.tsx @@ -1,16 +1,7 @@ import React, { FC } from 'react' -import { Close } from '@mui/icons-material' -import { - Button, - Dialog, - DialogActions, - DialogContent, - DialogTitle, - IconButton, - Stack, -} from '@mui/material' import { User } from '../../api/User' import UserForm from './UserForm' +import Modal from '../common/Modal' interface Props { onClose: () => void @@ -19,41 +10,16 @@ interface Props { } const UserModalForm: FC = ({ open, onClose, user }) => { - const handleClose = () => { - onClose() - } - return ( - - - - {user ? 'Update User' : 'Create User'} - - - - - - - - - - - - - + + + ) }