Files
channels-seerr/src/components/UserList/BulkEditModal.tsx
Gauthier b36bb3fa58 refactor: switch from Axios for Fetch API (#840)
* refactor: switch ExternalAPI to Fetch API

* fix: add missing auth token in Plex request

* fix: send proper URL params

* ci: try to fix format checker

* ci: ci: try to fix format checker

* ci: try to fix format checker

* refactor: make tautulli use the ExternalAPI class

* refactor: add rate limit to fetch api

* refactor: add rate limit to fetch api

* refactor: switch server from axios to fetch api

* refactor: switch frontend from axios to fetch api

* fix: switch from URL objects to strings

* fix: use the right search params for ExternalAPI

* fix: better log for ExternalAPI errors

* feat: add retry to external API requests

* fix: try to fix network errors with IPv6

* fix: imageProxy rate limit

* revert: remove retry to external API requests

* feat: set IPv4 first as an option

* fix(jellyfinapi): add missing argument in JellyfinAPI constructor

* refactor: clean the rate limit utility
2024-07-14 19:04:36 +02:00

115 lines
3.1 KiB
TypeScript

import Modal from '@app/components/Common/Modal';
import PermissionEdit from '@app/components/PermissionEdit';
import type { User } from '@app/hooks/useUser';
import { useUser } from '@app/hooks/useUser';
import globalMessages from '@app/i18n/globalMessages';
import defineMessages from '@app/utils/defineMessages';
import { useEffect, useState } from 'react';
import { useIntl } from 'react-intl';
import { useToasts } from 'react-toast-notifications';
interface BulkEditProps {
selectedUserIds: number[];
users?: User[];
onCancel?: () => void;
onComplete?: (updatedUsers: User[]) => void;
onSaving?: (isSaving: boolean) => void;
}
const messages = defineMessages('components.UserList', {
userssaved: 'User permissions saved successfully!',
userfail: 'Something went wrong while saving user permissions.',
edituser: 'Edit User Permissions',
});
const BulkEditModal = ({
selectedUserIds,
users,
onCancel,
onComplete,
onSaving,
}: BulkEditProps) => {
const { user: currentUser } = useUser();
const intl = useIntl();
const { addToast } = useToasts();
const [currentPermission, setCurrentPermission] = useState(0);
const [isSaving, setIsSaving] = useState(false);
useEffect(() => {
if (onSaving) {
onSaving(isSaving);
}
}, [isSaving, onSaving]);
const updateUsers = async () => {
try {
setIsSaving(true);
const res = await fetch('/api/v1/user', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
ids: selectedUserIds,
permissions: currentPermission,
}),
});
if (!res.ok) throw new Error();
const updated: User[] = await res.json();
if (onComplete) {
onComplete(updated);
}
addToast(intl.formatMessage(messages.userssaved), {
appearance: 'success',
autoDismiss: true,
});
} catch (e) {
addToast(intl.formatMessage(messages.userfail), {
appearance: 'error',
autoDismiss: true,
});
} finally {
setIsSaving(false);
}
};
useEffect(() => {
if (users) {
const selectedUsers = users.filter((u) => selectedUserIds.includes(u.id));
const { permissions: allPermissionsEqual } = selectedUsers.reduce(
({ permissions: aPerms }, { permissions: bPerms }) => {
return {
permissions: aPerms === bPerms ? aPerms : NaN,
};
},
{ permissions: selectedUsers[0].permissions }
);
if (allPermissionsEqual) {
setCurrentPermission(allPermissionsEqual);
}
}
}, [users, selectedUserIds]);
return (
<Modal
title={intl.formatMessage(messages.edituser)}
onOk={() => {
updateUsers();
}}
okDisabled={isSaving}
okText={intl.formatMessage(globalMessages.save)}
onCancel={onCancel}
>
<div className="mb-6">
<PermissionEdit
actingUser={currentUser}
currentPermission={currentPermission}
onUpdate={(newPermission) => setCurrentPermission(newPermission)}
/>
</div>
</Modal>
);
};
export default BulkEditModal;