2020-08-06 16:27:51 +02:00
|
|
|
import React, { useState, useRef } from 'react';
|
|
|
|
import { Header } from '@buffetjs/custom';
|
|
|
|
import { Padded } from '@buffetjs/core';
|
|
|
|
import { Formik } from 'formik';
|
|
|
|
import { useIntl } from 'react-intl';
|
|
|
|
import { useRouteMatch } from 'react-router-dom';
|
2021-05-17 18:38:00 +02:00
|
|
|
import { request, useNotification, useOverlayBlocker } from '@strapi/helper-plugin';
|
2020-08-04 13:07:55 +02:00
|
|
|
|
2020-08-06 16:27:51 +02:00
|
|
|
import BaselineAlignement from '../../../components/BaselineAlignement';
|
|
|
|
import ContainerFluid from '../../../components/ContainerFluid';
|
|
|
|
import FormCard from '../../../components/FormBloc';
|
|
|
|
import SizedInput from '../../../components/SizedInput';
|
|
|
|
import getTrad from '../../../utils/getTrad';
|
|
|
|
import pluginId from '../../../pluginId';
|
|
|
|
import UsersPermissions from '../../../components/UsersPermissions';
|
|
|
|
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();
|
|
|
|
const [isSubmiting, setIsSubmiting] = 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`);
|
2020-09-14 14:28:13 +02:00
|
|
|
const { routes, policies, isLoading } = usePlugins();
|
2020-08-27 15:57:20 +02:00
|
|
|
const { role, isLoading: isRoleLoading, onSubmitSucceeded } = useFetchRole(id);
|
2020-08-06 16:27:51 +02:00
|
|
|
const permissionsRef = useRef();
|
|
|
|
|
2020-08-27 15:00:10 +02:00
|
|
|
const headerActions = (handleSubmit, handleReset) => {
|
|
|
|
if (isLoading) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: formatMessage({
|
2021-08-04 22:23:00 +08:00
|
|
|
id: 'app.components.Button.reset',
|
2020-08-27 15:00:10 +02:00
|
|
|
defaultMessage: 'Reset',
|
|
|
|
}),
|
2020-08-27 15:57:20 +02:00
|
|
|
onClick: () => {
|
|
|
|
handleReset();
|
|
|
|
permissionsRef.current.resetForm();
|
|
|
|
},
|
2020-08-27 15:00:10 +02:00
|
|
|
color: 'cancel',
|
|
|
|
type: 'button',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: formatMessage({
|
2021-08-04 22:23:00 +08:00
|
|
|
id: 'app.components.Button.save',
|
2020-08-27 15:00:10 +02:00
|
|
|
defaultMessage: 'Save',
|
|
|
|
}),
|
|
|
|
onClick: handleSubmit,
|
|
|
|
color: 'success',
|
|
|
|
type: 'submit',
|
|
|
|
isLoading: isSubmiting,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
};
|
2020-08-06 16:27:51 +02:00
|
|
|
|
|
|
|
const handleCreateRoleSubmit = data => {
|
2021-05-17 18:38:00 +02:00
|
|
|
lockApp();
|
2020-08-06 16:27:51 +02:00
|
|
|
setIsSubmiting(true);
|
|
|
|
|
|
|
|
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(() => {
|
|
|
|
setIsSubmiting(false);
|
2021-05-17 18:38:00 +02:00
|
|
|
unlockApp();
|
2020-08-06 16:27:51 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
enableReinitialize
|
|
|
|
initialValues={{ name: role.name, description: role.description }}
|
|
|
|
onSubmit={handleCreateRoleSubmit}
|
|
|
|
validationSchema={schema}
|
|
|
|
>
|
|
|
|
{({ handleSubmit, values, errors, handleReset, handleChange, handleBlur }) => (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<ContainerFluid padding="0">
|
|
|
|
<Header
|
|
|
|
title={{
|
|
|
|
label: role.name,
|
|
|
|
}}
|
|
|
|
content={role.description}
|
|
|
|
actions={headerActions(handleSubmit, handleReset)}
|
|
|
|
isLoading={isRoleLoading}
|
|
|
|
/>
|
|
|
|
<BaselineAlignement top size="3px" />
|
|
|
|
<FormCard
|
|
|
|
title={formatMessage({
|
|
|
|
id: getTrad('EditPage.form.roles'),
|
|
|
|
defaultMessage: 'Role details',
|
|
|
|
})}
|
2020-08-27 15:00:10 +02:00
|
|
|
isLoading={isLoading}
|
2020-08-06 16:27:51 +02:00
|
|
|
>
|
|
|
|
<SizedInput
|
|
|
|
label="Settings.roles.form.input.name"
|
|
|
|
defaultMessage="Name"
|
|
|
|
name="name"
|
|
|
|
type="text"
|
|
|
|
error={errors.name ? { id: errors.name } : null}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
value={values.name}
|
|
|
|
onChange={handleChange}
|
|
|
|
/>
|
|
|
|
<SizedInput
|
|
|
|
label="Settings.roles.form.input.description"
|
|
|
|
defaultMessage="Description"
|
|
|
|
name="description"
|
|
|
|
type="textarea"
|
|
|
|
error={errors.description ? { id: errors.description } : null}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
value={values.description}
|
|
|
|
onChange={handleChange}
|
|
|
|
// Override the default height of the textarea
|
|
|
|
style={{ height: 115 }}
|
|
|
|
/>
|
|
|
|
</FormCard>
|
|
|
|
</ContainerFluid>
|
|
|
|
<div style={{ paddingTop: '1.8rem' }} />
|
2020-08-27 15:00:10 +02:00
|
|
|
{!isLoading && !isRoleLoading && (
|
2020-08-06 16:27:51 +02:00
|
|
|
<UsersPermissions
|
|
|
|
ref={permissionsRef}
|
2020-09-14 14:28:13 +02:00
|
|
|
permissions={role.permissions}
|
2020-08-06 16:27:51 +02:00
|
|
|
routes={routes}
|
|
|
|
policies={policies}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<Padded top size="md" />
|
|
|
|
</form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EditPage;
|