|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import _ from 'lodash'; |
| 4 | + |
| 5 | +class CreateTweetDialog extends React.Component { |
| 6 | + |
| 7 | + static propTypes = { |
| 8 | + title: PropTypes.node, |
| 9 | + description: PropTypes.node |
| 10 | + }; |
| 11 | + |
| 12 | + constructor(props) { |
| 13 | + super(props); |
| 14 | + |
| 15 | + // set initial state |
| 16 | + this.state = { |
| 17 | + data: { |
| 18 | + text: '' |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + // bind custom methods |
| 23 | + this.show = this.show.bind(this); |
| 24 | + this.dismiss = this.dismiss.bind(this); |
| 25 | + this.request = this.request.bind(this); |
| 26 | + this.onSubmit = this.onSubmit.bind(this); |
| 27 | + this.onChange = this.onChange.bind(this); |
| 28 | + } |
| 29 | + |
| 30 | + componentDidMount() { |
| 31 | + this.show(); |
| 32 | + } |
| 33 | + |
| 34 | + show() { |
| 35 | + const modal = this.refs.modal; |
| 36 | + $(modal).modal('show'); |
| 37 | + } |
| 38 | + |
| 39 | + dismiss() { |
| 40 | + const modal = this.refs.modal; |
| 41 | + $(modal).modal('hide'); |
| 42 | + } |
| 43 | + |
| 44 | + request(data) { |
| 45 | + lore.actions.tweet.create(data); |
| 46 | + } |
| 47 | + |
| 48 | + onSubmit() { |
| 49 | + const { data } = this.state; |
| 50 | + this.request(data); |
| 51 | + this.dismiss(); |
| 52 | + } |
| 53 | + |
| 54 | + onChange(name, value) { |
| 55 | + const nextData = _.merge({}, this.state.data); |
| 56 | + nextData[name] = value; |
| 57 | + this.setState({ |
| 58 | + data: nextData |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + render() { |
| 63 | + const { data } = this.state; |
| 64 | + |
| 65 | + return ( |
| 66 | + <div ref="modal" className="modal fade"> |
| 67 | + <div className="modal-dialog"> |
| 68 | + <div className="modal-content"> |
| 69 | + <div className="modal-header"> |
| 70 | + <button type="button" className="close" onClick={this.dismiss}> |
| 71 | + <span>×</span> |
| 72 | + </button> |
| 73 | + <h4 className="modal-title"> |
| 74 | + Create Tweet |
| 75 | + </h4> |
| 76 | + </div> |
| 77 | + <div className="modal-body"> |
| 78 | + <div className="row"> |
| 79 | + <div className="col-md-12"> |
| 80 | + <div className="form-group"> |
| 81 | + <label>Message</label> |
| 82 | + <textarea |
| 83 | + className="form-control" |
| 84 | + rows="3" |
| 85 | + value={data.text} |
| 86 | + placeholder="What's happening?" |
| 87 | + onChange={(event) => { |
| 88 | + this.onChange('text', event.target.value) |
| 89 | + }} |
| 90 | + /> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + <div className="modal-footer"> |
| 96 | + <div className="row"> |
| 97 | + <div className="col-md-12"> |
| 98 | + <button |
| 99 | + type="button" |
| 100 | + className="btn btn-default" |
| 101 | + onClick={this.dismiss} |
| 102 | + > |
| 103 | + Cancel |
| 104 | + </button> |
| 105 | + <button |
| 106 | + type="button" |
| 107 | + className="btn btn-primary" |
| 108 | + disabled={!data.text} |
| 109 | + onClick={this.onSubmit} |
| 110 | + > |
| 111 | + Create |
| 112 | + </button> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +export default CreateTweetDialog; |
0 commit comments