2017-11-07 16:33:15 +01:00
|
|
|
/**
|
2019-04-16 16:55:53 +02:00
|
|
|
*
|
|
|
|
* InputSearchContainer
|
|
|
|
*
|
|
|
|
*/
|
2017-11-07 16:33:15 +01:00
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
2018-01-08 14:15:27 +01:00
|
|
|
import { findIndex, has, includes, isEmpty, map, toLower } from 'lodash';
|
2017-11-07 16:33:15 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2019-04-16 16:55:53 +02:00
|
|
|
import { Label } from 'strapi-helper-plugin';
|
2019-02-22 11:16:42 +01:00
|
|
|
import InputSearchLi from '../InputSearchLi';
|
2017-11-07 18:16:42 +01:00
|
|
|
|
2019-09-17 16:20:45 +02:00
|
|
|
import { Addon, List, Wrapper } from './Components';
|
2019-09-17 15:24:22 +02:00
|
|
|
|
2019-04-16 16:55:53 +02:00
|
|
|
class InputSearchContainer extends React.Component {
|
|
|
|
// eslint-disable-line react/prefer-stateless-function
|
2017-11-23 13:09:05 +01:00
|
|
|
state = {
|
|
|
|
errors: [],
|
|
|
|
filteredUsers: this.props.values,
|
|
|
|
isAdding: false,
|
2018-02-06 18:42:42 +01:00
|
|
|
isFocused: false,
|
2017-11-23 13:09:05 +01:00
|
|
|
users: this.props.values,
|
|
|
|
value: '',
|
|
|
|
};
|
2017-11-07 18:16:42 +01:00
|
|
|
|
2019-08-13 11:31:10 +02:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2017-11-07 18:16:42 +01:00
|
|
|
if (nextProps.didDeleteUser !== this.props.didDeleteUser) {
|
2019-04-16 16:55:53 +02:00
|
|
|
this.setState({
|
|
|
|
users: nextProps.values,
|
|
|
|
filteredUsers: nextProps.values,
|
|
|
|
});
|
2017-11-07 18:16:42 +01:00
|
|
|
}
|
2017-11-15 14:00:51 +01:00
|
|
|
|
|
|
|
if (nextProps.didGetUsers !== this.props.didGetUsers) {
|
2019-04-16 16:55:53 +02:00
|
|
|
this.setState({
|
|
|
|
users: nextProps.values,
|
|
|
|
filteredUsers: nextProps.values,
|
|
|
|
});
|
2017-11-15 14:00:51 +01:00
|
|
|
}
|
2017-11-23 12:43:40 +01:00
|
|
|
|
|
|
|
if (nextProps.didFetchUsers !== this.props.didFetchUsers) {
|
2017-11-23 13:09:05 +01:00
|
|
|
this.setState({ filteredUsers: nextProps.users, isAdding: true });
|
2017-11-23 12:43:40 +01:00
|
|
|
}
|
2017-11-07 18:16:42 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 18:42:42 +01:00
|
|
|
handleBlur = () => this.setState({ isFocused: !this.state.isFocused });
|
|
|
|
|
2017-11-07 18:16:42 +01:00
|
|
|
handleChange = ({ target }) => {
|
2019-04-16 16:55:53 +02:00
|
|
|
const filteredUsers = isEmpty(target.value)
|
|
|
|
? this.state.users
|
|
|
|
: this.state.users.filter(user =>
|
2019-08-13 11:31:10 +02:00
|
|
|
includes(toLower(user.name), toLower(target.value))
|
|
|
|
);
|
2017-11-23 12:43:40 +01:00
|
|
|
|
|
|
|
if (isEmpty(filteredUsers) && !isEmpty(target.value)) {
|
|
|
|
this.props.getUser(target.value);
|
|
|
|
}
|
|
|
|
|
2017-11-23 13:09:05 +01:00
|
|
|
if (isEmpty(target.value)) {
|
2019-04-16 16:55:53 +02:00
|
|
|
return this.setState({
|
|
|
|
value: target.value,
|
|
|
|
isAdding: false,
|
|
|
|
users: this.props.values,
|
|
|
|
filteredUsers: this.props.values,
|
|
|
|
});
|
2017-11-23 13:09:05 +01:00
|
|
|
}
|
|
|
|
|
2017-11-07 18:16:42 +01:00
|
|
|
this.setState({ value: target.value, filteredUsers });
|
2019-04-16 16:55:53 +02:00
|
|
|
};
|
2017-11-07 16:33:15 +01:00
|
|
|
|
2018-02-06 18:42:42 +01:00
|
|
|
handleFocus = () => this.setState({ isFocused: !this.state.isFocused });
|
|
|
|
|
2019-04-16 16:55:53 +02:00
|
|
|
handleClick = item => {
|
2017-11-23 13:09:05 +01:00
|
|
|
if (this.state.isAdding) {
|
2017-11-29 12:36:35 +01:00
|
|
|
const id = has(item, '_id') ? '_id' : 'id';
|
|
|
|
const users = this.props.values;
|
|
|
|
// Check if user is already associated with this role
|
2017-12-08 10:42:45 +01:00
|
|
|
if (findIndex(users, [id, item[id]]) === -1) {
|
2017-11-29 12:36:35 +01:00
|
|
|
this.props.onClickAdd(item);
|
|
|
|
users.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the input focus
|
|
|
|
this.searchInput.focus();
|
|
|
|
// Empty the input and display users
|
2019-04-16 16:55:53 +02:00
|
|
|
this.setState({
|
|
|
|
value: '',
|
|
|
|
isAdding: false,
|
|
|
|
users,
|
|
|
|
filteredUsers: users,
|
|
|
|
});
|
2017-11-23 13:09:05 +01:00
|
|
|
} else {
|
|
|
|
this.props.onClickDelete(item);
|
|
|
|
}
|
2019-04-16 16:55:53 +02:00
|
|
|
};
|
2017-11-23 13:09:05 +01:00
|
|
|
|
2017-11-07 16:33:15 +01:00
|
|
|
render() {
|
|
|
|
return (
|
2019-09-17 15:24:22 +02:00
|
|
|
<Wrapper className="col-md-6">
|
2018-02-06 18:42:42 +01:00
|
|
|
<Label htmlFor={this.props.name} message={this.props.label} />
|
2019-09-17 15:24:22 +02:00
|
|
|
<div className="input-group">
|
2019-09-17 16:20:45 +02:00
|
|
|
<Addon
|
|
|
|
className={`input-group-addon ${this.state.isFocused && 'focus'}`}
|
|
|
|
>
|
2019-09-17 17:36:03 +02:00
|
|
|
<i className="fas fa-search"></i>
|
2019-09-17 16:20:45 +02:00
|
|
|
</Addon>
|
2017-11-07 16:33:15 +01:00
|
|
|
<FormattedMessage id="users-permissions.InputSearch.placeholder">
|
2019-04-16 16:55:53 +02:00
|
|
|
{message => (
|
2017-11-07 16:33:15 +01:00
|
|
|
<input
|
2019-09-17 16:20:45 +02:00
|
|
|
className={`form-control ${
|
2019-08-13 11:31:10 +02:00
|
|
|
!isEmpty(this.state.errors) ? 'is-invalid' : ''
|
2019-09-17 16:20:45 +02:00
|
|
|
}`}
|
2017-11-07 16:33:15 +01:00
|
|
|
id={this.props.name}
|
|
|
|
name={this.props.name}
|
2018-02-06 18:42:42 +01:00
|
|
|
onBlur={this.handleBlur}
|
2017-11-07 18:16:42 +01:00
|
|
|
onChange={this.handleChange}
|
2018-02-06 18:42:42 +01:00
|
|
|
onFocus={this.handleFocus}
|
2017-11-07 18:16:42 +01:00
|
|
|
value={this.state.value}
|
2017-11-07 16:33:15 +01:00
|
|
|
placeholder={message}
|
|
|
|
type="text"
|
2019-04-16 16:55:53 +02:00
|
|
|
ref={input => {
|
|
|
|
this.searchInput = input;
|
|
|
|
}}
|
2017-11-07 16:33:15 +01:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</FormattedMessage>
|
|
|
|
</div>
|
2019-09-17 16:20:45 +02:00
|
|
|
<List className={this.state.isFocused && 'focused'}>
|
2017-11-07 18:16:42 +01:00
|
|
|
<ul>
|
2019-04-16 16:55:53 +02:00
|
|
|
{map(this.state.filteredUsers, user => (
|
2017-11-23 13:09:05 +01:00
|
|
|
<InputSearchLi
|
2017-11-27 14:26:54 +01:00
|
|
|
key={user.id || user._id}
|
2017-11-23 13:09:05 +01:00
|
|
|
item={user}
|
|
|
|
isAdding={this.state.isAdding}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
/>
|
2017-11-07 18:16:42 +01:00
|
|
|
))}
|
|
|
|
</ul>
|
2019-09-17 16:20:45 +02:00
|
|
|
</List>
|
2019-09-17 15:24:22 +02:00
|
|
|
</Wrapper>
|
2017-11-07 16:33:15 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 18:42:42 +01:00
|
|
|
InputSearchContainer.defaultProps = {
|
2017-11-23 12:43:40 +01:00
|
|
|
users: [],
|
2017-11-08 16:06:21 +01:00
|
|
|
values: [],
|
|
|
|
};
|
2017-11-07 16:33:15 +01:00
|
|
|
|
2018-02-06 18:42:42 +01:00
|
|
|
InputSearchContainer.propTypes = {
|
2017-11-08 16:06:21 +01:00
|
|
|
didDeleteUser: PropTypes.bool.isRequired,
|
2017-11-23 12:43:40 +01:00
|
|
|
didFetchUsers: PropTypes.bool.isRequired,
|
2017-11-15 14:00:51 +01:00
|
|
|
didGetUsers: PropTypes.bool.isRequired,
|
2017-11-23 12:43:40 +01:00
|
|
|
getUser: PropTypes.func.isRequired,
|
2018-02-06 18:42:42 +01:00
|
|
|
label: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
params: PropTypes.object,
|
|
|
|
}).isRequired,
|
2017-11-07 16:33:15 +01:00
|
|
|
name: PropTypes.string.isRequired,
|
2017-11-23 13:09:05 +01:00
|
|
|
onClickAdd: PropTypes.func.isRequired,
|
2017-11-07 18:16:42 +01:00
|
|
|
onClickDelete: PropTypes.func.isRequired,
|
2017-11-23 12:43:40 +01:00
|
|
|
users: PropTypes.array,
|
2017-11-08 16:06:21 +01:00
|
|
|
values: PropTypes.array,
|
2017-11-07 16:33:15 +01:00
|
|
|
};
|
|
|
|
|
2018-02-06 18:42:42 +01:00
|
|
|
export default InputSearchContainer;
|