ParameterDialog.tsx 3.07 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {translate} from 'react-i18next';

// actions
import {clearParameterDetails, deleteParameter, saveParameter} from 'kpi/actions/admin';

// model
import KPIParameter from 'model/kpi/KPIParameter';

import {withStyles} from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import DialogContent from '@material-ui/core/DialogContent';
import DialogTitle from '@material-ui/core/DialogTitle';
import ParameterForm from './c/parameterForm';

/*tslint:disable*/
const styles = (theme) => ({
  createButton: {
    [theme.breakpoints.down('sm')]: {
      width: '100%',
      margin: '8px 0',
    },
  },
});
/*tslint:enable*/

interface IParameterDialogProps extends React.ClassAttributes<any> {
  saveParameter: (parameter: KPIParameter) => void;
  clearParameterDetails: () => void;
  deleteParameter: (parameter: KPIParameter) => void;
  useLink: boolean;
  classes: any;
  parameter: KPIParameter;
37
  variant?: 'text' | 'flat' | 'outlined' | 'contained' | 'contained' | 'fab' | 'extendedFab';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  t: any;
}

class ParameterDialog extends React.Component<IParameterDialogProps, any> {

  public state = {
    open: false,
  };

  private handleCreate = () => {
    const {clearParameterDetails} = this.props;
    clearParameterDetails();
    this.show();
  }

  private show = () => {
    this.setState(
      (state) => ({...state, open: true}));
  }

  private hide = () => {
    this.setState({open: false});
  }

  private handleSubmit = (parameter: KPIParameter) => {
    const {saveParameter} = this.props;
    saveParameter(parameter);
    this.setState({open: false});
  }

  private handleDelete = () => {
    const {deleteParameter, parameter} = this.props;
    deleteParameter(parameter);
    this.setState({open: false});
  }

  public render() {
75
    const {classes, parameter, variant = 'contained', t, useLink} = this.props;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    return (
      <span>
        { useLink && parameter ?
          <a onClick={ this.show }><b>{ parameter.title }</b></a>
          :
          <Button className={ classes.createButton } onClick={ this.handleCreate } variant={ variant }>{ t(`kpi.admin.p.parameterDialog.button`) }</Button>
        }
        { this.state.open &&
        <Dialog open={ this.state.open } onClose={ this.hide } maxWidth="md" fullWidth>
          <DialogTitle>{ t(`kpi.admin.p.parameterDialog.title`) }</DialogTitle>
          <DialogContent>
            <ParameterForm initialValues={ parameter } nameDisabled={ parameter && parameter.name } onDelete={ parameter && this.handleDelete } onCancel={ this.hide } onSubmit={ this.handleSubmit }/>
          </DialogContent>
        </Dialog>
        }
    </span>
    );
  }
}

const mapStateToProps = (state) => ({
  // null maybe
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
  clearParameterDetails,
  saveParameter,
  deleteParameter,
}, dispatch);

export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)<any>(translate()(ParameterDialog)));