diff options
author | eug-vs <eug-vs@keemail.me> | 2020-10-08 20:21:35 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-10-08 20:21:35 +0300 |
commit | fd7e4817cd0e0160e85ad98ad81f406c66a94800 (patch) | |
tree | 4acabe508c38ff7f56c48fd70543fdebb3ffa976 | |
parent | 40cc9f63feb15e1bb14c5360b4437d3379df96e2 (diff) | |
download | which-ui-fd7e4817cd0e0160e85ad98ad81f406c66a94800.tar.gz |
feat: correctly wire modal logic
-rw-r--r-- | src/components/ModalScreen/ModalScreen.tsx | 19 | ||||
-rw-r--r-- | src/containers/PollCreation/PollCreation.tsx | 41 |
2 files changed, 33 insertions, 27 deletions
diff --git a/src/components/ModalScreen/ModalScreen.tsx b/src/components/ModalScreen/ModalScreen.tsx index 7090980..6c514cd 100644 --- a/src/components/ModalScreen/ModalScreen.tsx +++ b/src/components/ModalScreen/ModalScreen.tsx @@ -17,7 +17,9 @@ import CloseIcon from '@material-ui/icons/Close'; interface PropTypes { title: string; - rightIcon?: JSX.Element; + actionIcon?: JSX.Element; + handleAction?: () => void; + isActionDisabled?: boolean; } const useStyles = makeStyles(theme => ({ @@ -38,7 +40,7 @@ const Transition = React.forwardRef(( ref: React.Ref<unknown> ) => <Slide direction="left" ref={ref} {...props} />); -const ModalScreen: React.FC<PropTypes> = ({ title, rightIcon, children }) => { +const ModalScreen: React.FC<PropTypes> = ({ title, actionIcon, handleAction, isActionDisabled, children }) => { const [isOpen, setIsOpen] = useState<boolean>(true); const classes = useStyles(); const theme = useTheme(); @@ -48,6 +50,11 @@ const ModalScreen: React.FC<PropTypes> = ({ title, rightIcon, children }) => { const handleClose = useCallback(() => setIsOpen(false), [setIsOpen]); const onExited = useCallback(() => history.goBack(), [history]); + const handleClickAction = useCallback(async () => { + if (handleAction) await handleAction(); + return handleClose(); + }, [handleAction, handleClose]); + return ( <Dialog open={isOpen} @@ -66,11 +73,9 @@ const ModalScreen: React.FC<PropTypes> = ({ title, rightIcon, children }) => { <Typography variant="h6"> { title } </Typography> - { rightIcon || ( - <IconButton style={{ visibility: 'hidden' }}> - <CloseIcon /> - </IconButton> - )} + <IconButton onClick={handleClickAction} disabled={isActionDisabled}> + { actionIcon } + </IconButton> </Toolbar> </AppBar> <Divider /> diff --git a/src/containers/PollCreation/PollCreation.tsx b/src/containers/PollCreation/PollCreation.tsx index 18c3c6b..68f41d2 100644 --- a/src/containers/PollCreation/PollCreation.tsx +++ b/src/containers/PollCreation/PollCreation.tsx @@ -1,22 +1,17 @@ import React from 'react'; import Bluebird from 'bluebird'; -import { useHistory } from 'react-router-dom'; import { makeStyles } from '@material-ui/core/styles'; -import { - IconButton, - Card, - Container, - LinearProgress -} from '@material-ui/core'; +import { Card, Container, LinearProgress } from '@material-ui/core'; import SendIcon from '@material-ui/icons/Send'; import { useSnackbar } from 'notistack'; import useS3Preupload from './useS3Preupload'; import ImageInput from './ImageInput'; import ModalScreen from '../../components/ModalScreen/ModalScreen'; +import Message from '../../components/Message/Message'; import UserStrip from '../../components/UserStrip/UserStrip'; import { post } from '../../requests'; -import { useFeed } from '../../hooks/APIClient'; +import { useFeed, useProfile } from '../../hooks/APIClient'; import { useAuth } from '../../hooks/useAuth'; @@ -30,10 +25,10 @@ const useStyles = makeStyles(theme => ({ const PollCreation: React.FC = () => { const classes = useStyles(); - const history = useHistory(); const { user } = useAuth(); const { enqueueSnackbar } = useSnackbar(); const { mutate: updateFeed } = useFeed(); + const { mutate: updateProfile } = useProfile(user?.username || ''); const { file: left, setFile: setLeft, @@ -56,26 +51,23 @@ const PollCreation: React.FC = () => { right: { url: rightUrl } }; - history.push('/feed'); - post('/polls/', { contents }).then(() => { updateFeed(); + updateProfile(); enqueueSnackbar('Your poll has been successfully created!', { variant: 'success' }); }); } catch (error) { enqueueSnackbar('Failed to create a poll. Please, try again.', { variant: 'error' }); - history.push('/feed'); } }; - const submitIcon = ( - <IconButton onClick={handleSubmit} disabled={!(left && right)}> - <SendIcon /> - </IconButton> - ); - return ( - <ModalScreen title="Create a poll" rightIcon={submitIcon}> + <ModalScreen + title="Create a poll" + actionIcon={<SendIcon />} + handleAction={handleSubmit} + isActionDisabled={!(left && right) || leftProgress > 0 || rightProgress > 0} + > <Container maxWidth="sm" disableGutters> <Card elevation={3}> {user && <UserStrip user={user} info="" />} @@ -83,8 +75,17 @@ const PollCreation: React.FC = () => { <ImageInput callback={setLeft} progress={leftProgress} /> <ImageInput callback={setRight} progress={rightProgress} /> </div> - {(leftProgress > 0 || rightProgress > 0) && <LinearProgress color="primary" />} </Card> + {(leftProgress > 0 || rightProgress > 0) && ( + <> + <LinearProgress color="primary" /> + <Message + tagline="You can close this window now" + message="We will upload your images in the background." + noMargin + /> + </> + )} </Container> </ModalScreen> ); |