2017-11-26 03:51:37 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { createSelector } from 'reselect';
|
2019-12-16 21:21:32 +00:00
|
|
|
import { metadataProfileNames } from 'Helpers/Props';
|
2020-09-07 01:33:10 +00:00
|
|
|
import createSortedSectionSelector from 'Store/Selectors/createSortedSectionSelector';
|
|
|
|
import sortByName from 'Utilities/Array/sortByName';
|
2023-07-10 21:42:51 +00:00
|
|
|
import translate from 'Utilities/String/translate';
|
2023-07-22 00:05:32 +00:00
|
|
|
import EnhancedSelectInput from './EnhancedSelectInput';
|
2017-11-26 03:51:37 +00:00
|
|
|
|
|
|
|
function createMapStateToProps() {
|
|
|
|
return createSelector(
|
2020-02-03 21:45:48 +00:00
|
|
|
createSortedSectionSelector('settings.metadataProfiles', sortByName),
|
2017-11-26 03:51:37 +00:00
|
|
|
(state, { includeNoChange }) => includeNoChange,
|
2023-07-22 00:05:32 +00:00
|
|
|
(state, { includeNoChangeDisabled }) => includeNoChangeDisabled,
|
2017-11-26 03:51:37 +00:00
|
|
|
(state, { includeMixed }) => includeMixed,
|
2019-12-16 21:21:32 +00:00
|
|
|
(state, { includeNone }) => includeNone,
|
2023-07-22 00:05:32 +00:00
|
|
|
(metadataProfiles, includeNoChange, includeNoChangeDisabled = true, includeMixed, includeNone) => {
|
2019-12-16 21:21:32 +00:00
|
|
|
const profiles = metadataProfiles.items.filter((item) => item.name !== metadataProfileNames.NONE);
|
|
|
|
const noneProfile = metadataProfiles.items.find((item) => item.name === metadataProfileNames.NONE);
|
|
|
|
|
2020-02-03 21:45:48 +00:00
|
|
|
const values = _.map(profiles, (metadataProfile) => {
|
2017-11-26 03:51:37 +00:00
|
|
|
return {
|
|
|
|
key: metadataProfile.id,
|
|
|
|
value: metadataProfile.name
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
if (includeNone) {
|
|
|
|
values.push({
|
|
|
|
key: noneProfile.id,
|
|
|
|
value: noneProfile.name
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-26 03:51:37 +00:00
|
|
|
if (includeNoChange) {
|
|
|
|
values.unshift({
|
|
|
|
key: 'noChange',
|
2023-07-10 21:42:51 +00:00
|
|
|
value: translate('NoChange'),
|
2024-03-14 04:05:15 +00:00
|
|
|
isDisabled: includeNoChangeDisabled
|
2017-11-26 03:51:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (includeMixed) {
|
|
|
|
values.unshift({
|
|
|
|
key: 'mixed',
|
|
|
|
value: '(Mixed)',
|
2024-03-14 04:05:15 +00:00
|
|
|
isDisabled: true
|
2017-11-26 03:51:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
values
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class MetadataProfileSelectInputConnector extends Component {
|
|
|
|
|
|
|
|
//
|
|
|
|
// Lifecycle
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const {
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
values
|
|
|
|
} = this.props;
|
|
|
|
|
2023-07-22 00:05:32 +00:00
|
|
|
if (!value || !values.some((option) => option.key === value || parseInt(option.key) === value)) {
|
|
|
|
const firstValue = values.find((option) => !isNaN(parseInt(option.key)));
|
2017-11-26 03:51:37 +00:00
|
|
|
|
|
|
|
if (firstValue) {
|
|
|
|
this.onChange({ name, value: firstValue.key });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Listeners
|
|
|
|
|
|
|
|
onChange = ({ name, value }) => {
|
2023-07-22 00:05:32 +00:00
|
|
|
this.props.onChange({ name, value: value === 'noChange' ? value : parseInt(value) });
|
2021-12-24 18:18:14 +00:00
|
|
|
};
|
2017-11-26 03:51:37 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Render
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2023-07-22 00:05:32 +00:00
|
|
|
<EnhancedSelectInput
|
2017-11-26 03:51:37 +00:00
|
|
|
{...this.props}
|
|
|
|
onChange={this.onChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MetadataProfileSelectInputConnector.propTypes = {
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
|
|
|
values: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
|
|
includeNoChange: PropTypes.bool.isRequired,
|
2019-12-16 21:21:32 +00:00
|
|
|
includeNone: PropTypes.bool.isRequired,
|
2017-11-26 03:51:37 +00:00
|
|
|
onChange: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
MetadataProfileSelectInputConnector.defaultProps = {
|
2023-07-22 00:05:32 +00:00
|
|
|
includeNoChange: false,
|
|
|
|
includeNone: true
|
2017-11-26 03:51:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(createMapStateToProps)(MetadataProfileSelectInputConnector);
|