2017-06-18 17:23:58 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* EditFormRelation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import Select from 'react-select';
|
|
|
|
import 'react-select/dist/react-select.css';
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
import request from 'utils/request';
|
|
|
|
|
|
|
|
class EditFormRelation extends React.Component { // eslint-disable-line react/prefer-stateless-function
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.onChange = this.onChange.bind(this);
|
|
|
|
this.getOptions = this.getOptions.bind(this);
|
|
|
|
this.getOptions = this.getOptions.bind(this);
|
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onChange(value) {
|
|
|
|
this.props.setRecordAttribute(this.props.relation.attribute, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
getOptions(query) {
|
|
|
|
// Init `params` object
|
|
|
|
const params = {};
|
|
|
|
|
|
|
|
// Set results limit
|
|
|
|
params.limit = 20;
|
|
|
|
|
|
|
|
// Set `query` parameter if necessary
|
|
|
|
if (query) {
|
|
|
|
params.query = query;
|
|
|
|
params.queryAttribute = this.props.relation.displayedAttribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request URL
|
2017-07-06 17:51:13 +02:00
|
|
|
const requestUrlSuffix = query && this.props.record.get(this.props.relation.attribute) ? this.props.record.get(this.props.relation.attribute) : '';
|
2017-06-19 19:47:38 +02:00
|
|
|
const requestUrl = `/content-manager/explorer/${this.props.relation.model}/${requestUrlSuffix}`;
|
2017-06-18 17:23:58 +02:00
|
|
|
|
|
|
|
// Call our request helper (see 'utils/request')
|
2017-06-19 19:47:38 +02:00
|
|
|
return request(requestUrl, {
|
2017-06-18 17:23:58 +02:00
|
|
|
method: 'GET',
|
|
|
|
params,
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
const options = _.isArray(response)
|
|
|
|
? _.map(response, item => ({
|
2017-07-06 17:51:13 +02:00
|
|
|
value: item.id,
|
2017-06-18 17:23:58 +02:00
|
|
|
label: item[this.props.relation.displayedAttribute],
|
|
|
|
}))
|
|
|
|
: [{
|
2017-07-06 17:51:13 +02:00
|
|
|
value: response.id,
|
2017-06-18 17:23:58 +02:00
|
|
|
label: response[this.props.relation.displayedAttribute],
|
|
|
|
}];
|
|
|
|
|
|
|
|
return {options};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-06-20 19:10:23 +02:00
|
|
|
const description = this.props.relation.description
|
|
|
|
? <p>{this.props.relation.description}</p>
|
|
|
|
: '';
|
|
|
|
|
2017-08-29 17:32:48 +02:00
|
|
|
/* eslint-disable jsx-a11y/label-has-for */
|
2017-06-18 17:23:58 +02:00
|
|
|
return (
|
|
|
|
<div className="form-group">
|
2017-08-29 17:32:48 +02:00
|
|
|
<label htmlFor={this.props.relation.label}>{this.props.relation.label}</label>
|
2017-06-20 19:10:23 +02:00
|
|
|
{description}
|
2017-06-18 17:23:58 +02:00
|
|
|
<Select.Async
|
|
|
|
onChange={this.onChange}
|
|
|
|
loadOptions={this.getOptions}
|
|
|
|
simpleValue
|
|
|
|
value={this.props.record.get(this.props.relation.attribute)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2017-08-29 17:32:48 +02:00
|
|
|
/* eslint-disable jsx-a11y/label-has-for */
|
2017-06-18 17:23:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EditFormRelation.propTypes = {
|
|
|
|
record: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.object,
|
|
|
|
React.PropTypes.bool,
|
2017-08-18 17:02:33 +02:00
|
|
|
]).isRequired,
|
2017-06-18 17:23:58 +02:00
|
|
|
relation: React.PropTypes.object.isRequired,
|
|
|
|
setRecordAttribute: React.PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EditFormRelation;
|