Skip to content

Commit

Permalink
feat(Workspace/Toolbar) : Rewrite toolbar(previously util) in functio…
Browse files Browse the repository at this point in the history
…nal component (#70)
  • Loading branch information
de7ign committed Mar 28, 2020
1 parent 3464213 commit f2914f8
Show file tree
Hide file tree
Showing 8 changed files with 531 additions and 1,328 deletions.
8 changes: 5 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class App extends React.Component {
muiSnackbarRef = React.createRef();

componentDidMount() {
ReactGA.initialize("UA-138685124-2");
ReactGA.pageview(window.location.pathname);
if (process.env.NODE_ENV === "production") {
ReactGA.initialize("UA-138685124-2");
ReactGA.pageview(window.location.pathname);
}
}

/**
Expand All @@ -30,7 +32,7 @@ class App extends React.Component {
<StrictMode>
<div className="App">
<Header />
<Workspace notification={this.displaySnackbar} />
<Workspace snackbar={this.displaySnackbar} />
<Notice />
<Footer />
<MuiSnackbar ref={this.muiSnackbarRef} />
Expand Down
14 changes: 13 additions & 1 deletion src/components/snackbar/MuiSnackbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ const MuiSnackbar = forwardRef((props, ref) => {
});

useImperativeHandle(ref, () => ({
/**
* Displays the snackbar notification
*
* @param {String} variant - error, warning, info, success
* @param {String} message - The notification message
*/
openSnackbar: (variant, message) => {
const allowedVariant = ["error", "warning", "info", "success"];
if (allowedVariant.indexOf(variant) === 0) {
throw new Error(
"Invalid variant Type, allowed variant types are 'error', 'warning', 'info', 'success'"
);
}
if (open) return;
setAlertData({
variant,
Expand All @@ -20,7 +32,7 @@ const MuiSnackbar = forwardRef((props, ref) => {
}
}));

const handleOnClose = reason => {
const handleOnClose = (_, reason) => {
if (reason === "clickaway") return;
setOpen(false);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/workspace/DialogBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const DialogBox = props => {

DialogBox.propTypes = {
dialogTexts: PropTypes.objectOf(PropTypes.string).isRequired,
open: PropTypes.func.isRequired,
open: PropTypes.bool.isRequired,
submit: PropTypes.func.isRequired,
close: PropTypes.func.isRequired
};
Expand Down
272 changes: 272 additions & 0 deletions src/components/workspace/ToolBar.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
import React, { useRef, useState } from "react";
import {
Paper,
Typography,
Divider,
Button,
TextField,
DialogTitle,
DialogContent,
DialogContentText,
Dialog
} from "@material-ui/core";
import { Build, Share, Delete, Autorenew, Warning } from "@material-ui/icons";
import { makeStyles } from "@material-ui/core/styles";
import fileDownload from "js-file-download";
import PropTypes from "prop-types";

const useStyles = makeStyles(theme => ({
paper: {
display: "flex",
alignItems: "stretch",
padding: theme.spacing.unit,
height: "85vh"
},
divUtil: {
flex: 1
},
icon: {
margin: theme.spacing.unit,
verticalAlign: "middle",
fontSize: "large"
},
buttons: {
marginTop: theme.spacing.unit
}
}));

const ToolBar = props => {
const { clearNetwork, updateNetwork, getNetworkData, getImageBlob } = props;
const [isExportDialogOpen, setExportDialogOpen] = useState(false);
const testInput = useRef();

const handleClearOnClick = () => {
clearNetwork();
};

const handleImportOnChange = ({ target }) => {
const fileReader = new FileReader();
const { files } = target;

fileReader.readAsText(files[0]);
fileReader.onload = e => {
clearNetwork();
const importData = JSON.parse(e.target.result);
// TODO: verify the JSON schema
updateNetwork(importData);
};
// change the file input
// eslint-disable-next-line no-param-reassign
target.value = null;
};

const handleExportOnClick = () => {
setExportDialogOpen(true);
};

const handleExportDialogOnClose = () => {
setExportDialogOpen(false);
};

const exportAsPNG = () => {
setExportDialogOpen(false);
const imgBlob = getImageBlob();
const download = document.createElement("a");
download.href = imgBlob;
download.download = "export.png";
download.click();
download.remove();
};

const exportAsJSON = () => {
const payloadJSON = getNetworkData();
const payload = JSON.stringify(payloadJSON);
fileDownload(payload, "export.json");
setExportDialogOpen(false);
};

const handleExportAsPNGOnClick = () => {
exportAsPNG();
};

const handleExportAsJSONOnClick = () => {
exportAsJSON();
};

const handleTestOnClick = () => {
// TODO: complete the method
};

const classes = useStyles();
return (
<>
<Paper className={classes.paper} elevation={6}>
<div className={classes.divUtil}>
<Typography variant="overline" color="secondary">
<Warning className={classes.icon} />
This Toolbar is in work in progress.
<br />
Until finalised you may notice some tools move, redesigned or even
disappear for a while
</Typography>
<Divider />
<br />
<Typography variant="overline">
<Build className={classes.icon} />
Test
</Typography>
<Divider />

<TextField
placeholder="string"
fullWidth
margin="dense"
variant="outlined"
InputLabelProps={{
shrink: true
}}
inputRef={testInput}
/>
<Button
variant="outlined"
fullWidth
onClick={handleTestOnClick}
style={{ marginTop: "4px" }}
>
Test
</Button>
{/* <br /> */}
{/* <IconButton style={{ color: "#000000" }}> */}
{/* <SkipPrevious /> */}
{/* </IconButton> */}
{/* {playing ? ( */}
{/* <IconButton */}
{/* style={{ color: "#000000" }} */}
{/* onClick={this.handlePlayPauseToggle} */}
{/* > */}
{/* <Pause /> */}
{/* </IconButton> */}
{/* ) : ( */}
{/* <IconButton */}
{/* style={{ color: "#000000" }} */}
{/* onClick={this.handlePlayPauseToggle} */}
{/* > */}
{/* <PlayArrow /> */}
{/* </IconButton> */}
{/* )} */}
{/* <IconButton style={{ color: "#000000" }}> */}
{/* <SkipNext /> */}
{/* </IconButton> */}
{/* <IconButton */}
{/* style={{ color: "#000000" }} */}
{/* onClick={this.handleStopClick} */}
{/* > */}
{/* <Stop /> */}
{/* </IconButton> */}

<br />
<br />

<Typography variant="overline">
<Autorenew className={classes.icon} />
Convert
</Typography>
<Divider />
<Button variant="outlined" fullWidth className={classes.buttons}>
Convert
</Button>

<br />
<br />

<Typography variant="overline">
<Share className={classes.icon} />
Share
</Typography>
<Divider />
<Button
variant="outlined"
fullWidth
onClick={handleExportOnClick}
className={classes.buttons}
>
Export
</Button>

<label htmlFor="icon-button-photo">
<input
accept=".json"
id="icon-button-photo"
onChange={handleImportOnChange}
type="file"
hidden
/>
<Button
variant="outlined"
component="span"
fullWidth
className={classes.buttons}
>
Import
</Button>
</label>

<br />
<br />

<Typography variant="overline">
<Delete className={classes.icon} />
Clear Workspace
</Typography>
<Divider />
<Button
variant="outlined"
fullWidth
onClick={handleClearOnClick}
className={classes.buttons}
>
Clear
</Button>
</div>
</Paper>
<Dialog
open={isExportDialogOpen}
onClose={handleExportDialogOnClose}
aria-labelledby="export-as-dialog"
>
<DialogTitle id="export-as-dialog">Export</DialogTitle>

<DialogContent>
<DialogContentText>Please choose an export format</DialogContentText>
<div style={{ paddingTop: 4, paddingBottom: 4 }}>
<Button
variant="outlined"
fullWidth
style={{ marginTop: 4, marginBottom: 4 }}
onClick={handleExportAsJSONOnClick}
>
as JSON
</Button>
<Button
variant="outlined"
fullWidth
style={{ marginTop: 4, marginBottom: 4 }}
onClick={handleExportAsPNGOnClick}
>
as PNG
</Button>
</div>
</DialogContent>
</Dialog>
</>
);
};

ToolBar.propTypes = {
clearNetwork: PropTypes.func.isRequired,
updateNetwork: PropTypes.func.isRequired,
getNetworkData: PropTypes.func.isRequired,
getImageBlob: PropTypes.func.isRequired
};

export default ToolBar;
Loading

0 comments on commit f2914f8

Please sign in to comment.