106 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-06-18 17:23:58 +02:00
/**
*
* SelectMany
2017-06-18 17:23:58 +02:00
*
*/
import React from 'react';
2017-06-18 17:23:58 +02:00
import Select from 'react-select';
import PropTypes from 'prop-types';
2017-06-18 17:23:58 +02:00
import 'react-select/dist/react-select.css';
import { isArray, isNull, isUndefined, get } from 'lodash';
2017-06-18 17:23:58 +02:00
import request from 'utils/request';
import templateObject from 'utils/templateObject';
2017-06-18 17:23:58 +02:00
import styles from './styles.scss';
class SelectMany extends React.Component { // eslint-disable-line react/prefer-stateless-function
2017-06-18 17:23:58 +02:00
constructor(props) {
super(props);
2017-06-18 17:23:58 +02:00
this.state = {
isLoading: true,
};
}
getOptions = (query) => {
const params = {
limit: 20,
};
2017-06-18 17:23:58 +02:00
// Set `query` parameter if necessary
if (query) {
params.query = query;
params.queryAttribute = this.props.relation.displayedAttribute;
}
// Request URL
const requestUrlSuffix = query && this.props.record.get(this.props.relation.alias).toJS() ? this.props.record.get(this.props.relation.alias).toJS() : '';
const requestUrl = `/content-manager/explorer/${this.props.relation.model || this.props.relation.collection}/${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) ?
response.map(item => ({
value: item,
label: templateObject({ mainField: this.props.relation.displayedAttribute }, item).mainField,
})) :
[{
value: response,
2017-06-18 17:23:58 +02:00
label: response[this.props.relation.displayedAttribute],
}];
return { options };
2017-09-25 15:35:27 +02:00
})
.catch(() => {
window.Strapi.notification.error('An error occurred during relationship fetch.');
2017-06-18 17:23:58 +02:00
});
}
handleChange = (value) => {
this.props.setRecordAttribute(this.props.relation.alias, value);
}
2017-06-18 17:23:58 +02:00
render() {
2017-06-20 19:10:23 +02:00
const description = this.props.relation.description
? <p>{this.props.relation.description}</p>
: '';
const value = this.props.record.get(this.props.relation.alias);
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 ${styles.selectMany}`}>
<label htmlFor={this.props.relation.alias}>{this.props.relation.alias}</label>
2017-06-20 19:10:23 +02:00
{description}
2017-06-18 17:23:58 +02:00
<Select.Async
onChange={this.handleChange}
2017-06-18 17:23:58 +02:00
loadOptions={this.getOptions}
multi
value={isNull(value) || isUndefined(value) || value.size === 0 ? null : value.toJS().map(item => ({
value: get(item, 'value') || item,
label: item.label || templateObject({ mainField: this.props.relation.displayedAttribute }, item).mainField,
}))}
2017-06-18 17:23:58 +02:00
/>
</div>
);
2017-08-29 17:32:48 +02:00
/* eslint-disable jsx-a11y/label-has-for */
2017-06-18 17:23:58 +02:00
}
}
SelectMany.propTypes = {
record: PropTypes.oneOfType([
PropTypes.object,
PropTypes.bool,
2017-08-18 17:02:33 +02:00
]).isRequired,
relation: PropTypes.object.isRequired,
setRecordAttribute: PropTypes.func.isRequired,
2017-06-18 17:23:58 +02:00
};
export default SelectMany;