Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {Component} from 'react'
  2. import PropTypes from 'prop-types';
  3. import {bindActionCreators} from 'redux'
  4. import {connect} from 'react-redux'
  5. import {Button, Col, ControlLabel, Form, FormControl, FormGroup, Glyphicon, Row} from "react-bootstrap";
  6. import * as actions from "./CertificatesApi";
  7.  
  8. class Certificate extends Component {
  9.  
  10.     constructor(props, context) {
  11.         super(props, context);
  12.  
  13.         this.state = {
  14.             resource: {},
  15.             validationErrors: {},
  16.             previousTitle: '',
  17.             userConfirm: '',
  18.             users: null,
  19.             page: 1,
  20.             sizePerPage: 10
  21.         }
  22.     };
  23.  
  24.  
  25.     handleTitleChange = (e) => {
  26.         const {resource} = this.state;
  27.         this.setState({resource: {...resource, title: e.target.value}});
  28.     };
  29.     handleDescriptionChange = (e) => {
  30.         const {resource} = this.state;
  31.         this.setState({resource: {...resource, description: e.target.value}});
  32.     };
  33.     handleUserChange = (e) => {
  34.         this.setState({userConfirm: e.target.value});
  35.     };
  36.     saveCertificate = (e) => {
  37.         e.preventDefault();
  38.         const {resource, userConfirm} = this.state;
  39.         const validationErrors = {};
  40.         if (Object.keys(resource).length > 0) {
  41.             if (!resource.title || resource.title.length < 5)
  42.                 validationErrors.title = "invalid title";
  43.             if (!resource.description || resource.description.length < 6)
  44.                 validationErrors.description = "invalid description";
  45.             if (userConfirm !== resource.user)
  46.                 validationErrors.userConfirm = "users don't match";
  47.         }
  48.         if (Object.keys(validationErrors).length > 0) {
  49.             this.setState({validationErrors});
  50.         } else {
  51.             this.props.actions.saveCertificate(resource, () => {
  52.                 this.context.router.history.push('/certificates');
  53.             });
  54.         }
  55.     };
  56.  
  57.     reload() {
  58.         const {page, sizePerPage} = this.state;
  59.         this.props.actions.loadUsers({page: page, per_page: sizePerPage},
  60.             users => this.setState({users, page, sizePerPage}));
  61.     }
  62.  
  63.     componentDidMount() {
  64.         const id = this.props.match.params.id;
  65.         if (id != null) {
  66.             this.loadCertificate(id, organizer => this.setState({resource: organizer}));
  67.         } else {
  68.             this.setState({resource: {}});
  69.         }
  70.         this.reload();
  71.     };
  72.  
  73.     loadCertificate(id) {
  74.         this.props.actions.loadCertificate(id,
  75.             resource => this.setState({resource: resource, previousTitle: resource.title}));
  76.     };
  77.  
  78.     getValidationState(id) {
  79.         const {validationErrors} = this.state;
  80.         if (validationErrors.title && id === 'title') {
  81.             return 'error';
  82.         }
  83.         if (validationErrors.description && id === 'description') {
  84.             return 'error';
  85.         }
  86.         if (validationErrors.userConfirm && id === 'userConfirm') {
  87.             return 'error';
  88.         }
  89.         return null;
  90.     }
  91.  
  92.     render() {
  93.         const {resource, validationErrors, previousTitle, userConfirm, users} = this.state;  
  94.         return (
  95.             <div>
  96.                 {resource && <Row className="vertical-middle breadcrumbs">
  97.                     <Col xs={8}>
  98.                         <h5>
  99.                             <Glyphicon
  100.                                 glyph="cog"/> Admin > Certificates > {resource.id ?
  101.                                 <span><b>{previousTitle}</b> - edit</span> :
  102.                                 <span>New</span>}
  103.                         </h5>
  104.                     </Col>
  105.                 </Row>
  106.                 }
  107.                 {resource &&
  108.                 <Row id='form'>
  109.                     <Col xs={12} md={6}>
  110.                         <Form horizontal onSubmit={this.saveCertificate}>
  111.                             <FormGroup
  112.                                 controlId="title"
  113.                                 validationState={this.getValidationState('title')}
  114.                             >
  115.                                 <Col componentClass={ControlLabel} sm={2}>Certificate name</Col>
  116.                                 <Col sm={10}>
  117.                                     <FormControl
  118.                                         type="text"
  119.                                         placeholder="Enter title"
  120.                                         onChange={this.handleTitleChange}
  121.                                     />
  122.                                     {
  123.                                         Object.keys(validationErrors).length > 0 && validationErrors.title &&
  124.                                         <ControlLabel>{validationErrors.title}</ControlLabel>
  125.                                     }
  126.                                 </Col>
  127.                                 <FormControl.Feedback/>
  128.                             </FormGroup>
  129.                             <FormGroup
  130.                                 controlId="description"
  131.                                 validationState={this.getValidationState('description')}
  132.                             >
  133.                                 <Col componentClass={ControlLabel} sm={2}>Description</Col>
  134.                                 <Col sm={10}>
  135.                                     <FormControl
  136.                                         placeholder="Enter description"
  137.                                         componentClass="textarea"
  138.                                         rows="3"
  139.                                         onChange={this.handleDescriptionChange}
  140.                                     />
  141.                                     {
  142.                                         Object.keys(validationErrors).length > 0 && validationErrors.description &&
  143.                                         <ControlLabel>{validationErrors.description}</ControlLabel>
  144.                                     }
  145.                                 </Col>
  146.                                 <FormControl.Feedback/>
  147.                             </FormGroup>
  148.                             <FormGroup
  149.                                 controlId="formControlsSelect"
  150.                                 validationState={this.getValidationState('userConfirm')}
  151.                             >
  152.                                 <Col componentClass={ControlLabel}
  153.                                      sm={2}>User</Col>
  154.                                 <Col sm={10}>
  155.                                     <FormControl componentClass="select" placeholder="select" value={userConfirm} onChange={this.handleUserChange}>
  156.                                         <option value="select">Select user</option>
  157.                                         <option value="select">{console.log(users)}</option>
  158.                                     </FormControl>
  159.                                 </Col>
  160.                                 <FormControl.Feedback/>
  161.                             </FormGroup>
  162.                             <Col xsOffset={2} xs={10} className='form-buttons margin10'>
  163.                                 <Button type="submit" bsStyle={'success'}>Save</Button>
  164.                                 <Button
  165.                                     bsStyle={'warning'}
  166.                                     onClick={() => this.context.router.history.push(`/certificates`)}
  167.                                 >
  168.                                     Cancel
  169.                                 </Button>
  170.                             </Col>
  171.                         </Form>
  172.                     </Col>
  173.                 </Row>
  174.                 }
  175.  
  176.             </div>
  177.         );
  178.     }
  179. }
  180.  
  181. Certificate.contextTypes = {
  182.     router: PropTypes.object
  183. };
  184.  
  185.  
  186. const mapDispatchToProps = dispatch => ({
  187.     actions: bindActionCreators(
  188.         actions,
  189.         dispatch)
  190. });
  191.  
  192. export default connect(
  193.     undefined,
  194.     mapDispatchToProps
  195. )(Certificate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement