// Copyright 2019 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import Button from '@material-ui/core/Button'; import Dialog from '@material-ui/core/Dialog'; import DialogActions from '@material-ui/core/DialogActions'; import DialogContent from '@material-ui/core/DialogContent'; import DialogTitle from '@material-ui/core/DialogTitle'; import IconButton from '@material-ui/core/IconButton'; import Portal from '@material-ui/core/Portal'; import Snackbar from '@material-ui/core/Snackbar'; import SnackbarContent from '@material-ui/core/SnackbarContent'; import { createStyles, Theme, WithStyles, withStyles } from '@material-ui/core/styles'; import TextField from '@material-ui/core/TextField'; import AddIcon from '@material-ui/icons/Add'; import CloseIcon from '@material-ui/icons/Close'; import CloudUploadIcon from '@material-ui/icons/CloudUpload'; import * as React from 'react'; const styles = (theme: Theme) => createStyles({ leftIcon: { marginRight: theme.spacing(1), }, uploadButton: { marginBottom: theme.spacing(3), }, errorSnackBar: { background: theme.palette.error.dark, }, }); interface Props extends WithStyles { onSubmitTask: (taskCfg: string) => Promise } interface States { dialogOpened: boolean errorOpened: boolean errorMessage: string taskConfig: string } class TaskButton extends React.Component { constructor(props: Props) { super(props); this.state = { dialogOpened: false, errorOpened: false, errorMessage: '', taskConfig: '', }; } handleOpenDialog = () => this.setState({ dialogOpened: true }); handleCloseDialog = () => this.setState({ dialogOpened: false }); handleCloseError = () => this.setState({ errorOpened: false }); handleUploadFile = (e: React.ChangeEvent) => { const files = e.currentTarget.files; if (files === null) { return; } const reader = new FileReader(); reader.onload = (e: any) => this.setState({ taskConfig: e.target.result }); reader.readAsText(files[0]); }; handleChange = (e: React.ChangeEvent) => this.setState({ taskConfig: e.target.value }); handleSubmitTask = async () => { try { await this.props.onSubmitTask(this.state.taskConfig); this.handleCloseDialog(); } catch (e) { this.setState({ errorOpened: true, errorMessage: '' + e }); } }; render() { const { classes } = this.props; return (
Submit task
{/* the Portal workarounds mui-org/material-ui#12201 */} } />
) } } export default withStyles(styles)(TaskButton);