import React, { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import LoadingSpinner from '../Common/LoadingSpinner';
import { Permission, useUser } from '../../hooks/useUser';
import { hasPermission } from '../../../server/lib/permissions';
import Button from '../Common/Button';
import { useIntl, defineMessages, FormattedMessage } from 'react-intl';
import axios from 'axios';
import { useToasts } from 'react-toast-notifications';
import Header from '../Common/Header';
const messages = defineMessages({
edituser: 'Edit User',
username: 'Username',
avatar: 'Avatar',
email: 'Email',
permissions: 'Permissions',
admin: 'Admin',
adminDescription:
'Full administrator access. Bypasses all permission checks.',
users: 'Manage Users',
usersDescription:
'Grants permission to manage Overseerr users. Users with this permission cannot modify users with Administrator privilege, or grant it.',
settings: 'Manage Settings',
settingsDescription:
'Grants permission to modify all Overseerr settings. User must have this permission to be able to grant it to others.',
managerequests: 'Manage Requests',
managerequestsDescription:
'Grants permission to manage Overseerr requests. This includes approving and denying requests.',
request: 'Request',
requestDescription:
'Grants permission to make requests for movies or tv shows.',
vote: 'Vote',
voteDescription:
'Grants permission to vote on requests (voting not yet implemented)',
autoapprove: 'Auto Approve',
autoapproveDescription:
'Grants auto approval for any requests made by this user.',
save: 'Save',
saving: 'Saving...',
usersaved: 'User succesfully saved',
userfail: 'Something went wrong saving the user.',
});
interface PermissionOption {
id: string;
name: string;
description: string;
permission: Permission;
}
const UserEdit: React.FC = () => {
const router = useRouter();
const intl = useIntl();
const { addToast } = useToasts();
const [isUpdating, setIsUpdating] = useState(false);
const { user: currentUser, hasPermission: currentHasPermission } = useUser();
const { user, error, revalidate } = useUser({
id: Number(router.query.userId),
});
const [currentPermission, setCurrentPermission] = useState(0);
useEffect(() => {
if (currentPermission !== user?.permissions ?? 0) {
setCurrentPermission(user?.permissions ?? 0);
}
// We know what we are doing here.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [user]);
const updateUser = async () => {
try {
setIsUpdating(true);
await axios.put(`/api/v1/user/${user?.id}`, {
permissions: currentPermission,
email: user?.email,
avatar: user?.avatar,
});
addToast(intl.formatMessage(messages.usersaved), {
appearance: 'success',
autoDismiss: true,
});
} catch (e) {
addToast(intl.formatMessage(messages.userfail), {
appearance: 'error',
autoDismiss: true,
});
throw new Error(`Something went wrong saving the user: ${e.message}`);
} finally {
revalidate();
setIsUpdating(false);
}
};
if (!user && !error) {
return
{permissionOption.description}