mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Cleanup types and functions
This commit is contained in:
parent
d52c56eb2e
commit
2e855382fe
1 changed files with 50 additions and 54 deletions
|
@ -25,13 +25,44 @@ type UnratedItem = {
|
||||||
checkedAttribute: string
|
checkedAttribute: string
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function handleSaveUser(
|
||||||
|
page: HTMLDivElement,
|
||||||
|
getSchedulesFromPage: () => AccessSchedule[],
|
||||||
|
getAllowedTagsFromPage: () => string[],
|
||||||
|
getBlockedTagsFromPage: () => string[],
|
||||||
|
onSaveComplete: () => void
|
||||||
|
) {
|
||||||
|
return (user: UserDto) => {
|
||||||
|
const userId = user.Id;
|
||||||
|
const userPolicy = user.Policy;
|
||||||
|
if (!userId || !userPolicy) {
|
||||||
|
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;
|
||||||
|
userPolicy.BlockUnratedItems = Array.prototype.filter
|
||||||
|
.call(page.querySelectorAll('.chkUnratedItem'), i => i.checked)
|
||||||
|
.map(i => i.getAttribute('data-itemtype'));
|
||||||
|
userPolicy.AccessSchedules = getSchedulesFromPage();
|
||||||
|
userPolicy.AllowedTags = getAllowedTagsFromPage();
|
||||||
|
userPolicy.BlockedTags = getBlockedTagsFromPage();
|
||||||
|
ServerConnections.getCurrentApiClientAsync()
|
||||||
|
.then(apiClient => apiClient.updateUserPolicy(userId, userPolicy))
|
||||||
|
.then(() => onSaveComplete())
|
||||||
|
.catch(err => {
|
||||||
|
console.error('[userparentalcontrol] failed to update user policy', err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const UserParentalControl: FunctionComponent = () => {
|
const UserParentalControl: FunctionComponent = () => {
|
||||||
const [ userName, setUserName ] = useState('');
|
const [ userName, setUserName ] = useState('');
|
||||||
const [ parentalRatings, setParentalRatings ] = useState<ParentalRating[]>([]);
|
const [ parentalRatings, setParentalRatings ] = useState<ParentalRating[]>([]);
|
||||||
const [ unratedItems, setUnratedItems ] = useState<UnratedItem[]>([]);
|
const [ unratedItems, setUnratedItems ] = useState<UnratedItem[]>([]);
|
||||||
const [ accessSchedules, setAccessSchedules ] = useState<AccessSchedule[]>([]);
|
const [ accessSchedules, setAccessSchedules ] = useState<AccessSchedule[]>([]);
|
||||||
const [ allowedTags, setAllowedTags ] = useState([]);
|
const [ allowedTags, setAllowedTags ] = useState<string[]>([]);
|
||||||
const [ blockedTags, setBlockedTags ] = useState([]);
|
const [ blockedTags, setBlockedTags ] = useState<string[]>([]);
|
||||||
|
|
||||||
const element = useRef<HTMLDivElement>(null);
|
const element = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
@ -109,7 +140,7 @@ const UserParentalControl: FunctionComponent = () => {
|
||||||
blockUnratedItems.dispatchEvent(new CustomEvent('create'));
|
blockUnratedItems.dispatchEvent(new CustomEvent('create'));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const loadAllowedTags = useCallback((tags) => {
|
const loadAllowedTags = useCallback((tags: string[]) => {
|
||||||
const page = element.current;
|
const page = element.current;
|
||||||
|
|
||||||
if (!page) {
|
if (!page) {
|
||||||
|
@ -124,15 +155,13 @@ const UserParentalControl: FunctionComponent = () => {
|
||||||
for (const btnDeleteTag of allowedTagsElem.querySelectorAll('.btnDeleteTag')) {
|
for (const btnDeleteTag of allowedTagsElem.querySelectorAll('.btnDeleteTag')) {
|
||||||
btnDeleteTag.addEventListener('click', function () {
|
btnDeleteTag.addEventListener('click', function () {
|
||||||
const tag = btnDeleteTag.getAttribute('data-tag');
|
const tag = btnDeleteTag.getAttribute('data-tag');
|
||||||
const newTags = tags.filter(function (t: string) {
|
const newTags = tags.filter(t => t !== tag);
|
||||||
return t != tag;
|
|
||||||
});
|
|
||||||
loadAllowedTags(newTags);
|
loadAllowedTags(newTags);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const loadBlockedTags = useCallback((tags) => {
|
const loadBlockedTags = useCallback((tags: string[]) => {
|
||||||
const page = element.current;
|
const page = element.current;
|
||||||
|
|
||||||
if (!page) {
|
if (!page) {
|
||||||
|
@ -147,9 +176,7 @@ const UserParentalControl: FunctionComponent = () => {
|
||||||
for (const btnDeleteTag of blockedTagsElem.querySelectorAll('.btnDeleteTag')) {
|
for (const btnDeleteTag of blockedTagsElem.querySelectorAll('.btnDeleteTag')) {
|
||||||
btnDeleteTag.addEventListener('click', function () {
|
btnDeleteTag.addEventListener('click', function () {
|
||||||
const tag = btnDeleteTag.getAttribute('data-tag');
|
const tag = btnDeleteTag.getAttribute('data-tag');
|
||||||
const newTags = tags.filter(function (t: string) {
|
const newTags = tags.filter(t => t !== tag);
|
||||||
return t != tag;
|
|
||||||
});
|
|
||||||
loadBlockedTags(newTags);
|
loadBlockedTags(newTags);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -171,15 +198,13 @@ const UserParentalControl: FunctionComponent = () => {
|
||||||
btnDelete.addEventListener('click', function () {
|
btnDelete.addEventListener('click', function () {
|
||||||
const index = parseInt(btnDelete.getAttribute('data-index') ?? '0', 10);
|
const index = parseInt(btnDelete.getAttribute('data-index') ?? '0', 10);
|
||||||
schedules.splice(index, 1);
|
schedules.splice(index, 1);
|
||||||
const newindex = schedules.filter(function (i: number) {
|
const newindex = schedules.filter((i: number) => i != index);
|
||||||
return i != index;
|
|
||||||
});
|
|
||||||
renderAccessSchedule(newindex);
|
renderAccessSchedule(newindex);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const loadUser = useCallback((user, allParentalRatings) => {
|
const loadUser = useCallback((user: UserDto, allParentalRatings: ParentalRating[]) => {
|
||||||
const page = element.current;
|
const page = element.current;
|
||||||
|
|
||||||
if (!page) {
|
if (!page) {
|
||||||
|
@ -187,33 +212,31 @@ const UserParentalControl: FunctionComponent = () => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setUserName(user.Name);
|
setUserName(user.Name || '');
|
||||||
LibraryMenu.setTitle(user.Name);
|
LibraryMenu.setTitle(user.Name);
|
||||||
loadUnratedItems(user);
|
loadUnratedItems(user);
|
||||||
|
|
||||||
loadAllowedTags(user.Policy.AllowedTags);
|
loadAllowedTags(user.Policy?.AllowedTags || []);
|
||||||
loadBlockedTags(user.Policy.BlockedTags);
|
loadBlockedTags(user.Policy?.BlockedTags || []);
|
||||||
populateRatings(allParentalRatings);
|
populateRatings(allParentalRatings);
|
||||||
|
|
||||||
let ratingValue = '';
|
let ratingValue = '';
|
||||||
|
if (user.Policy?.MaxParentalRating) {
|
||||||
if (user.Policy.MaxParentalRating != null) {
|
allParentalRatings.forEach(rating => {
|
||||||
for (let i = 0, length = allParentalRatings.length; i < length; i++) {
|
if (rating.Value && user.Policy?.MaxParentalRating && user.Policy.MaxParentalRating >= rating.Value) {
|
||||||
const rating = allParentalRatings[i];
|
ratingValue = `${rating.Value}`;
|
||||||
|
|
||||||
if (user.Policy.MaxParentalRating >= rating.Value) {
|
|
||||||
ratingValue = rating.Value;
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
(page.querySelector('#selectMaxParentalRating') as HTMLSelectElement).value = ratingValue;
|
(page.querySelector('#selectMaxParentalRating') as HTMLSelectElement).value = ratingValue;
|
||||||
|
|
||||||
if (user.Policy.IsAdministrator) {
|
if (user.Policy?.IsAdministrator) {
|
||||||
(page.querySelector('.accessScheduleSection') as HTMLDivElement).classList.add('hide');
|
(page.querySelector('.accessScheduleSection') as HTMLDivElement).classList.add('hide');
|
||||||
} else {
|
} else {
|
||||||
(page.querySelector('.accessScheduleSection') as HTMLDivElement).classList.remove('hide');
|
(page.querySelector('.accessScheduleSection') as HTMLDivElement).classList.remove('hide');
|
||||||
}
|
}
|
||||||
renderAccessSchedule(user.Policy.AccessSchedules || []);
|
renderAccessSchedule(user.Policy?.AccessSchedules || []);
|
||||||
loading.hide();
|
loading.hide();
|
||||||
}, [loadAllowedTags, loadBlockedTags, loadUnratedItems, populateRatings, renderAccessSchedule]);
|
}, [loadAllowedTags, loadBlockedTags, loadUnratedItems, populateRatings, renderAccessSchedule]);
|
||||||
|
|
||||||
|
@ -486,31 +509,4 @@ const UserParentalControl: FunctionComponent = () => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
function handleSaveUser(page: HTMLDivElement, getSchedulesFromPage: () => AccessSchedule[], getAllowedTagsFromPage: () => string[], getBlockedTagsFromPage: () => string[], onSaveComplete: () => void) {
|
|
||||||
return (user: UserDto) => {
|
|
||||||
const userId = user.Id;
|
|
||||||
const userPolicy = user.Policy;
|
|
||||||
if (!userId || !userPolicy) {
|
|
||||||
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;
|
|
||||||
userPolicy.BlockUnratedItems = Array.prototype.filter.call(page.querySelectorAll('.chkUnratedItem'), function (i) {
|
|
||||||
return i.checked;
|
|
||||||
}).map(function (i) {
|
|
||||||
return i.getAttribute('data-itemtype');
|
|
||||||
});
|
|
||||||
userPolicy.AccessSchedules = getSchedulesFromPage();
|
|
||||||
userPolicy.AllowedTags = getAllowedTagsFromPage();
|
|
||||||
userPolicy.BlockedTags = getBlockedTagsFromPage();
|
|
||||||
ServerConnections.getCurrentApiClientAsync()
|
|
||||||
.then(apiClient => apiClient.updateUserPolicy(userId, userPolicy))
|
|
||||||
.then(() => onSaveComplete())
|
|
||||||
.catch(err => {
|
|
||||||
console.error('[userparentalcontrol] failed to update user policy', err);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default UserParentalControl;
|
export default UserParentalControl;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue