2017-06-18 17:23:58 +02:00
|
|
|
/**
|
|
|
|
*
|
2017-09-07 17:16:31 +02:00
|
|
|
* SelectMany
|
2017-06-18 17:23:58 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-09-26 16:36:28 +02:00
|
|
|
import React from 'react';
|
2017-06-18 17:23:58 +02:00
|
|
|
import Select from 'react-select';
|
2017-09-18 09:34:29 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2017-06-18 17:23:58 +02:00
|
|
|
import 'react-select/dist/react-select.css';
|
2017-10-21 15:25:00 +02:00
|
|
|
import { isArray, isNull, isUndefined, get, findIndex } from 'lodash';
|
2017-06-18 17:23:58 +02:00
|
|
|
|
|
|
|
import request from 'utils/request';
|
2017-09-07 17:16:31 +02:00
|
|
|
import templateObject from 'utils/templateObject';
|
2017-06-18 17:23:58 +02:00
|
|
|
|
2017-09-07 17:16:31 +02:00
|
|
|
import styles from './styles.scss';
|
|
|
|
|
2018-04-19 15:45:35 +02:00
|
|
|
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-09-07 17:16:31 +02:00
|
|
|
|
2017-06-18 17:23:58 +02:00
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-19 15:45:35 +02:00
|
|
|
getOptions = query => {
|
2017-09-07 17:16:31 +02:00
|
|
|
const params = {
|
2018-04-19 15:45:35 +02:00
|
|
|
_limit: 20,
|
2017-12-11 18:23:15 +01:00
|
|
|
source: this.props.relation.plugin || 'content-manager',
|
2017-09-07 17:16:31 +02:00
|
|
|
};
|
2017-06-18 17:23:58 +02:00
|
|
|
|
|
|
|
// Set `query` parameter if necessary
|
|
|
|
if (query) {
|
2018-04-19 15:45:35 +02:00
|
|
|
params[`${this.props.relation.displayedAttribute}_contains`] = query;
|
2017-06-18 17:23:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Request URL
|
2018-04-19 15:45:35 +02:00
|
|
|
const requestUrlSuffix =
|
|
|
|
query && get(this.props.record, [this.props.relation.alias])
|
|
|
|
? get(this.props.record, [this.props.relation.alias])
|
|
|
|
: '';
|
|
|
|
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 => {
|
2018-04-19 15:45:35 +02:00
|
|
|
const options = isArray(response)
|
|
|
|
? response.map(item => ({
|
2017-09-07 17:16:31 +02:00
|
|
|
value: item,
|
2018-04-19 15:45:35 +02:00
|
|
|
label: templateObject({ mainField: this.props.relation.displayedAttribute }, item)
|
|
|
|
.mainField,
|
|
|
|
}))
|
|
|
|
: [
|
|
|
|
{
|
|
|
|
value: response,
|
|
|
|
label: response[this.props.relation.displayedAttribute],
|
|
|
|
},
|
|
|
|
];
|
2017-06-18 17:23:58 +02:00
|
|
|
|
2017-09-07 17:16:31 +02:00
|
|
|
return { options };
|
2017-09-25 15:35:27 +02:00
|
|
|
})
|
|
|
|
.catch(() => {
|
2017-12-07 13:26:25 +01:00
|
|
|
strapi.notification.error('content-manager.notification.error.relationship.fetch');
|
2017-06-18 17:23:58 +02:00
|
|
|
});
|
2018-04-19 15:45:35 +02:00
|
|
|
};
|
2017-06-18 17:23:58 +02:00
|
|
|
|
2018-04-19 15:45:35 +02:00
|
|
|
handleChange = value => {
|
|
|
|
const filteredValue = value.filter(
|
|
|
|
(data, index) => findIndex(value, o => o.value.id === data.value.id) === index,
|
|
|
|
);
|
2018-02-22 10:35:19 +01:00
|
|
|
const target = {
|
|
|
|
name: `record.${this.props.relation.alias}`,
|
|
|
|
type: 'select',
|
|
|
|
value: filteredValue,
|
|
|
|
};
|
2017-10-21 15:25:00 +02:00
|
|
|
|
2018-02-22 10:35:19 +01:00
|
|
|
this.props.setRecordAttribute({ target });
|
2018-04-19 15:45:35 +02:00
|
|
|
};
|
2017-09-19 14:07:58 +02:00
|
|
|
|
2017-06-18 17:23:58 +02:00
|
|
|
render() {
|
2018-04-19 15:45:35 +02:00
|
|
|
const description = this.props.relation.description ? (
|
|
|
|
<p>{this.props.relation.description}</p>
|
|
|
|
) : (
|
|
|
|
''
|
|
|
|
);
|
2017-06-20 19:10:23 +02:00
|
|
|
|
2018-02-22 10:35:19 +01:00
|
|
|
const value = get(this.props.record, 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 (
|
2017-09-07 17:16:31 +02:00
|
|
|
<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
|
2017-09-19 14:07:58 +02:00
|
|
|
onChange={this.handleChange}
|
2017-06-18 17:23:58 +02:00
|
|
|
loadOptions={this.getOptions}
|
2017-10-21 15:25:00 +02:00
|
|
|
id={this.props.relation.alias}
|
2017-09-07 17:16:31 +02:00
|
|
|
multi
|
2018-04-19 15:45:35 +02:00
|
|
|
value={
|
|
|
|
isNull(value) || isUndefined(value) || value.size === 0
|
|
|
|
? null
|
|
|
|
: value.map(item => {
|
|
|
|
if (item) {
|
|
|
|
return {
|
|
|
|
value: get(item, 'value') || item,
|
|
|
|
label:
|
|
|
|
get(item, 'label') ||
|
|
|
|
templateObject({ mainField: this.props.relation.displayedAttribute }, item)
|
|
|
|
.mainField ||
|
|
|
|
item.value.id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 17:16:31 +02:00
|
|
|
SelectMany.propTypes = {
|
2018-04-19 15:45:35 +02:00
|
|
|
record: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]).isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
relation: PropTypes.object.isRequired,
|
|
|
|
setRecordAttribute: PropTypes.func.isRequired,
|
2017-06-18 17:23:58 +02:00
|
|
|
};
|
|
|
|
|
2017-09-07 17:16:31 +02:00
|
|
|
export default SelectMany;
|