New: Sort preferred words in profile on save

Closes #3241
pull/6/head
Mark McDowall 5 years ago committed by Qstick
parent b7458f6d9c
commit 4399724e97

@ -9,10 +9,6 @@ import ConfirmModal from 'Components/Modal/ConfirmModal';
import EditReleaseProfileModalConnector from './EditReleaseProfileModalConnector'; import EditReleaseProfileModalConnector from './EditReleaseProfileModalConnector';
import styles from './ReleaseProfile.css'; import styles from './ReleaseProfile.css';
function sortPreferred(preferred) {
return preferred.slice().sort((a, b) => b.value - a.value);
}
class ReleaseProfile extends Component { class ReleaseProfile extends Component {
// //
@ -22,20 +18,11 @@ class ReleaseProfile extends Component {
super(props, context); super(props, context);
this.state = { this.state = {
sortedPreferred: sortPreferred(props.preferred),
isEditReleaseProfileModalOpen: false, isEditReleaseProfileModalOpen: false,
isDeleteReleaseProfileModalOpen: false isDeleteReleaseProfileModalOpen: false
}; };
} }
componentDidUpdate(prevProps) {
const { preferred } = this.props;
if (prevProps.preferred !== preferred) {
this.setState({ sortedPreferred: sortPreferred(preferred) });
}
}
// //
// Listeners // Listeners
@ -70,12 +57,12 @@ class ReleaseProfile extends Component {
id, id,
required, required,
ignored, ignored,
preferred,
tags, tags,
tagList tagList
} = this.props; } = this.props;
const { const {
sortedPreferred,
isEditReleaseProfileModalOpen, isEditReleaseProfileModalOpen,
isDeleteReleaseProfileModalOpen isDeleteReleaseProfileModalOpen
} = this.state; } = this.state;
@ -126,7 +113,7 @@ class ReleaseProfile extends Component {
<div> <div>
{ {
sortedPreferred.map((item) => { preferred.map((item) => {
const isPreferred = item.value >= 0; const isPreferred = item.value >= 0;
return ( return (

@ -18,4 +18,12 @@ namespace NzbDrone.Core.Profiles.Releases
Tags = new HashSet<int>(); Tags = new HashSet<int>();
} }
} }
public class ReleaseProfilePreferredComparer : IComparer<KeyValuePair<string, int>>
{
public int Compare(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
{
return y.Value.CompareTo(x.Value);
}
}
} }

@ -18,18 +18,24 @@ namespace NzbDrone.Core.Profiles.Releases
public class ReleaseProfileService : IReleaseProfileService public class ReleaseProfileService : IReleaseProfileService
{ {
private readonly ReleaseProfilePreferredComparer _preferredComparer;
private readonly IRestrictionRepository _repo; private readonly IRestrictionRepository _repo;
private readonly Logger _logger; private readonly Logger _logger;
public ReleaseProfileService(IRestrictionRepository repo, Logger logger) public ReleaseProfileService(IRestrictionRepository repo, Logger logger)
{ {
_preferredComparer = new ReleaseProfilePreferredComparer();
_repo = repo; _repo = repo;
_logger = logger; _logger = logger;
} }
public List<ReleaseProfile> All() public List<ReleaseProfile> All()
{ {
return _repo.All().ToList(); var all = _repo.All().ToList();
all.ForEach(r => r.Preferred.Sort(_preferredComparer));
return all;
} }
public List<ReleaseProfile> AllForTag(int tagId) public List<ReleaseProfile> AllForTag(int tagId)
@ -54,11 +60,15 @@ namespace NzbDrone.Core.Profiles.Releases
public ReleaseProfile Add(ReleaseProfile restriction) public ReleaseProfile Add(ReleaseProfile restriction)
{ {
restriction.Preferred.Sort(_preferredComparer);
return _repo.Insert(restriction); return _repo.Insert(restriction);
} }
public ReleaseProfile Update(ReleaseProfile restriction) public ReleaseProfile Update(ReleaseProfile restriction)
{ {
restriction.Preferred.Sort(_preferredComparer);
return _repo.Update(restriction); return _repo.Update(restriction);
} }
} }

Loading…
Cancel
Save