From 5744923265ff51e392575edfd6a8182589105dc6 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Wed, 12 Aug 2020 01:59:27 +0300
Subject: feat: PollCreation page and Fab component

---
 src/containers/PollCreation/PollCreation.tsx | 105 +++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)
 create mode 100644 src/containers/PollCreation/PollCreation.tsx

(limited to 'src/containers/PollCreation/PollCreation.tsx')

diff --git a/src/containers/PollCreation/PollCreation.tsx b/src/containers/PollCreation/PollCreation.tsx
new file mode 100644
index 0000000..1f3a802
--- /dev/null
+++ b/src/containers/PollCreation/PollCreation.tsx
@@ -0,0 +1,105 @@
+import React, { useState } from 'react';
+import { makeStyles } from '@material-ui/core/styles';
+import Collapse from '@material-ui/core/Collapse';
+import {
+  Button,
+  Card,
+  ClickAwayListener,
+  Divider,
+  Container
+} from '@material-ui/core';
+import { Poll } from 'which-types';
+import { useSnackbar } from 'notistack';
+import axios from 'axios';
+
+import PollCreationImage from './PollCreationImage';
+import UserStrip from '../../components/UserStrip/UserStrip';
+import { get, post } from '../../requests';
+import { useAuth } from '../../hooks/useAuth';
+
+interface PropTypes{
+  addPoll: (poll: Poll) => void;
+}
+
+const useStyles = makeStyles(theme => ({
+  root: {
+    marginBottom: theme.spacing(4)
+  },
+  images: {
+    height: theme.spacing(50),
+    display: 'flex'
+  }
+}));
+
+const PollCreation: React.FC<PropTypes> = ({ addPoll }) => {
+  const classes = useStyles();
+  const [expanded, setExpanded] = useState(true);
+  const [left, setLeft] = useState<File>();
+  const [right, setRight] = useState<File>();
+  const { enqueueSnackbar } = useSnackbar();
+  const { user } = useAuth();
+
+  const readyToSubmit = left && right;
+
+  const handleClickAway = () => {
+    setExpanded(false);
+  };
+
+  const uploadImage = (file?: File) => {
+    const headers = { 'Content-Type': 'image/png' };
+    return get('/files')
+      .then(response => response.data)
+      .then(uploadUrl => axios.put(uploadUrl, file, { headers }))
+      .then(response => {
+        const { config: { url } } = response;
+        return url && url.slice(0, url.indexOf('.png') + 4);
+      });
+  };
+
+  const handleClick = async () => {
+    if (expanded && readyToSubmit) {
+      const [leftUrl, rightUrl] = await Promise.all([uploadImage(left), uploadImage(right)]);
+
+      const contents = {
+        left: { url: leftUrl },
+        right: { url: rightUrl }
+      };
+
+      post('/polls/', { contents }).then(response => {
+        addPoll(response.data);
+        enqueueSnackbar('Your poll has been successfully created!', {
+          variant: 'success'
+        });
+      });
+    }
+    setExpanded(!expanded);
+  };
+
+  return (
+    <Container maxWidth="sm" disableGutters>
+      <ClickAwayListener onClickAway={handleClickAway}>
+        <Card className={classes.root}>
+          <Collapse in={expanded} timeout="auto" unmountOnExit>
+            {user && <UserStrip user={user} info="" />}
+            <Divider />
+            <div className={classes.images}>
+              <PollCreationImage file={left} setFile={setLeft} />
+              <PollCreationImage file={right} setFile={setRight} />
+            </div>
+          </Collapse>
+          <Button
+            color="primary"
+            disabled={expanded && !readyToSubmit}
+            variant={expanded ? 'contained' : 'outlined'}
+            onClick={handleClick}
+            fullWidth
+          >
+            {expanded ? 'Submit' : 'Create a Poll'}
+          </Button>
+        </Card>
+      </ClickAwayListener>
+    </Container>
+  );
+};
+
+export default PollCreation;
-- 
cgit v1.2.3


From 52799ec4e4cd5801423ee0d2aa56039c061afdb4 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Thu, 13 Aug 2020 17:24:09 +0300
Subject: feat!: remove expansion from PollCreation

---
 src/containers/PollCreation/PollCreation.tsx | 48 +++++++++++-----------------
 1 file changed, 18 insertions(+), 30 deletions(-)

(limited to 'src/containers/PollCreation/PollCreation.tsx')

diff --git a/src/containers/PollCreation/PollCreation.tsx b/src/containers/PollCreation/PollCreation.tsx
index 1f3a802..7501d3a 100644
--- a/src/containers/PollCreation/PollCreation.tsx
+++ b/src/containers/PollCreation/PollCreation.tsx
@@ -1,10 +1,8 @@
 import React, { useState } from 'react';
 import { makeStyles } from '@material-ui/core/styles';
-import Collapse from '@material-ui/core/Collapse';
 import {
   Button,
   Card,
-  ClickAwayListener,
   Divider,
   Container
 } from '@material-ui/core';
@@ -33,7 +31,6 @@ const useStyles = makeStyles(theme => ({
 
 const PollCreation: React.FC<PropTypes> = ({ addPoll }) => {
   const classes = useStyles();
-  const [expanded, setExpanded] = useState(true);
   const [left, setLeft] = useState<File>();
   const [right, setRight] = useState<File>();
   const { enqueueSnackbar } = useSnackbar();
@@ -41,10 +38,6 @@ const PollCreation: React.FC<PropTypes> = ({ addPoll }) => {
 
   const readyToSubmit = left && right;
 
-  const handleClickAway = () => {
-    setExpanded(false);
-  };
-
   const uploadImage = (file?: File) => {
     const headers = { 'Content-Type': 'image/png' };
     return get('/files')
@@ -57,7 +50,7 @@ const PollCreation: React.FC<PropTypes> = ({ addPoll }) => {
   };
 
   const handleClick = async () => {
-    if (expanded && readyToSubmit) {
+    if (readyToSubmit) {
       const [leftUrl, rightUrl] = await Promise.all([uploadImage(left), uploadImage(right)]);
 
       const contents = {
@@ -72,32 +65,27 @@ const PollCreation: React.FC<PropTypes> = ({ addPoll }) => {
         });
       });
     }
-    setExpanded(!expanded);
   };
 
   return (
     <Container maxWidth="sm" disableGutters>
-      <ClickAwayListener onClickAway={handleClickAway}>
-        <Card className={classes.root}>
-          <Collapse in={expanded} timeout="auto" unmountOnExit>
-            {user && <UserStrip user={user} info="" />}
-            <Divider />
-            <div className={classes.images}>
-              <PollCreationImage file={left} setFile={setLeft} />
-              <PollCreationImage file={right} setFile={setRight} />
-            </div>
-          </Collapse>
-          <Button
-            color="primary"
-            disabled={expanded && !readyToSubmit}
-            variant={expanded ? 'contained' : 'outlined'}
-            onClick={handleClick}
-            fullWidth
-          >
-            {expanded ? 'Submit' : 'Create a Poll'}
-          </Button>
-        </Card>
-      </ClickAwayListener>
+      <Card className={classes.root}>
+        {user && <UserStrip user={user} info="" />}
+        <Divider />
+        <div className={classes.images}>
+          <PollCreationImage file={left} setFile={setLeft} />
+          <PollCreationImage file={right} setFile={setRight} />
+        </div>
+        <Button
+          color="primary"
+          disabled={!readyToSubmit}
+          variant="contained"
+          onClick={handleClick}
+          fullWidth
+        >
+          Submit
+        </Button>
+      </Card>
     </Container>
   );
 };
-- 
cgit v1.2.3

