|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import AppBar from '@material-ui/core/AppBar/AppBar'; |
| 4 | +import Toolbar from '@material-ui/core/Toolbar/Toolbar'; |
| 5 | +import { withRouter } from 'react-router'; |
| 6 | +import clsx from 'clsx'; |
| 7 | +import { withTranslation } from 'react-i18next'; |
| 8 | +import FormGroup from '@material-ui/core/FormGroup'; |
| 9 | +import FormControlLabel from '@material-ui/core/FormControlLabel'; |
| 10 | +import Checkbox from '@material-ui/core/Checkbox'; |
| 11 | +import Button from '@material-ui/core//Button'; |
| 12 | +import { withStyles } from '@material-ui/core/styles'; |
| 13 | +import CssBaseline from '@material-ui/core/CssBaseline'; |
| 14 | +import { Typography } from '@material-ui/core'; |
| 15 | +import { connect } from 'react-redux'; |
| 16 | +import { exportSpace } from '../../../actions'; |
| 17 | +import Styles from '../../../Styles'; |
| 18 | +import Loader from '../../common/Loader'; |
| 19 | +import Main from '../../common/Main'; |
| 20 | +import SpaceNotFound from '../SpaceNotFound'; |
| 21 | + |
| 22 | +const styles = theme => ({ |
| 23 | + ...Styles(theme), |
| 24 | + buttonGroup: { |
| 25 | + textAlign: 'center', |
| 26 | + }, |
| 27 | +}); |
| 28 | + |
| 29 | +class ExportSelectionScreen extends Component { |
| 30 | + static propTypes = { |
| 31 | + classes: PropTypes.shape({ |
| 32 | + root: PropTypes.string.isRequired, |
| 33 | + appBar: PropTypes.string.isRequired, |
| 34 | + appBarShift: PropTypes.string.isRequired, |
| 35 | + menuButton: PropTypes.string.isRequired, |
| 36 | + hide: PropTypes.string.isRequired, |
| 37 | + drawer: PropTypes.string.isRequired, |
| 38 | + drawerPaper: PropTypes.string.isRequired, |
| 39 | + drawerHeader: PropTypes.string.isRequired, |
| 40 | + content: PropTypes.string.isRequired, |
| 41 | + contentShift: PropTypes.string.isRequired, |
| 42 | + buttonGroup: PropTypes.string.isRequired, |
| 43 | + submitButton: PropTypes.string.isRequired, |
| 44 | + button: PropTypes.string.isRequired, |
| 45 | + }).isRequired, |
| 46 | + theme: PropTypes.shape({ direction: PropTypes.string }).isRequired, |
| 47 | + dispatchExportSpace: PropTypes.func.isRequired, |
| 48 | + activity: PropTypes.bool.isRequired, |
| 49 | + history: PropTypes.shape({ |
| 50 | + goBack: PropTypes.func.isRequired, |
| 51 | + length: PropTypes.number.isRequired, |
| 52 | + }).isRequired, |
| 53 | + userId: PropTypes.string.isRequired, |
| 54 | + location: PropTypes.shape({ |
| 55 | + search: PropTypes.string.isRequired, |
| 56 | + state: PropTypes.shape({ |
| 57 | + space: PropTypes.shape({ |
| 58 | + id: PropTypes.string.isRequired, |
| 59 | + name: PropTypes.string.isRequired, |
| 60 | + }), |
| 61 | + }), |
| 62 | + }).isRequired, |
| 63 | + t: PropTypes.func.isRequired, |
| 64 | + }; |
| 65 | + |
| 66 | + state = { |
| 67 | + space: true, |
| 68 | + actions: true, |
| 69 | + resources: true, |
| 70 | + }; |
| 71 | + |
| 72 | + handleChange = event => { |
| 73 | + this.setState({ [event.target.name]: event.target.checked }); |
| 74 | + }; |
| 75 | + |
| 76 | + handleBack = () => { |
| 77 | + const { |
| 78 | + history: { goBack }, |
| 79 | + } = this.props; |
| 80 | + goBack(); |
| 81 | + }; |
| 82 | + |
| 83 | + handleSubmit = () => { |
| 84 | + const { |
| 85 | + userId, |
| 86 | + location: { |
| 87 | + state: { |
| 88 | + space: { id, name }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + dispatchExportSpace, |
| 92 | + } = this.props; |
| 93 | + const { space, actions, resources } = this.state; |
| 94 | + const selection = { space, actions, resources }; |
| 95 | + dispatchExportSpace(id, name, userId, selection); |
| 96 | + }; |
| 97 | + |
| 98 | + render() { |
| 99 | + const { |
| 100 | + classes, |
| 101 | + t, |
| 102 | + location: { state }, |
| 103 | + activity, |
| 104 | + } = this.props; |
| 105 | + const { |
| 106 | + space: isSpaceChecked, |
| 107 | + resources: isResourcesChecked, |
| 108 | + actions: isActionsChecked, |
| 109 | + } = this.state; |
| 110 | + |
| 111 | + if (!state || !state.space) { |
| 112 | + return <SpaceNotFound />; |
| 113 | + } |
| 114 | + |
| 115 | + if (activity) { |
| 116 | + return ( |
| 117 | + <div className={classes.root}> |
| 118 | + <CssBaseline /> |
| 119 | + <AppBar position="fixed"> |
| 120 | + <Toolbar /> |
| 121 | + </AppBar> |
| 122 | + <main className="Main"> |
| 123 | + <Loader /> |
| 124 | + </main> |
| 125 | + </div> |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + const spaceCheckbox = ( |
| 130 | + <Checkbox |
| 131 | + checked={isSpaceChecked} |
| 132 | + onChange={this.handleChange} |
| 133 | + name="space" |
| 134 | + color="primary" |
| 135 | + /> |
| 136 | + ); |
| 137 | + |
| 138 | + const resourcesCheckbox = ( |
| 139 | + <Checkbox |
| 140 | + checked={isResourcesChecked} |
| 141 | + onChange={this.handleChange} |
| 142 | + name="resources" |
| 143 | + color="primary" |
| 144 | + /> |
| 145 | + ); |
| 146 | + const actionsCheckbox = ( |
| 147 | + <Checkbox |
| 148 | + checked={isActionsChecked} |
| 149 | + onChange={this.handleChange} |
| 150 | + name="actions" |
| 151 | + color="primary" |
| 152 | + /> |
| 153 | + ); |
| 154 | + |
| 155 | + return ( |
| 156 | + <Main fullScreen> |
| 157 | + <div> |
| 158 | + <Typography align="center" variant="h4"> |
| 159 | + {t('What do you want to export?')} |
| 160 | + </Typography> |
| 161 | + |
| 162 | + <br /> |
| 163 | + <FormGroup> |
| 164 | + <FormControlLabel control={spaceCheckbox} label={t('This Space')} /> |
| 165 | + <FormControlLabel |
| 166 | + control={resourcesCheckbox} |
| 167 | + label={t("This Space's User Inputs")} |
| 168 | + /> |
| 169 | + <FormControlLabel |
| 170 | + control={actionsCheckbox} |
| 171 | + label={t("This Space's Analytics")} |
| 172 | + /> |
| 173 | + </FormGroup> |
| 174 | + <br /> |
| 175 | + <div className={classes.buttonGroup}> |
| 176 | + <Button |
| 177 | + variant="contained" |
| 178 | + color="primary" |
| 179 | + className={clsx(classes.button, classes.submitButton)} |
| 180 | + onClick={this.handleBack} |
| 181 | + > |
| 182 | + {t('Back')} |
| 183 | + </Button> |
| 184 | + <Button |
| 185 | + variant="contained" |
| 186 | + color="primary" |
| 187 | + className={clsx(classes.button, classes.submitButton)} |
| 188 | + onClick={this.handleSubmit} |
| 189 | + > |
| 190 | + {t('Export')} |
| 191 | + </Button> |
| 192 | + </div> |
| 193 | + </div> |
| 194 | + </Main> |
| 195 | + ); |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +const mapStateToProps = ({ authentication, Space }) => ({ |
| 200 | + userId: authentication.getIn(['user', 'id']), |
| 201 | + activity: Boolean(Space.getIn(['current', 'activity']).size), |
| 202 | +}); |
| 203 | + |
| 204 | +const mapDispatchToProps = { |
| 205 | + dispatchExportSpace: exportSpace, |
| 206 | +}; |
| 207 | + |
| 208 | +const TranslatedComponent = withTranslation()(ExportSelectionScreen); |
| 209 | + |
| 210 | +export default withRouter( |
| 211 | + withStyles(styles, { withTheme: true })( |
| 212 | + connect(mapStateToProps, mapDispatchToProps)(TranslatedComponent) |
| 213 | + ) |
| 214 | +); |
0 commit comments