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,
|
2023-05-30 03:05:17 +00:00
|
|
|
(state, { value }) => value,
|
|
|
|
(state, { includeMissingValue }) => includeMissingValue,
|
2017-09-04 02:20:56 +00:00
|
|
|
(state, { includeNoChange }) => includeNoChange,
|
2023-05-30 03:05:17 +00:00
|
|
|
(rootFolders, value, includeMissingValue, includeNoChange) => {
|
2019-08-05 02:57:30 +00:00
|
|
|
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',
|
2023-05-30 03:05:17 +00:00
|
|
|
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: '',
|
2017-11-16 02:24:33 +00:00
|
|
|
isDisabled: true,
|
|
|
|
isHidden: true
|
2017-09-04 02:20:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-30 03:05:17 +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() {
|
2017-11-16 02:24:33 +00:00
|
|
|
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;
|
|
|
|
|
2019-08-05 02:57:30 +00:00
|
|
|
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);
|