diff options
Diffstat (limited to 'animism-align/frontend/views/paragraph/components/paragraph.form.js')
| -rw-r--r-- | animism-align/frontend/views/paragraph/components/paragraph.form.js | 196 |
1 files changed, 65 insertions, 131 deletions
diff --git a/animism-align/frontend/views/paragraph/components/paragraph.form.js b/animism-align/frontend/views/paragraph/components/paragraph.form.js index d90b663..de3114c 100644 --- a/animism-align/frontend/views/paragraph/components/paragraph.form.js +++ b/animism-align/frontend/views/paragraph/components/paragraph.form.js @@ -1,153 +1,87 @@ import React, { Component } from 'react' -import { Link } from 'react-router-dom' +// import { Link } from 'react-router-dom' +import { bindActionCreators } from 'redux' +import { connect } from 'react-redux' -import { session } from '../../../session' +import actions from '../../../actions' -import { TextInput, LabelDescription, TextArea, Checkbox, SubmitButton, Loader } from '../../../common' +import { clamp, timestamp, capitalize } from '../../../util' +import { Select } from '../../../common' -const newGraph = () => ({ - path: '', - title: '', - username: session('username'), - description: '', -}) +const PARAGRAPH_TYPES = [ + 'paragraph', 'blockquote', 'hidden', +].map(name => ({ name, label: capitalize(name.replace('_', ' ')) })) -export default class GraphForm extends Component { - state = { - title: "", - submitTitle: "", - data: { ...newGraph() }, - errorFields: new Set([]), +class ParagraphForm extends Component { + constructor(props){ + super(props) + this.handleChange = this.handleChange.bind(this) + this.handleSelect = this.handleSelect.bind(this) + this.handleSubmit = this.handleSubmit.bind(this) } - componentDidMount() { - const { data, isNew } = this.props - const title = isNew ? 'new project' : 'editing ' + data.title - const submitTitle = isNew ? "Create Graph" : "Save Changes" - this.setState({ - title, - submitTitle, - errorFields: new Set([]), - data: { - ...newGraph(), - ...data - }, - }) + if (this.textareaRef && this.textareaRef.current) { + this.textareaRef.current.focus() + } } - handleChange(e) { - const { errorFields } = this.state const { name, value } = e.target - if (errorFields.has(name)) { - errorFields.delete(name) - } - let sanitizedValue = value - if (name === 'path') { - sanitizedValue = sanitizedValue.toLowerCase().replace(/ /, '-').replace(/[!@#$%^&*()[\]{}]/, '-').replace(/-+/, '-') - } - this.setState({ - errorFields, - data: { - ...this.state.data, - [name]: sanitizedValue, - } - }) + this.handleSelect(name, value) } - handleSelect(name, value) { - const { errorFields } = this.state - if (errorFields.has(name)) { - errorFields.delete(name) - } - this.setState({ - errorFields, - data: { - ...this.state.data, - [name]: value, - } + const { onUpdate, paragraph } = this.props + onUpdate({ + ...paragraph, + [name]: value, }) } - - handleSubmit(e) { - e.preventDefault() - const { isNew, onSubmit } = this.props - const { data } = this.state - const requiredKeys = "title username path description".split(" ") - const validKeys = "title username path description".split(" ") - const validData = validKeys.reduce((a,b) => { a[b] = data[b]; return a }, {}) - const errorFields = requiredKeys.filter(key => !validData[key]) - if (errorFields.length) { - console.log('error', errorFields, validData) - this.setState({ errorFields: new Set(errorFields) }) - } else { - if (isNew) { - // side effect: set username if we're creating a new graph - session.set('username', data.username) - } else { - validData.id = data.id - } - console.log('submit', validData) - onSubmit(validData) - } + handleSubmit() { + const { paragraph, onClose } = this.props + actions.paragraph.update(paragraph) + .then(response => { + console.log(response) + onClose() + }) } - render() { - const { isNew } = this.props - const { title, submitTitle, errorFields, data } = this.state + const { paragraph, y } = this.props return ( - <div className='form'> - <h1>{title}</h1> - <form onSubmit={this.handleSubmit.bind(this)}> - <TextInput - title="Path" - name="path" - required - data={data} - error={errorFields.has('path')} - onChange={this.handleChange.bind(this)} - autoComplete="off" - /> - <LabelDescription> - {data.path - ? 'Project URLs will be: /' + data.path + '/example' - : 'Enter the base path for this project.'} - </LabelDescription> - <TextInput - title="Title" - name="title" - required - data={data} - error={errorFields.has('title')} - onChange={this.handleChange.bind(this)} - autoComplete="off" - /> - <TextInput - title="Author" - name="username" - required - data={data} - error={errorFields.has('username')} - onChange={this.handleChange.bind(this)} - autoComplete="off" - /> - <TextArea - title="Description" - name="description" - data={data} - onChange={this.handleChange.bind(this)} - /> - <SubmitButton - title={submitTitle} - onClick={this.handleSubmit.bind(this)} + <div + className='paragraphForm' + style={{ + top: y, + }} + > + {this.renderButtons()} + </div> + ) + } + renderButtons() { + const { paragraph } = this.props + return ( + <div className='row buttons'> + <div className='row'> + <Select + name='type' + selected={paragraph.type} + options={PARAGRAPH_TYPES} + defaultOption='text' + onChange={this.handleSelect} /> - {!!errorFields.size && - <label> - <span></span> - <span>Please complete the required fields =)</span> - </label> - } - </form> + <div className='ts'>{timestamp(paragraph.start_ts, 1, true)}</div> + </div> + <div> + <button onClick={this.handleSubmit}>Save</button> + </div> </div> ) } } + +const mapStateToProps = state => ({ +}) + +const mapDispatchToProps = dispatch => ({ +}) + +export default connect(mapStateToProps, mapDispatchToProps)(ParagraphForm) |
