-
Notifications
You must be signed in to change notification settings - Fork 182
Add Extra Field to Live API #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,11 @@ var React = require('react'); | |
|
|
||
| var Input = React.createClass({ | ||
|
|
||
| removeField: function (fieldName, event) { | ||
| event.preventDefault(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why preventDefault There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is placed within a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| this.props.removeField(fieldName); | ||
| }, | ||
|
|
||
| handleChange: function (value) { | ||
| this.props.onChange(value); | ||
| }, | ||
|
|
@@ -12,6 +17,13 @@ var Input = React.createClass({ | |
| <label | ||
| htmlFor={this.props.name} | ||
| className="col-sm-4 control-label"> | ||
| {this.props.isCustom ? ( | ||
| <i | ||
| className='fa fa-minus-circle' | ||
| title='Remove Field' | ||
| onClick={this.removeField.bind(this, this.props.name)} | ||
| /> | ||
| ) : null} | ||
| {this.props.name} | ||
| </label> | ||
| <div className="col-sm-8"> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| var _ = require('underscore'); | ||
| var React = require('react'); | ||
|
|
||
| var AddFieldsForm = require('./request/add-fields'); | ||
| var Header = require('./helpers/header'); | ||
| var Headers = require('./request/headers'); | ||
| var FieldsData = require('./request/fields-data'); | ||
|
|
@@ -29,6 +31,42 @@ var Request = React.createClass({ | |
| }); | ||
| }, | ||
|
|
||
| addField: function (fieldName) { | ||
| var endpoint = this.state.endpoint; | ||
| var fields = endpoint.fields; | ||
|
|
||
| // Check if field already exists | ||
| if (_.findWhere(fields, {'name': fieldName})) return; | ||
|
|
||
| fields.push({ | ||
| name: fieldName, | ||
| required: false, | ||
| type: 'Added Field', | ||
| isCustom: true | ||
| }); | ||
|
|
||
| endpoint.fields = fields; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would _.extend({}, here to avoid mutation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait. No need to do this... Push mutates the array anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| this.setState({ | ||
| endpoint: endpoint | ||
| }); | ||
| }, | ||
|
|
||
| removeField: function (fieldName) { | ||
| var data = this.state.data; | ||
| var endpoint = this.state.endpoint; | ||
| var fields = endpoint.fields; | ||
|
|
||
| data = _.omit(data, fieldName); | ||
| fields = _.without(fields, _.findWhere(fields, {name: fieldName})); | ||
| endpoint.fields = fields; | ||
|
|
||
| this.setState({ | ||
| data: data, | ||
| endpoint: endpoint | ||
| }); | ||
| }, | ||
|
|
||
| setSelectedMethod: function (method) { | ||
| this.setState({ | ||
| selectedMethod: method | ||
|
|
@@ -87,7 +125,10 @@ var Request = React.createClass({ | |
| <FieldsData | ||
| fields={endpoint.fields} | ||
| data={this.state.data} | ||
| removeCustomField={this.removeField} | ||
| onChange={this.handleDataFieldChange} /> | ||
|
|
||
| <AddFieldsForm onAdd={this.addField} /> | ||
| </div> | ||
| )} | ||
| </div> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| var React = require('react'); | ||
|
|
||
| var Header = require('../helpers/header'); | ||
|
|
||
| var AddFieldsForm = React.createClass({ | ||
|
|
||
| getInitialState: function() { | ||
| return { | ||
| fieldName: '' | ||
| }; | ||
| }, | ||
|
|
||
| addField: function () { | ||
| if (this.state.fieldName) { | ||
| this.props.onAdd(this.state.fieldName); | ||
| this.setState({ | ||
| fieldName: '' | ||
| }); | ||
| } | ||
| }, | ||
|
|
||
| handleKeyPress: function (event) { | ||
| if (event.key === 'Enter') { | ||
| event.preventDefault(); | ||
| this.addField(); | ||
| } | ||
| }, | ||
|
|
||
| handleChange: function (event) { | ||
| this.setState({ | ||
| fieldName: event.target.value | ||
| }); | ||
| }, | ||
|
|
||
| render: function () { | ||
| return ( | ||
| <div> | ||
| <Header title='Add Extra Fields' /> | ||
|
|
||
| <div className='form-group'> | ||
| <label className='col-sm-4 control-label'>Field Name</label> | ||
| <div className="col-sm-6"> | ||
| <input | ||
| type='text' | ||
| className='form-control input-sm' | ||
| placeholder='ie. email_address' | ||
| onKeyPress = {this.handleKeyPress} | ||
| onChange={this.handleChange} | ||
| value={this.state.fieldName} /> | ||
| </div> | ||
| <div className="col-sm-2"> | ||
| <button | ||
| type="button" | ||
| className='btn btn-sm btn-block btn-primary' | ||
| onClick={this.addField}> | ||
| Add | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| module.exports = AddFieldsForm; |