|
| 1 | +/** |
| 2 | + * @file ExperienceCreateForm.js |
| 3 | + * Exports a component that allows users to create an EditVR experience. |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { Component } from 'react'; |
| 7 | +import PropTypes from 'prop-types'; |
| 8 | +import { TextField, Button, withStyles } from '@material-ui/core'; |
| 9 | + |
| 10 | +import { Message } from '../'; |
| 11 | +import ExperienceCreateFormStyles from './ExperienceCreateForm.style'; |
| 12 | +import { EXPERIENCES_CREATE } from '../../constants'; |
| 13 | + |
| 14 | +class ExperienceCreateForm extends Component { |
| 15 | + static propTypes = { |
| 16 | + submitHandler: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]), |
| 17 | + classes: PropTypes.shape({ |
| 18 | + textField: PropTypes.string.isRequired, |
| 19 | + button: PropTypes.string.isRequired |
| 20 | + }).isRequired, |
| 21 | + dispatch: PropTypes.func.isRequired, |
| 22 | + user: PropTypes.shape({ |
| 23 | + authentication: PropTypes.shape({ |
| 24 | + accessToken: PropTypes.string.isRequired, |
| 25 | + csrfToken: PropTypes.string.isRequired |
| 26 | + }).isRequired |
| 27 | + }).isRequired, |
| 28 | + experiences: PropTypes.shape({ |
| 29 | + error: PropTypes.string |
| 30 | + }) |
| 31 | + }; |
| 32 | + |
| 33 | + static defaultProps = { |
| 34 | + submitHandler: false, |
| 35 | + experiences: { |
| 36 | + error: null |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + /** |
| 41 | + * Handles a login form submit action. |
| 42 | + * |
| 43 | + * @param {object} event - Submit event object. |
| 44 | + */ |
| 45 | + handleSubmit = event => { |
| 46 | + event.preventDefault(); |
| 47 | + const { dispatch, user } = this.props; |
| 48 | + dispatch({ |
| 49 | + type: EXPERIENCES_CREATE, |
| 50 | + user, |
| 51 | + title: event.target[0].value, |
| 52 | + field_experience_path: event.target[1].value |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheretdoc} |
| 58 | + */ |
| 59 | + render() { |
| 60 | + const { |
| 61 | + classes, |
| 62 | + submitHandler, |
| 63 | + experiences: { error } |
| 64 | + } = this.props; |
| 65 | + return ( |
| 66 | + <form onSubmit={submitHandler || this.handleSubmit}> |
| 67 | + {error && <Message>{error}</Message>} |
| 68 | + <TextField |
| 69 | + id="title" |
| 70 | + label="Title" |
| 71 | + type="text" |
| 72 | + required |
| 73 | + helperText="Enter a user-friendly title for your experience." |
| 74 | + className={classes.textField} |
| 75 | + /> |
| 76 | + <TextField |
| 77 | + id="field_experience_path" |
| 78 | + label="URL Path" |
| 79 | + type="text" |
| 80 | + required |
| 81 | + helperText="Enter a name for your experience. For example, if you enter 'my-new-experience', your experience will be published to /experience/my-new-experience." |
| 82 | + className={classes.textField} |
| 83 | + /> |
| 84 | + <Button |
| 85 | + variant="raised" |
| 86 | + color="primary" |
| 87 | + type="submit" |
| 88 | + className={classes.button} |
| 89 | + > |
| 90 | + Create |
| 91 | + </Button> |
| 92 | + </form> |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export default withStyles(ExperienceCreateFormStyles)(ExperienceCreateForm); |
0 commit comments