180 lines
4.8 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';
2018-04-19 17:19:56 +02:00
import { cloneDeep, isArray, isNull, isUndefined, get, findIndex, includes } 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';
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-06-18 17:23:58 +02:00
this.state = {
isLoading: true,
2018-04-19 17:19:56 +02:00
options: [],
toSkip: 0,
2017-06-18 17:23:58 +02:00
};
}
2018-04-19 17:19:56 +02:00
componentDidMount() {
this.getOptions('');
}
componentDidUpdate(prevProps, prevState) {
if (prevState.toSkip !== this.state.toSkip) {
this.getOptions('');
}
}
2018-04-19 15:45:35 +02:00
getOptions = query => {
const params = {
2018-04-19 17:19:56 +02:00
limit: 20,
skip: this.state.toSkip,
source: this.props.relation.plugin || 'content-manager',
};
2017-06-18 17:23:58 +02:00
// Set `query` parameter if necessary
if (query) {
2018-04-19 17:19:56 +02:00
delete params.limit,
delete params.skip,
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 => ({
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
2018-04-19 17:19:56 +02:00
const newOptions = cloneDeep(this.state.options);
options.map(option => {
// Don't add the values when searching
if (findIndex(newOptions, o => o.value.id === option.value.id) === -1) {
return newOptions.push(option);
}
});
return this.setState({
options: newOptions,
isLoading: false,
});
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(
2018-04-19 17:19:56 +02:00
(data, index) => findIndex(value, o => o.value.id === data.value.id) === index
2018-04-19 15:45:35 +02:00
);
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
};
2018-04-19 17:19:56 +02:00
handleBottomScroll = () => {
this.setState(prevState => {
return {
toSkip: prevState.toSkip + 20,
};
});
}
handleInputChange = (value) => {
const clonedOptions = this.state.options;
const filteredValues = clonedOptions.filter(data => includes(data.label, value));
if (filteredValues.length === 0) {
return this.getOptions(value);
}
}
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 (
<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}
2018-04-19 17:19:56 +02:00
<Select
onChange={this.handleChange}
2018-04-19 17:19:56 +02:00
options={this.state.options}
2017-10-21 15:25:00 +02:00
id={this.props.relation.alias}
2018-04-19 17:19:56 +02:00
isLoading={this.state.isLoading}
onMenuScrollToBottom={this.handleBottomScroll}
onInputChange={this.handleInputChange}
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
}
}
SelectMany.propTypes = {
2018-04-19 15:45:35 +02:00
record: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]).isRequired,
relation: PropTypes.object.isRequired,
setRecordAttribute: PropTypes.func.isRequired,
2017-06-18 17:23:58 +02:00
};
export default SelectMany;