|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { compose } from 'redux'; |
| 3 | +import { connect } from 'react-redux'; |
| 4 | +import { |
| 5 | + Button, |
| 6 | + Card, |
| 7 | + CardBody, |
| 8 | + CardFooter, |
| 9 | + CardHeader, |
| 10 | + Col, |
| 11 | + FormGroup, |
| 12 | + InputGroup, |
| 13 | + InputGroupAddon, |
| 14 | + InputGroupText, |
| 15 | + Label, |
| 16 | + Row, |
| 17 | +} from 'reactstrap'; |
| 18 | + |
| 19 | +import ListErrors from 'components/ListErrors'; |
| 20 | +import { Field, reduxForm } from 'redux-form'; |
| 21 | +import { Link } from 'react-router-dom'; |
| 22 | +import injectReducer from 'utils/injectReducer'; |
| 23 | +import reducer from './reducer'; |
| 24 | +import { postPermission, onFormLoad, onFormUnLoad } from './actions'; |
| 25 | + |
| 26 | +let data = { |
| 27 | + id: null, |
| 28 | + name: '', |
| 29 | + display_name: '', |
| 30 | + sort: '', |
| 31 | +}; |
| 32 | + |
| 33 | +class Form extends Component { |
| 34 | + componentWillReceiveProps(nextProps) { |
| 35 | + if (this.props.match.params.id !== nextProps.match.params.id) { |
| 36 | + if (nextProps.match.params.id) { |
| 37 | + this.props.onFormUnLoad(); |
| 38 | + return this.props.onFormLoad(this.props.match.params.id); |
| 39 | + } |
| 40 | + this.props.onFormLoad(null); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + componentDidMount() { |
| 45 | + if (this.props.match.params.id) { |
| 46 | + return this.props.onFormLoad(this.props.match.params.id); |
| 47 | + } |
| 48 | + this.props.onFormLoad(null); |
| 49 | + } |
| 50 | + |
| 51 | + componentWillUnmount() { |
| 52 | + this.props.onFormUnLoad(); |
| 53 | + } |
| 54 | + |
| 55 | + render() { |
| 56 | + const { handleSubmit } = this.props; |
| 57 | + const { invalid } = this.props; |
| 58 | + const { permission } = this.props; |
| 59 | + const isEditMode = permission ? true : false; |
| 60 | + const { errors } = this.props; |
| 61 | + |
| 62 | + if (permission) { |
| 63 | + data.id = permission.id; |
| 64 | + data.name = permission.name; |
| 65 | + data.display_name = permission.display_name; |
| 66 | + data.sort = permission.sort; |
| 67 | + } |
| 68 | + |
| 69 | + if (this.props.match.params.id && errors) { |
| 70 | + this.props.history.push('/access/permission'); |
| 71 | + } |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className="animated fadeIn"> |
| 75 | + <Row> |
| 76 | + <Col xs="12"> |
| 77 | + <form |
| 78 | + onSubmit={handleSubmit(this.props.postPermission.bind(this))} |
| 79 | + className="form-horizontal" |
| 80 | + > |
| 81 | + <Card> |
| 82 | + <CardHeader> |
| 83 | + <i className="fa fa fa-user-plus" /> |
| 84 | + {isEditMode ? 'Update' : 'Create'} Permission |
| 85 | + </CardHeader> |
| 86 | + <CardBody> |
| 87 | + <Row> |
| 88 | + <Col sm="8"> |
| 89 | + <ListErrors errors={this.props.errors} /> |
| 90 | + |
| 91 | + <FormGroup row> |
| 92 | + <Label |
| 93 | + className="col-md-3 col-form-label" |
| 94 | + htmlFor="name" |
| 95 | + > |
| 96 | + Name* |
| 97 | + </Label> |
| 98 | + <Col md="9"> |
| 99 | + <InputGroup> |
| 100 | + <Field |
| 101 | + className="form-control" |
| 102 | + component="input" |
| 103 | + type="text" |
| 104 | + id="name" |
| 105 | + name="name" |
| 106 | + placeholder="Enter First Name..." |
| 107 | + required |
| 108 | + /> |
| 109 | + <InputGroupAddon addonType="append"> |
| 110 | + <InputGroupText> |
| 111 | + <i className="fa fa-user" /> |
| 112 | + </InputGroupText> |
| 113 | + </InputGroupAddon> |
| 114 | + </InputGroup> |
| 115 | + </Col> |
| 116 | + </FormGroup> |
| 117 | + <FormGroup row> |
| 118 | + <Label |
| 119 | + className="col-md-3 col-form-label" |
| 120 | + htmlFor="display_name" |
| 121 | + > |
| 122 | + Display Name* |
| 123 | + </Label> |
| 124 | + <Col md="9"> |
| 125 | + <InputGroup> |
| 126 | + <Field |
| 127 | + className="form-control" |
| 128 | + component="input" |
| 129 | + type="text" |
| 130 | + id="display_name" |
| 131 | + name="display_name" |
| 132 | + placeholder="Enter Last Name..." |
| 133 | + required |
| 134 | + /> |
| 135 | + <InputGroupAddon addonType="append"> |
| 136 | + <InputGroupText> |
| 137 | + <i className="fa fa-user" /> |
| 138 | + </InputGroupText> |
| 139 | + </InputGroupAddon> |
| 140 | + </InputGroup> |
| 141 | + </Col> |
| 142 | + </FormGroup> |
| 143 | + <FormGroup row> |
| 144 | + <Label |
| 145 | + className="col-md-3 col-form-label" |
| 146 | + htmlFor="sort" |
| 147 | + > |
| 148 | + Sort |
| 149 | + </Label> |
| 150 | + <Col md="9"> |
| 151 | + <InputGroup> |
| 152 | + <Field |
| 153 | + className="form-control" |
| 154 | + component="input" |
| 155 | + type="sort" |
| 156 | + id="sort" |
| 157 | + name="sort" |
| 158 | + placeholder="Enter sort..." |
| 159 | + required |
| 160 | + /> |
| 161 | + <InputGroupAddon addonType="append"> |
| 162 | + <InputGroupText> |
| 163 | + <i className="fa fa-envelope" /> |
| 164 | + </InputGroupText> |
| 165 | + </InputGroupAddon> |
| 166 | + </InputGroup> |
| 167 | + </Col> |
| 168 | + </FormGroup> |
| 169 | + </Col> |
| 170 | + </Row> |
| 171 | + </CardBody> |
| 172 | + <CardFooter> |
| 173 | + <Button |
| 174 | + block={false} |
| 175 | + outline |
| 176 | + color="primary" |
| 177 | + disabled={invalid} |
| 178 | + type="submit" |
| 179 | + > |
| 180 | + <i className="fa fa-save" /> Submit |
| 181 | + </Button> |
| 182 | + <Button |
| 183 | + tag={Link} |
| 184 | + to={`/access/permission`} |
| 185 | + className="btn btn-outline-danger" |
| 186 | + > |
| 187 | + <i className="fa fa-arrow-left" /> Go Back |
| 188 | + </Button> |
| 189 | + </CardFooter> |
| 190 | + </Card> |
| 191 | + </form> |
| 192 | + </Col> |
| 193 | + </Row> |
| 194 | + </div> |
| 195 | + ); |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +const mapStateToProps = state => ({ |
| 200 | + ...state.permissions, |
| 201 | +}); |
| 202 | + |
| 203 | +const withreduxForm = reduxForm({ |
| 204 | + form: 'CreatePermissionForm', |
| 205 | + initialValues: data, |
| 206 | +}); |
| 207 | + |
| 208 | +const withReducer = injectReducer({ key: 'permissions', reducer }); |
| 209 | + |
| 210 | +const withConnect = connect( |
| 211 | + mapStateToProps, |
| 212 | + { postPermission, onFormLoad, onFormUnLoad } |
| 213 | +); |
| 214 | + |
| 215 | +export default compose( |
| 216 | + withReducer, |
| 217 | + withreduxForm, |
| 218 | + withConnect |
| 219 | +)(Form); |
0 commit comments