Lidarr/frontend/src/Components/Form/RootFolderSelectInputConnec...

135 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-09-04 02:20:56 +00:00
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import RootFolderSelectInput from './RootFolderSelectInput';
const ADD_NEW_KEY = 'addNew';
function createMapStateToProps() {
return createSelector(
2020-02-09 19:15:43 +00:00
(state) => state.settings.rootFolders,
(state, { value }) => value,
(state, { includeMissingValue }) => includeMissingValue,
2017-09-04 02:20:56 +00:00
(state, { includeNoChange }) => includeNoChange,
(rootFolders, value, includeMissingValue, includeNoChange) => {
const values = rootFolders.items.map((rootFolder) => {
2017-09-04 02:20:56 +00:00
return {
key: rootFolder.path,
value: rootFolder.path,
2020-02-09 19:15:43 +00:00
name: rootFolder.name,
2017-09-04 02:20:56 +00:00
freeSpace: rootFolder.freeSpace
};
});
if (includeNoChange) {
values.unshift({
key: 'noChange',
2020-02-09 19:15:43 +00:00
value: '',
name: 'No Change',
isDisabled: true,
isMissing: false
2017-09-04 02:20:56 +00:00
});
}
if (!values.length) {
values.push({
key: '',
value: '',
2020-02-09 19:15:43 +00:00
name: '',
isDisabled: true,
isHidden: true
2017-09-04 02:20:56 +00:00
});
}
if (includeMissingValue && !values.find((v) => v.key === value)) {
values.push({
key: value,
value,
isMissing: true,
isDisabled: true
});
}
2017-09-04 02:20:56 +00:00
values.push({
key: ADD_NEW_KEY,
2020-02-09 19:15:43 +00:00
value: '',
name: 'Add a new path'
2017-09-04 02:20:56 +00:00
});
return {
values,
isSaving: rootFolders.isSaving,
saveError: rootFolders.saveError
};
}
);
}
class RootFolderSelectInputConnector extends Component {
//
// Lifecycle
2020-06-27 21:51:59 +00:00
UNSAFE_componentWillMount() {
const {
value,
values,
onChange
} = this.props;
if (value == null && values[0].key === '') {
onChange({ name, value: '' });
}
}
2017-09-04 02:20:56 +00:00
componentDidMount() {
const {
name,
value,
values,
onChange
} = this.props;
if (!value || !values.some((v) => v.key === value) || value === ADD_NEW_KEY) {
2017-09-04 02:20:56 +00:00
const defaultValue = values[0];
if (defaultValue.key === ADD_NEW_KEY) {
onChange({ name, value: '' });
} else {
onChange({ name, value: defaultValue.key });
}
}
}
//
// Render
render() {
const {
...otherProps
} = this.props;
return (
<RootFolderSelectInput
{...otherProps}
onNewRootFolderSelect={this.onNewRootFolderSelect}
/>
);
}
}
RootFolderSelectInputConnector.propTypes = {
name: PropTypes.string.isRequired,
value: PropTypes.string,
values: PropTypes.arrayOf(PropTypes.object).isRequired,
includeNoChange: PropTypes.bool.isRequired,
2020-02-09 19:15:43 +00:00
onChange: PropTypes.func.isRequired
2017-09-04 02:20:56 +00:00
};
RootFolderSelectInputConnector.defaultProps = {
includeNoChange: false
};
2020-02-09 19:15:43 +00:00
export default connect(createMapStateToProps)(RootFolderSelectInputConnector);