2020-08-06 16:27:51 +02:00
|
|
|
import React, { useState, useRef } from 'react';
|
2021-09-06 14:23:40 +02:00
|
|
|
import { Main, HeaderLayout, Button } from '@strapi/parts';
|
2020-08-06 16:27:51 +02:00
|
|
|
import { Formik } from 'formik';
|
|
|
|
import { useIntl } from 'react-intl';
|
|
|
|
import { useRouteMatch } from 'react-router-dom';
|
2021-09-06 14:23:40 +02:00
|
|
|
import {
|
|
|
|
request,
|
|
|
|
useNotification,
|
|
|
|
useOverlayBlocker,
|
|
|
|
SettingsPageTitle,
|
|
|
|
} from '@strapi/helper-plugin';
|
2020-08-04 13:07:55 +02:00
|
|
|
|
2020-08-06 16:27:51 +02:00
|
|
|
import getTrad from '../../../utils/getTrad';
|
|
|
|
import pluginId from '../../../pluginId';
|
|
|
|
import { usePlugins, useFetchRole } from '../../../hooks';
|
2020-08-04 13:07:55 +02:00
|
|
|
|
2020-08-06 16:27:51 +02:00
|
|
|
import schema from './utils/schema';
|
|
|
|
|
|
|
|
const EditPage = () => {
|
|
|
|
const { formatMessage } = useIntl();
|
2021-09-06 14:23:40 +02:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
2021-05-17 15:55:46 +02:00
|
|
|
const toggleNotification = useNotification();
|
2021-05-17 18:38:00 +02:00
|
|
|
const { lockApp, unlockApp } = useOverlayBlocker();
|
2020-08-06 16:27:51 +02:00
|
|
|
const {
|
|
|
|
params: { id },
|
2021-05-05 11:28:17 +02:00
|
|
|
} = useRouteMatch(`/settings/${pluginId}/roles/:id`);
|
2021-09-06 14:23:40 +02:00
|
|
|
const { isLoading } = usePlugins();
|
|
|
|
const { role, onSubmitSucceeded } = useFetchRole(id);
|
2020-08-06 16:27:51 +02:00
|
|
|
const permissionsRef = useRef();
|
|
|
|
|
|
|
|
const handleCreateRoleSubmit = data => {
|
2021-05-17 18:38:00 +02:00
|
|
|
lockApp();
|
2021-09-06 14:23:40 +02:00
|
|
|
setIsSubmitting(true);
|
2020-08-06 16:27:51 +02:00
|
|
|
|
|
|
|
const permissions = permissionsRef.current.getPermissions();
|
|
|
|
|
|
|
|
Promise.resolve(
|
|
|
|
request(`/${pluginId}/roles/${id}`, {
|
|
|
|
method: 'PUT',
|
|
|
|
body: { ...data, ...permissions, users: [] },
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(() => {
|
2020-08-27 15:57:20 +02:00
|
|
|
onSubmitSucceeded({ name: data.name, description: data.description });
|
|
|
|
permissionsRef.current.setFormAfterSubmit();
|
2021-05-17 15:55:46 +02:00
|
|
|
toggleNotification({
|
2020-10-02 18:58:15 +02:00
|
|
|
type: 'success',
|
|
|
|
message: { id: getTrad('Settings.roles.edited') },
|
|
|
|
});
|
2020-08-06 16:27:51 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
2021-05-17 15:55:46 +02:00
|
|
|
toggleNotification({
|
2020-10-02 18:58:15 +02:00
|
|
|
type: 'warning',
|
|
|
|
message: { id: 'notification.error' },
|
|
|
|
});
|
2020-08-06 16:27:51 +02:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2021-09-06 14:23:40 +02:00
|
|
|
setIsSubmitting(false);
|
2021-05-17 18:38:00 +02:00
|
|
|
unlockApp();
|
2020-08-06 16:27:51 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-09-06 14:23:40 +02:00
|
|
|
<Main labelledBy="title">
|
|
|
|
<SettingsPageTitle name="Roles" />
|
|
|
|
<Formik
|
|
|
|
enableReinitialize
|
|
|
|
initialValues={{ name: role.name, description: role.description }}
|
|
|
|
onSubmit={handleCreateRoleSubmit}
|
|
|
|
validationSchema={schema}
|
|
|
|
>
|
|
|
|
{({ handleSubmit }) => (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<HeaderLayout
|
|
|
|
id="title"
|
|
|
|
primaryAction={
|
|
|
|
!isLoading && (
|
|
|
|
<Button
|
|
|
|
disabled={role.code === 'strapi-super-admin'}
|
|
|
|
onClick={handleSubmit}
|
|
|
|
loading={isSubmitting}
|
|
|
|
>
|
|
|
|
{formatMessage({
|
|
|
|
id: 'app.components.Button.save',
|
|
|
|
defaultMessage: 'Save',
|
|
|
|
})}
|
|
|
|
</Button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
title={role.name}
|
|
|
|
subtitle={role.description}
|
2020-08-06 16:27:51 +02:00
|
|
|
/>
|
2021-09-06 14:23:40 +02:00
|
|
|
</form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
</Main>
|
2020-08-06 16:27:51 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EditPage;
|