import Alert from '@app/components/Common/Alert'; import Button from '@app/components/Common/Button'; import LoadingSpinner from '@app/components/Common/LoadingSpinner'; import PageTitle from '@app/components/Common/PageTitle'; import PermissionEdit from '@app/components/PermissionEdit'; import { useUser } from '@app/hooks/useUser'; import globalMessages from '@app/i18n/globalMessages'; import ErrorPage from '@app/pages/_error'; import defineMessages from '@app/utils/defineMessages'; import { ArrowDownOnSquareIcon } from '@heroicons/react/24/outline'; import { Form, Formik } from 'formik'; import { useRouter } from 'next/router'; import { useIntl } from 'react-intl'; import { useToasts } from 'react-toast-notifications'; import useSWR from 'swr'; const messages = defineMessages( 'components.UserProfile.UserSettings.UserPermissions', { toastSettingsSuccess: 'Permissions saved successfully!', toastSettingsFailure: 'Something went wrong while saving settings.', permissions: 'Permissions', unauthorizedDescription: 'You cannot modify your own permissions.', } ); const UserPermissions = () => { const intl = useIntl(); const { addToast } = useToasts(); const router = useRouter(); const { user: currentUser } = useUser(); const { user, revalidate: revalidateUser } = useUser({ id: Number(router.query.userId), }); const { data, error, mutate: revalidate, } = useSWR<{ permissions?: number }>( user ? `/api/v1/user/${user?.id}/settings/permissions` : null ); if (!data && !error) { return ; } if (!data) { return ; } if (currentUser?.id !== 1 && currentUser?.id === user?.id) { return ( <>

{intl.formatMessage(messages.permissions)}

); } return ( <>

{intl.formatMessage(messages.permissions)}

{ try { const res = await fetch( `/api/v1/user/${user?.id}/settings/permissions`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ permissions: values.currentPermissions ?? 0, }), } ); if (!res.ok) throw new Error(); addToast(intl.formatMessage(messages.toastSettingsSuccess), { autoDismiss: true, appearance: 'success', }); } catch (e) { addToast(intl.formatMessage(messages.toastSettingsFailure), { autoDismiss: true, appearance: 'error', }); } finally { revalidate(); revalidateUser(); } }} > {({ isSubmitting, setFieldValue, values }) => { return (
setFieldValue('currentPermissions', newPermission) } />
); }}
); }; export default UserPermissions;