Merge pull request #7145 from Shadowghost/set-parental-subrating
This commit is contained in:
@@ -30,6 +30,7 @@ type UnratedNamedItem = NamedItem & {
|
||||
|
||||
function handleSaveUser(
|
||||
page: HTMLDivElement,
|
||||
parentalRatingsRef: React.MutableRefObject<ParentalRating[]>,
|
||||
getSchedulesFromPage: () => AccessSchedule[],
|
||||
getAllowedTagsFromPage: () => string[],
|
||||
getBlockedTagsFromPage: () => string[],
|
||||
@@ -42,8 +43,12 @@ function handleSaveUser(
|
||||
throw new Error('Unexpected null user id or policy');
|
||||
}
|
||||
|
||||
const parentalRating = parseInt((page.querySelector('#selectMaxParentalRating') as HTMLSelectElement).value, 10);
|
||||
userPolicy.MaxParentalRating = Number.isNaN(parentalRating) ? null : parentalRating;
|
||||
const parentalRatingIndex = parseInt((page.querySelector('#selectMaxParentalRating') as HTMLSelectElement).value, 10);
|
||||
const parentalRating = parentalRatingsRef.current[parentalRatingIndex] as ParentalRating;
|
||||
const score = parentalRating?.RatingScore?.score;
|
||||
const subScore = parentalRating?.RatingScore?.subScore;
|
||||
userPolicy.MaxParentalRating = Number.isNaN(score) ? null : score;
|
||||
userPolicy.MaxParentalSubRating = Number.isNaN(subScore) ? null : subScore;
|
||||
userPolicy.BlockUnratedItems = Array.prototype.filter
|
||||
.call(page.querySelectorAll('.chkUnratedItem'), i => i.checked)
|
||||
.map(i => i.getAttribute('data-itemtype'));
|
||||
@@ -72,31 +77,7 @@ const UserParentalControl = () => {
|
||||
const libraryMenu = useMemo(async () => ((await import('../../../../scripts/libraryMenu')).default), []);
|
||||
|
||||
const element = useRef<HTMLDivElement>(null);
|
||||
|
||||
const populateRatings = useCallback((allParentalRatings: ParentalRating[]) => {
|
||||
let rating;
|
||||
const ratings: ParentalRating[] = [];
|
||||
|
||||
for (let i = 0, length = allParentalRatings.length; i < length; i++) {
|
||||
rating = allParentalRatings[i];
|
||||
|
||||
if (ratings.length) {
|
||||
const lastRating = ratings[ratings.length - 1];
|
||||
|
||||
if (lastRating.Value === rating.Value) {
|
||||
lastRating.Name += '/' + rating.Name;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
ratings.push({
|
||||
Name: rating.Name,
|
||||
Value: rating.Value
|
||||
});
|
||||
}
|
||||
|
||||
setParentalRatings(ratings);
|
||||
}, []);
|
||||
const parentalRatingsRef = useRef<ParentalRating[]>([]);
|
||||
|
||||
const loadUnratedItems = useCallback((user: UserDto) => {
|
||||
const page = element.current;
|
||||
@@ -161,16 +142,52 @@ const UserParentalControl = () => {
|
||||
|
||||
setAllowedTags(user.Policy?.AllowedTags || []);
|
||||
setBlockedTags(user.Policy?.BlockedTags || []);
|
||||
populateRatings(allParentalRatings);
|
||||
|
||||
let ratingValue = '';
|
||||
allParentalRatings.forEach(rating => {
|
||||
if (rating.Value != null && user.Policy?.MaxParentalRating != null && user.Policy.MaxParentalRating >= rating.Value) {
|
||||
ratingValue = `${rating.Value}`;
|
||||
// Build the grouped ratings array
|
||||
const ratings: ParentalRating[] = [];
|
||||
for (let i = 0, length = allParentalRatings.length; i < length; i++) {
|
||||
const rating = allParentalRatings[i];
|
||||
|
||||
if (ratings.length) {
|
||||
const lastRating = ratings[ratings.length - 1];
|
||||
|
||||
if (lastRating.RatingScore?.score === rating.RatingScore?.score && lastRating.RatingScore?.subScore == rating.RatingScore?.subScore) {
|
||||
lastRating.Name += '/' + rating.Name;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
setMaxParentalRating(ratingValue);
|
||||
ratings.push(rating);
|
||||
}
|
||||
setParentalRatings(ratings);
|
||||
parentalRatingsRef.current = ratings;
|
||||
|
||||
// Find matching rating - first try exact match with score and subscore
|
||||
let ratingIndex = '';
|
||||
const userMaxRating = user.Policy?.MaxParentalRating;
|
||||
const userMaxSubRating = user.Policy?.MaxParentalSubRating;
|
||||
|
||||
if (userMaxRating != null) {
|
||||
// First try to find exact match with both score and subscore
|
||||
ratings.forEach((rating, index) => {
|
||||
if (rating.RatingScore?.score === userMaxRating
|
||||
&& rating.RatingScore?.subScore === userMaxSubRating) {
|
||||
ratingIndex = `${index}`;
|
||||
}
|
||||
});
|
||||
|
||||
// If no exact match found, fallback to score-only match
|
||||
if (!ratingIndex) {
|
||||
ratings.forEach((rating, index) => {
|
||||
if (rating.RatingScore?.score != null
|
||||
&& rating.RatingScore.score <= userMaxRating) {
|
||||
ratingIndex = `${index}`;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
setMaxParentalRating(ratingIndex);
|
||||
|
||||
if (user.Policy?.IsAdministrator) {
|
||||
(page.querySelector('.accessScheduleSection') as HTMLDivElement).classList.add('hide');
|
||||
@@ -179,7 +196,7 @@ const UserParentalControl = () => {
|
||||
}
|
||||
setAccessSchedules(user.Policy?.AccessSchedules || []);
|
||||
loading.hide();
|
||||
}, [libraryMenu, setAllowedTags, setBlockedTags, loadUnratedItems, populateRatings]);
|
||||
}, [libraryMenu, setAllowedTags, setBlockedTags, loadUnratedItems]);
|
||||
|
||||
const loadData = useCallback(() => {
|
||||
if (!userId) {
|
||||
@@ -286,7 +303,7 @@ const UserParentalControl = () => {
|
||||
toast(globalize.translate('SettingsSaved'));
|
||||
};
|
||||
|
||||
const saveUser = handleSaveUser(page, getSchedulesFromPage, getAllowedTagsFromPage, getBlockedTagsFromPage, onSaveComplete);
|
||||
const saveUser = handleSaveUser(page, parentalRatingsRef, getSchedulesFromPage, getAllowedTagsFromPage, getBlockedTagsFromPage, onSaveComplete);
|
||||
|
||||
const onSubmit = (e: Event) => {
|
||||
if (!userId) {
|
||||
@@ -342,11 +359,11 @@ const UserParentalControl = () => {
|
||||
const optionMaxParentalRating = () => {
|
||||
let content = '';
|
||||
content += '<option value=\'\'></option>';
|
||||
for (const rating of parentalRatings) {
|
||||
if (rating.Value != null) {
|
||||
content += `<option value='${rating.Value}'>${escapeHTML(rating.Name)}</option>`;
|
||||
parentalRatings.forEach((rating, index) => {
|
||||
if (rating.RatingScore != null) {
|
||||
content += `<option value='${index}'>${escapeHTML(rating.Name)}</option>`;
|
||||
}
|
||||
}
|
||||
});
|
||||
return content;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user