2020-06-05 14:26:29 +02:00
|
|
|
import React, { useState } from 'react';
|
2020-05-27 17:07:10 +02:00
|
|
|
import { Header } from '@buffetjs/custom';
|
|
|
|
import { Padded } from '@buffetjs/core';
|
|
|
|
import { Formik } from 'formik';
|
|
|
|
import { useIntl } from 'react-intl';
|
2020-06-18 11:01:44 +02:00
|
|
|
import { CheckPagePermissions, request } from 'strapi-helper-plugin';
|
2020-05-27 17:07:10 +02:00
|
|
|
import { useHistory } from 'react-router-dom';
|
2020-06-18 11:01:44 +02:00
|
|
|
import adminPermissions from '../../../../src/permissions';
|
2020-06-05 14:26:29 +02:00
|
|
|
import { useFetchPermissionsLayout } from '../../../../src/hooks';
|
2020-05-27 17:07:10 +02:00
|
|
|
import BaselineAlignement from '../../../../src/components/BaselineAlignement';
|
|
|
|
import ContainerFluid from '../../../../src/components/ContainerFluid';
|
|
|
|
import FormCard from '../../../../src/components/FormBloc';
|
2020-06-01 10:53:36 +02:00
|
|
|
import { ButtonWithNumber, Permissions } from '../../../../src/components/Roles';
|
2020-05-27 17:07:10 +02:00
|
|
|
import SizedInput from '../../../../src/components/SizedInput';
|
|
|
|
|
|
|
|
import schema from './utils/schema';
|
|
|
|
|
|
|
|
const CreatePage = () => {
|
|
|
|
const { formatMessage } = useIntl();
|
2020-06-05 14:26:29 +02:00
|
|
|
const [isSubmiting, setIsSubmiting] = useState(false);
|
|
|
|
// @HichamELBSI Adding the layout since you might need it for the plugins sections
|
|
|
|
const { isLoading: isLayoutLoading } = useFetchPermissionsLayout();
|
2020-05-27 17:07:10 +02:00
|
|
|
const { goBack } = useHistory();
|
|
|
|
|
|
|
|
const headerActions = (handleSubmit, handleReset) => [
|
|
|
|
{
|
|
|
|
label: formatMessage({
|
|
|
|
id: 'app.components.Button.reset',
|
2020-06-09 16:58:48 +02:00
|
|
|
defaultMessage: 'Reset',
|
2020-05-27 17:07:10 +02:00
|
|
|
}),
|
|
|
|
onClick: handleReset,
|
|
|
|
color: 'cancel',
|
|
|
|
type: 'button',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: formatMessage({
|
|
|
|
id: 'app.components.Button.save',
|
2020-06-09 16:58:48 +02:00
|
|
|
defaultMessage: 'Save',
|
2020-05-27 17:07:10 +02:00
|
|
|
}),
|
|
|
|
onClick: handleSubmit,
|
|
|
|
color: 'success',
|
|
|
|
type: 'submit',
|
2020-06-05 14:26:29 +02:00
|
|
|
isLoading: isSubmiting,
|
2020-05-27 17:07:10 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const handleCreateRoleSubmit = async data => {
|
|
|
|
try {
|
2020-06-05 14:26:29 +02:00
|
|
|
setIsSubmiting(true);
|
2020-05-27 17:07:10 +02:00
|
|
|
const res = await request('/admin/roles', {
|
|
|
|
method: 'POST',
|
|
|
|
body: data,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res.data.id) {
|
|
|
|
strapi.notification.success('Settings.roles.created');
|
|
|
|
goBack();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// if (err.response) {
|
|
|
|
// const data = get(err, 'response.payload', { data: {} });
|
|
|
|
// const apiErrors = formatAPIErrors(data);
|
|
|
|
// }
|
|
|
|
strapi.notification.error('notification.error');
|
2020-06-05 14:26:29 +02:00
|
|
|
setIsSubmiting(false);
|
2020-05-27 17:07:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions = [
|
|
|
|
<ButtonWithNumber number={0} onClick={() => console.log('Open user modal')} key="user-button">
|
|
|
|
{formatMessage({
|
|
|
|
id: 'Settings.roles.form.button.users-with-role',
|
2020-06-09 16:58:48 +02:00
|
|
|
defaultMessage: 'Users with this role',
|
2020-05-27 17:07:10 +02:00
|
|
|
})}
|
|
|
|
</ButtonWithNumber>,
|
|
|
|
];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
2020-06-05 23:23:52 +02:00
|
|
|
initialValues={{ name: '', description: '' }}
|
2020-05-27 17:07:10 +02:00
|
|
|
onSubmit={handleCreateRoleSubmit}
|
|
|
|
validationSchema={schema}
|
|
|
|
>
|
|
|
|
{({ handleSubmit, values, errors, handleReset, handleChange, handleBlur }) => (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<ContainerFluid padding="0">
|
|
|
|
<Header
|
|
|
|
title={{
|
|
|
|
label: formatMessage({
|
|
|
|
id: 'Settings.roles.create.title',
|
2020-06-09 16:58:48 +02:00
|
|
|
defaultMessage: 'Create a role',
|
2020-05-27 17:07:10 +02:00
|
|
|
}),
|
|
|
|
}}
|
|
|
|
content={formatMessage({
|
|
|
|
id: 'Settings.roles.create.description',
|
2020-06-09 16:58:48 +02:00
|
|
|
defaultMessage: 'Define the rights given to the role',
|
2020-05-27 17:07:10 +02:00
|
|
|
})}
|
|
|
|
actions={headerActions(handleSubmit, handleReset)}
|
2020-06-05 14:26:29 +02:00
|
|
|
isLoading={isLayoutLoading}
|
2020-05-27 17:07:10 +02:00
|
|
|
/>
|
|
|
|
<BaselineAlignement top size="3px" />
|
|
|
|
<FormCard
|
|
|
|
actions={actions}
|
|
|
|
title={formatMessage({
|
|
|
|
id: 'Settings.roles.form.title',
|
2020-06-09 16:58:48 +02:00
|
|
|
defaultMessage: 'Details',
|
2020-05-27 17:07:10 +02:00
|
|
|
})}
|
|
|
|
subtitle={formatMessage({
|
|
|
|
id: 'Settings.roles.form.description',
|
2020-06-09 16:58:48 +02:00
|
|
|
defaultMessage: 'Name and description of the role',
|
2020-05-27 17:07:10 +02:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
<SizedInput
|
2020-06-09 16:58:48 +02:00
|
|
|
label="Settings.roles.form.input.name"
|
|
|
|
defaultMessage="Name"
|
2020-05-27 17:07:10 +02:00
|
|
|
name="name"
|
|
|
|
type="text"
|
|
|
|
error={errors.name ? { id: errors.name } : null}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
value={values.name}
|
|
|
|
onChange={handleChange}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<SizedInput
|
2020-06-09 16:58:48 +02:00
|
|
|
label="Settings.roles.form.input.description"
|
|
|
|
defaultMessage="Description"
|
2020-05-27 17:07:10 +02:00
|
|
|
name="description"
|
|
|
|
type="textarea"
|
|
|
|
onBlur={handleBlur}
|
|
|
|
value={values.description}
|
|
|
|
onChange={handleChange}
|
|
|
|
// Override the default height of the textarea
|
|
|
|
style={{ height: 115 }}
|
|
|
|
/>
|
|
|
|
</FormCard>
|
2020-06-05 14:26:29 +02:00
|
|
|
{!isLayoutLoading && (
|
2020-06-05 09:37:05 +02:00
|
|
|
<Padded top bottom size="md">
|
2020-06-05 14:26:29 +02:00
|
|
|
<Permissions />
|
|
|
|
</Padded>
|
|
|
|
)}
|
2020-05-27 17:07:10 +02:00
|
|
|
</ContainerFluid>
|
|
|
|
</form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-18 11:01:44 +02:00
|
|
|
export default () => (
|
|
|
|
<CheckPagePermissions permissions={adminPermissions.settings.roles.create}>
|
|
|
|
<CreatePage />
|
|
|
|
</CheckPagePermissions>
|
|
|
|
);
|
|
|
|
|
|
|
|
export { CreatePage };
|