239 lines
6.6 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 { SortableContainer, SortableElement, arrayMove } from 'react-sortable-hoc';
import PropTypes from 'prop-types';
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
// Utils.
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
// CSS.
import 'react-select/dist/react-select.css';
// Icons.
import IconRemove from '../../assets/images/icon_remove.svg';
import styles from './styles.scss';
const SortableItem = SortableElement(({idx, onRemove, item, onClick}) => {
return (
<li className={styles.sortableListItem}>
<div>
<div className={styles.dragHandle}><span></span></div>
<span className="sortable-item--value" onClick={() => onClick(item)}>{item.label}</span>
</div>
<div className={styles.sortableListItemActions}>
<img src={IconRemove} alt="Remove Icon" onClick={() => onRemove(idx)} />
</div>
</li>
);
});
const SortableList = SortableContainer(({items, onRemove, onClick}) => {
return (
<ul className={styles.sortableList}>
{items.map((item, index) => (
<SortableItem key={`item-${index}`} index={index} idx={index} item={item} onRemove={onRemove} onClick={onClick} />
))}
</ul>
);
});
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-05-23 10:57:51 +02:00
_limit: 20,
_start: 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-05-23 10:57:51 +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 requestUrl = `/content-manager/explorer/${this.props.relation.model ||
2018-04-23 11:36:21 +02:00
this.props.relation.collection}`;
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 values = get(this.props.record, this.props.relation.alias) || [];
2018-02-22 10:35:19 +01:00
const target = {
name: `record.${this.props.relation.alias}`,
type: 'select',
value: [...values, value],
2018-02-22 10:35:19 +01:00
};
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);
}
}
handleSortEnd = ({oldIndex, newIndex}) => {
const values = get(this.props.record, this.props.relation.alias);
const target = {
name: `record.${this.props.relation.alias}`,
type: 'select',
value: arrayMove(values, oldIndex, newIndex),
};
this.props.setRecordAttribute({ target });
};
handleRemove = (index) => {
const values = get(this.props.record, this.props.relation.alias);
const target = {
name: `record.${this.props.relation.alias}`,
type: 'select',
value: values.filter( (item, idx) => idx !== index),
};
this.props.setRecordAttribute({ target });
}
// Redirect to the edit page
handleClick = (item = {}) => {
this.props.onRedirect({
model: this.props.relation.collection || this.props.relation.model,
id: item.value.id || item.value._id,
source: this.props.relation.plugin,
});
}
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
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}
/>
<SortableList
items={
2018-04-19 15:45:35 +02:00
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,
};
}
})
}
onSortEnd={this.handleSortEnd}
onRemove={this.handleRemove}
distance={1}
onClick={this.handleClick}
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 = {
onRedirect: PropTypes.func.isRequired,
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;