Skip to content

Commit d214630

Browse files
author
Emmanouil Konstantinidis
committed
Active method
1 parent 9efd6d3 commit d214630

File tree

4 files changed

+1655
-52
lines changed

4 files changed

+1655
-52
lines changed

rest_framework_docs/static/rest_framework_docs/js/components/liveapi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var LiveAPIEndpoints = React.createClass({
2121
},
2222

2323
getData: function () {
24-
var method = this.refs.request.state.method;
24+
var method = this.refs.request.state.selectedMethod;
2525
return RequestUtils.shouldAddData(method) ? null : (
2626
this.refs.request.state.data
2727
);
@@ -35,7 +35,7 @@ var LiveAPIEndpoints = React.createClass({
3535
var data = this.getData();
3636

3737
// Now Make the Request
38-
APIRequest(request.method, request.urlEndpoint)
38+
APIRequest(request.selectedMethod, request.urlEndpoint)
3939
.send(data)
4040
.end(function (err, res) {
4141
self.setState({

rest_framework_docs/static/rest_framework_docs/js/components/request.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var _ = require('underscore');
12
var React = require('react');
23

34
var FieldsData = require('./request/fields-data');
@@ -7,24 +8,46 @@ var Methods = require('./request/methods');
78
var RequestUtils = require('../utils/request');
89

910
var Request = React.createClass({
10-
1111
getInitialState: function () {
1212
return {
13+
endpoint: {},
1314
data: {},
14-
method: null,
15-
urlEndpoint: this.props.endpoint.path
15+
selectedMethod: null,
1616
};
1717
},
1818

19-
componentWillReceiveProps: function(nextProps) {
19+
componentWillMount: function() {
20+
var endpoint = this.props.endpoint;
21+
endpoint['methods'] = this.transformMethods(endpoint.methods);
22+
23+
this.setState({
24+
endpoint: endpoint,
25+
selectedMethod: endpoint['methods'][0]
26+
});
27+
},
28+
29+
componentWillReceiveProps: function (nextProps) {
30+
var endpoint = nextProps.endpoint;
31+
endpoint['methods'] = _.isArray(endpoint.methods) ? endpoint.methods : this.transformMethods(endpoint.methods);
32+
2033
this.setState({
21-
urlEndpoint: nextProps.endpoint.path
34+
endpoint: endpoint,
35+
selectedMethod: endpoint['methods'][0]
2236
});
2337
},
2438

25-
setMethod: function (method) {
39+
transformMethods: function (methods) {
40+
return methods
41+
.replace(/\W+/g, ' ')
42+
.replace(/^[ ]+|[ ]+$/g,'')
43+
.split(' ');
44+
},
45+
46+
setSelectedMethod: function (method) {
47+
console.log('REQUEST _ setSelectedMethod');
48+
2649
this.setState({
27-
method: method
50+
selectedMethod: method
2851
});
2952
},
3053

@@ -43,7 +66,7 @@ var Request = React.createClass({
4366
},
4467

4568
render: function () {
46-
var endpoint = this.props.endpoint;
69+
var endpoint = this.state.endpoint;
4770

4871
return (
4972
<div>
@@ -52,11 +75,14 @@ var Request = React.createClass({
5275
<Header title='API Endpoint' />
5376
<FieldUrl
5477
name='urlEndpoint'
55-
url={this.state.urlEndpoint}
78+
url={endpoint.path}
5679
onChange={this.handleInputChange.bind(this, 'urlEndpoint')} />
5780

5881
<Header title='Method' />
59-
<Methods methods={endpoint.methods} active={this.state.method} setMethod={this.setMethod} />
82+
<Methods
83+
methods={this.state.endpoint.methods}
84+
selectedMethod={this.state.selectedMethod}
85+
setMethod={this.setSelectedMethod} />
6086

6187
<Header title='Headers' />
6288
<div className="form-group">

rest_framework_docs/static/rest_framework_docs/js/components/request/methods.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@ var Methods = React.createClass({
44

55
getInitialState: function() {
66
return {
7-
methods: []
7+
methods: [],
8+
selectedMethod: null
89
};
910
},
1011

1112
componentWillMount: function() {
12-
var methods = this.props.methods
13-
.replace(/\W+/g, ' ')
14-
.replace(/^[ ]+|[ ]+$/g,'')
15-
.split(' ');
16-
1713
this.setState({
18-
methods: methods
14+
methods: this.props.methods,
15+
selectedMethod: this.props.selectedMethod
1916
});
17+
},
2018

21-
this.props.setMethod(methods[0]);
19+
componentWillReceiveProps: function(nextProps) {
20+
this.setState({
21+
methods: nextProps.methods,
22+
selectedMethod: nextProps.selectedMethod
23+
});
2224
},
2325

2426
setMethod: function (value) {
2527
this.props.setMethod(value);
2628
},
2729

2830
render: function () {
29-
// var self = this;
30-
3131
return (
3232
<div className='btn-group methods'>
3333
{this.state.methods.map(function (method, i) {
3434
var methodClass = 'btn btn-sm method ' + method.toLowerCase() +
35-
(this.props.active == method ? ' active' : null);
35+
(this.state.selectedMethod == method ? ' active' : null);
3636
return (
3737
<button
3838
key={i}

0 commit comments

Comments
 (0)