From 5ac7672756f873cd085eb2fd029e5d9b524655d6 Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 14 Jun 2019 18:24:30 -0500 Subject: [PATCH] broken indexer selector --- .../src/Components/Form/FormInputGroup.js | 4 + .../Form/IndexerSelectInputConnector.js | 98 +++++++++++++++++++ frontend/src/Helpers/Props/inputTypes.js | 2 + .../Release/EditReleaseProfileModalContent.js | 2 +- .../Store/Selectors/createIndexerSelector.js | 15 +++ 5 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 frontend/src/Components/Form/IndexerSelectInputConnector.js create mode 100644 frontend/src/Store/Selectors/createIndexerSelector.js diff --git a/frontend/src/Components/Form/FormInputGroup.js b/frontend/src/Components/Form/FormInputGroup.js index 7abb297d1..78d3e53b6 100644 --- a/frontend/src/Components/Form/FormInputGroup.js +++ b/frontend/src/Components/Form/FormInputGroup.js @@ -14,6 +14,7 @@ import PasswordInput from './PasswordInput'; import PathInputConnector from './PathInputConnector'; import QualityProfileSelectInputConnector from './QualityProfileSelectInputConnector'; import LanguageProfileSelectInputConnector from './LanguageProfileSelectInputConnector'; +import IndexerSelectInputConnector from './IndexerSelectInputConnector'; import RootFolderSelectInputConnector from './RootFolderSelectInputConnector'; import SeriesTypeSelectInput from './SeriesTypeSelectInput'; import SelectInput from './SelectInput'; @@ -61,6 +62,9 @@ function getComponent(type) { case inputTypes.LANGUAGE_PROFILE_SELECT: return LanguageProfileSelectInputConnector; + case inputTypes.INDEXER_SELECT: + return IndexerSelectInputConnector; + case inputTypes.ROOT_FOLDER_SELECT: return RootFolderSelectInputConnector; diff --git a/frontend/src/Components/Form/IndexerSelectInputConnector.js b/frontend/src/Components/Form/IndexerSelectInputConnector.js new file mode 100644 index 000000000..3efca904b --- /dev/null +++ b/frontend/src/Components/Form/IndexerSelectInputConnector.js @@ -0,0 +1,98 @@ +import _ from 'lodash'; +import PropTypes from 'prop-types'; +import React, { Component } from 'react'; +import { connect } from 'react-redux'; +import { createSelector } from 'reselect'; +import sortByName from 'Utilities/Array/sortByName'; +import SelectInput from './SelectInput'; + +function createMapStateToProps() { + return createSelector( + (state) => state.settings.indexers, + (state, { includeNoChange }) => includeNoChange, + (state, { includeMixed }) => includeMixed, + (indexers, includeNoChange, includeMixed) => { + const values = _.map(indexers.items.sort(sortByName), (indexer) => { + return { + key: indexer.id, + value: indexer.name + }; + }); + + if (includeNoChange) { + values.unshift({ + key: 'noChange', + value: 'No Change', + disabled: true + }); + } + + if (includeMixed) { + values.unshift({ + key: 'mixed', + value: '(Mixed)', + disabled: true + }); + } + + return { + values + }; + } + ); +} + +class IndexerSelectInputConnector extends Component { + + // + // Lifecycle + + componentDidMount() { + const { + name, + value, + values + } = this.props; + + if (!value || !_.some(values, (option) => parseInt(option.key) === value)) { + const firstValue = _.find(values, (option) => !isNaN(parseInt(option.key))); + + if (firstValue) { + this.onChange({ name, value: firstValue.key }); + } + } + } + + // + // Listeners + + onChange = ({ name, value }) => { + this.props.onChange({ name, value: parseInt(value) }); + } + + // + // Render + + render() { + return ( + + ); + } +} + +IndexerSelectInputConnector.propTypes = { + name: PropTypes.string.isRequired, + value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), + values: PropTypes.arrayOf(PropTypes.object).isRequired, + includeNoChange: PropTypes.bool.isRequired, + onChange: PropTypes.func.isRequired +}; + +IndexerSelectInputConnector.defaultProps = { + includeNoChange: false +}; + +export default connect(createMapStateToProps)(IndexerSelectInputConnector); diff --git a/frontend/src/Helpers/Props/inputTypes.js b/frontend/src/Helpers/Props/inputTypes.js index 78a8aa1af..dcdd3698a 100644 --- a/frontend/src/Helpers/Props/inputTypes.js +++ b/frontend/src/Helpers/Props/inputTypes.js @@ -10,6 +10,7 @@ export const PASSWORD = 'password'; export const PATH = 'path'; export const QUALITY_PROFILE_SELECT = 'qualityProfileSelect'; export const LANGUAGE_PROFILE_SELECT = 'languageProfileSelect'; +export const INDEXER_SELECT = 'indexerSelect'; export const ROOT_FOLDER_SELECT = 'rootFolderSelect'; export const SELECT = 'select'; export const SERIES_TYPE_SELECT = 'seriesTypeSelect'; @@ -30,6 +31,7 @@ export const all = [ PATH, QUALITY_PROFILE_SELECT, LANGUAGE_PROFILE_SELECT, + INDEXER_SELECT, ROOT_FOLDER_SELECT, SELECT, SERIES_TYPE_SELECT, diff --git a/frontend/src/Settings/Profiles/Release/EditReleaseProfileModalContent.js b/frontend/src/Settings/Profiles/Release/EditReleaseProfileModalContent.js index 40a5ce2af..28ba1e6e9 100644 --- a/frontend/src/Settings/Profiles/Release/EditReleaseProfileModalContent.js +++ b/frontend/src/Settings/Profiles/Release/EditReleaseProfileModalContent.js @@ -123,7 +123,7 @@ function EditReleaseProfileModalContent(props) { Indexer indexerId, + (state) => state.settings.indexers.items, + (indexerId, indexers) => { + return indexers.find((profile) => { + return profile.id === indexerId; + }); + } + ); +} + +export default createIndexerSelector;