Fixed: Prevent frontend errors when many books added to Readarr

pull/1335/head^2
ta264 3 years ago
parent 05d24821f7
commit bf852cadbe

@ -6,11 +6,13 @@ import FormInputGroup from 'Components/Form/FormInputGroup';
import FormLabel from 'Components/Form/FormLabel'; import FormLabel from 'Components/Form/FormLabel';
import Button from 'Components/Link/Button'; import Button from 'Components/Link/Button';
import SpinnerButton from 'Components/Link/SpinnerButton'; import SpinnerButton from 'Components/Link/SpinnerButton';
import LoadingIndicator from 'Components/Loading/LoadingIndicator';
import ModalBody from 'Components/Modal/ModalBody'; import ModalBody from 'Components/Modal/ModalBody';
import ModalContent from 'Components/Modal/ModalContent'; import ModalContent from 'Components/Modal/ModalContent';
import ModalFooter from 'Components/Modal/ModalFooter'; import ModalFooter from 'Components/Modal/ModalFooter';
import ModalHeader from 'Components/Modal/ModalHeader'; import ModalHeader from 'Components/Modal/ModalHeader';
import { inputTypes } from 'Helpers/Props'; import { inputTypes } from 'Helpers/Props';
import getErrorMessage from 'Utilities/Object/getErrorMessage';
import translate from 'Utilities/String/translate'; import translate from 'Utilities/String/translate';
class EditBookModalContent extends Component { class EditBookModalContent extends Component {
@ -36,6 +38,9 @@ class EditBookModalContent extends Component {
authorName, authorName,
statistics, statistics,
item, item,
isFetching,
isPopulated,
error,
isSaving, isSaving,
onInputChange, onInputChange,
onModalClose, onModalClose,
@ -49,6 +54,7 @@ class EditBookModalContent extends Component {
} = item; } = item;
const hasFile = statistics ? statistics.bookFileCount : 0; const hasFile = statistics ? statistics.bookFileCount : 0;
const errorMessage = getErrorMessage(error, 'Unable to load editions');
return ( return (
<ModalContent onModalClose={onModalClose}> <ModalContent onModalClose={onModalClose}>
@ -88,6 +94,18 @@ class EditBookModalContent extends Component {
/> />
</FormGroup> </FormGroup>
{
isFetching &&
<LoadingIndicator />
}
{
error &&
<div>{errorMessage}</div>
}
{
isPopulated && !isFetching && !!editions.value.length &&
<FormGroup> <FormGroup>
<FormLabel> <FormLabel>
{translate('Edition')} {translate('Edition')}
@ -102,6 +120,7 @@ class EditBookModalContent extends Component {
onChange={onInputChange} onChange={onInputChange}
/> />
</FormGroup> </FormGroup>
}
</Form> </Form>
</ModalBody> </ModalBody>
@ -131,6 +150,9 @@ EditBookModalContent.propTypes = {
authorName: PropTypes.string.isRequired, authorName: PropTypes.string.isRequired,
statistics: PropTypes.object.isRequired, statistics: PropTypes.object.isRequired,
item: PropTypes.object.isRequired, item: PropTypes.object.isRequired,
isFetching: PropTypes.bool.isRequired,
error: PropTypes.object,
isPopulated: PropTypes.bool.isRequired,
isSaving: PropTypes.bool.isRequired, isSaving: PropTypes.bool.isRequired,
onInputChange: PropTypes.func.isRequired, onInputChange: PropTypes.func.isRequired,
onSavePress: PropTypes.func.isRequired, onSavePress: PropTypes.func.isRequired,

@ -4,6 +4,7 @@ import React, { Component } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { createSelector } from 'reselect'; import { createSelector } from 'reselect';
import { saveBook, setBookValue } from 'Store/Actions/bookActions'; import { saveBook, setBookValue } from 'Store/Actions/bookActions';
import { clearEditions, fetchEditions } from 'Store/Actions/editionActions';
import createAuthorSelector from 'Store/Selectors/createAuthorSelector'; import createAuthorSelector from 'Store/Selectors/createAuthorSelector';
import createBookSelector from 'Store/Selectors/createBookSelector'; import createBookSelector from 'Store/Selectors/createBookSelector';
import selectSettings from 'Store/Selectors/selectSettings'; import selectSettings from 'Store/Selectors/selectSettings';
@ -12,15 +13,25 @@ import EditBookModalContent from './EditBookModalContent';
function createMapStateToProps() { function createMapStateToProps() {
return createSelector( return createSelector(
(state) => state.books, (state) => state.books,
(state) => state.editions,
createBookSelector(), createBookSelector(),
createAuthorSelector(), createAuthorSelector(),
(bookState, book, author) => { (bookState, editionState, book, author) => {
const { const {
isSaving, isSaving,
saveError, saveError,
pendingChanges pendingChanges
} = bookState; } = bookState;
const {
isFetching,
isPopulated,
error,
items
} = editionState;
book.editions = items;
const bookSettings = _.pick(book, [ const bookSettings = _.pick(book, [
'monitored', 'monitored',
'anyEditionOk', 'anyEditionOk',
@ -34,6 +45,9 @@ function createMapStateToProps() {
authorName: author.authorName, authorName: author.authorName,
bookType: book.bookType, bookType: book.bookType,
statistics: book.statistics, statistics: book.statistics,
isFetching,
isPopulated,
error,
isSaving, isSaving,
saveError, saveError,
item: settings.settings, item: settings.settings,
@ -44,6 +58,8 @@ function createMapStateToProps() {
} }
const mapDispatchToProps = { const mapDispatchToProps = {
dispatchFetchEditions: fetchEditions,
dispatchClearEditions: clearEditions,
dispatchSetBookValue: setBookValue, dispatchSetBookValue: setBookValue,
dispatchSaveBook: saveBook dispatchSaveBook: saveBook
}; };
@ -53,12 +69,20 @@ class EditBookModalContentConnector extends Component {
// //
// Lifecycle // Lifecycle
componentDidMount() {
this.props.dispatchFetchEditions({ bookId: this.props.bookId });
}
componentDidUpdate(prevProps, prevState) { componentDidUpdate(prevProps, prevState) {
if (prevProps.isSaving && !this.props.isSaving && !this.props.saveError) { if (prevProps.isSaving && !this.props.isSaving && !this.props.saveError) {
this.props.onModalClose(); this.props.onModalClose();
} }
} }
componentWillUnmount() {
this.props.dispatchClearEditions();
}
// //
// Listeners // Listeners
@ -90,6 +114,8 @@ EditBookModalContentConnector.propTypes = {
bookId: PropTypes.number, bookId: PropTypes.number,
isSaving: PropTypes.bool.isRequired, isSaving: PropTypes.bool.isRequired,
saveError: PropTypes.object, saveError: PropTypes.object,
dispatchFetchEditions: PropTypes.func.isRequired,
dispatchClearEditions: PropTypes.func.isRequired,
dispatchSetBookValue: PropTypes.func.isRequired, dispatchSetBookValue: PropTypes.func.isRequired,
dispatchSaveBook: PropTypes.func.isRequired, dispatchSaveBook: PropTypes.func.isRequired,
onModalClose: PropTypes.func.isRequired onModalClose: PropTypes.func.isRequired

@ -0,0 +1,54 @@
import { createAction } from 'redux-actions';
import { createThunk, handleThunks } from 'Store/thunks';
import createFetchHandler from './Creators/createFetchHandler';
import createHandleActions from './Creators/createHandleActions';
import createClearReducer from './Creators/Reducers/createClearReducer';
//
// Variables
export const section = 'editions';
//
// State
export const defaultState = {
isFetching: false,
isPopulated: false,
error: null,
items: [],
itemMap: {}
};
//
// Actions Types
export const FETCH_EDITIONS = 'editions/fetchEditions';
export const CLEAR_EDITIONS = 'editions/clearEditions';
//
// Action Creators
export const fetchEditions = createThunk(FETCH_EDITIONS);
export const clearEditions = createAction(CLEAR_EDITIONS);
//
// Action Handlers
export const actionHandlers = handleThunks({
[FETCH_EDITIONS]: createFetchHandler(section, '/edition')
});
//
// Reducers
export const reducers = createHandleActions({
[CLEAR_EDITIONS]: createClearReducer(section, {
isFetching: false,
isPopulated: false,
error: null,
items: [],
itemMap: {}
})
}, defaultState, section);

@ -14,6 +14,7 @@ import * as calendar from './calendarActions';
import * as captcha from './captchaActions'; import * as captcha from './captchaActions';
import * as commands from './commandActions'; import * as commands from './commandActions';
import * as customFilters from './customFilterActions'; import * as customFilters from './customFilterActions';
import * as editions from './editionActions';
import * as history from './historyActions'; import * as history from './historyActions';
import * as interactiveImportActions from './interactiveImportActions'; import * as interactiveImportActions from './interactiveImportActions';
import * as oAuth from './oAuthActions'; import * as oAuth from './oAuthActions';
@ -47,6 +48,7 @@ export default [
captcha, captcha,
commands, commands,
customFilters, customFilters,
editions,
history, history,
interactiveImportActions, interactiveImportActions,
oAuth, oAuth,

@ -9,6 +9,7 @@ namespace NzbDrone.Core.Books
{ {
public interface IEditionRepository : IBasicRepository<Edition> public interface IEditionRepository : IBasicRepository<Edition>
{ {
List<Edition> GetAllMonitoredEditions();
Edition FindByForeignEditionId(string foreignEditionId); Edition FindByForeignEditionId(string foreignEditionId);
List<Edition> FindByBook(int id); List<Edition> FindByBook(int id);
List<Edition> FindByAuthor(int id); List<Edition> FindByAuthor(int id);
@ -25,6 +26,11 @@ namespace NzbDrone.Core.Books
{ {
} }
public List<Edition> GetAllMonitoredEditions()
{
return Query(x => x.Monitored == true);
}
public Edition FindByForeignEditionId(string foreignEditionId) public Edition FindByForeignEditionId(string foreignEditionId)
{ {
var edition = Query(x => x.ForeignEditionId == foreignEditionId).SingleOrDefault(); var edition = Query(x => x.ForeignEditionId == foreignEditionId).SingleOrDefault();

@ -12,7 +12,7 @@ namespace NzbDrone.Core.Books
{ {
Edition GetEdition(int id); Edition GetEdition(int id);
Edition GetEditionByForeignEditionId(string foreignEditionId); Edition GetEditionByForeignEditionId(string foreignEditionId);
List<Edition> GetAllEditions(); List<Edition> GetAllMonitoredEditions();
void InsertMany(List<Edition> editions); void InsertMany(List<Edition> editions);
void UpdateMany(List<Edition> editions); void UpdateMany(List<Edition> editions);
void DeleteMany(List<Edition> editions); void DeleteMany(List<Edition> editions);
@ -48,9 +48,9 @@ namespace NzbDrone.Core.Books
return _editionRepository.FindByForeignEditionId(foreignEditionId); return _editionRepository.FindByForeignEditionId(foreignEditionId);
} }
public List<Edition> GetAllEditions() public List<Edition> GetAllMonitoredEditions()
{ {
return _editionRepository.All().ToList(); return _editionRepository.GetAllMonitoredEditions();
} }
public void InsertMany(List<Edition> editions) public void InsertMany(List<Edition> editions)

@ -73,7 +73,7 @@ namespace Readarr.Api.V1.Books
var books = _bookService.GetAllBooks(); var books = _bookService.GetAllBooks();
var authors = _authorService.GetAllAuthors().ToDictionary(x => x.AuthorMetadataId); var authors = _authorService.GetAllAuthors().ToDictionary(x => x.AuthorMetadataId);
var editions = _editionService.GetAllEditions().GroupBy(x => x.BookId).ToDictionary(x => x.Key, y => y.ToList()); var editions = _editionService.GetAllMonitoredEditions().GroupBy(x => x.BookId).ToDictionary(x => x.Key, y => y.ToList());
foreach (var book in books) foreach (var book in books)
{ {

@ -0,0 +1,27 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Core.Books;
using Readarr.Api.V1.Books;
using Readarr.Http;
namespace NzbDrone.Api.V1.Editions
{
[V1ApiController]
public class EditionController : Controller
{
private readonly IEditionService _editionService;
public EditionController(IEditionService editionService)
{
_editionService = editionService;
}
[HttpGet]
public List<EditionResource> GetEditions(int bookId)
{
var editions = _editionService.GetEditionsByBook(bookId);
return editions.ToResource();
}
}
}
Loading…
Cancel
Save