2021-09-08 18:34:55 +02:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import {
|
|
|
|
Main,
|
|
|
|
HeaderLayout,
|
|
|
|
Button,
|
|
|
|
Stack,
|
|
|
|
Box,
|
|
|
|
GridItem,
|
|
|
|
Grid,
|
|
|
|
TextInput,
|
|
|
|
Textarea,
|
|
|
|
} from '@strapi/parts';
|
|
|
|
import { H3 } from '@strapi/parts/Text';
|
|
|
|
import { CheckIcon } from '@strapi/icons';
|
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 {
|
|
|
|
useOverlayBlocker,
|
|
|
|
SettingsPageTitle,
|
2021-09-08 18:34:55 +02:00
|
|
|
CustomContentLayout,
|
|
|
|
LoadingIndicatorPage,
|
|
|
|
useNotification,
|
2021-09-06 14:23:40 +02:00
|
|
|
} 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';
|
|
|
|
import schema from './utils/schema';
|
2021-09-08 18:34:55 +02:00
|
|
|
import axiosInstance from '../../../utils/axiosInstance';
|
2020-08-06 16:27:51 +02:00
|
|
|
|
|
|
|
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-08 18:34:55 +02:00
|
|
|
const { isLoading: isLoadingPlugins } = usePlugins();
|
|
|
|
const { role, onSubmitSucceeded, isLoading: isLoadingRole } = useFetchRole(id);
|
2020-08-06 16:27:51 +02:00
|
|
|
|
2021-09-08 18:34:55 +02:00
|
|
|
const handleCreateRoleSubmit = async data => {
|
|
|
|
// Set loading state
|
2021-05-17 18:38:00 +02:00
|
|
|
lockApp();
|
2021-09-06 14:23:40 +02:00
|
|
|
setIsSubmitting(true);
|
2021-09-08 18:34:55 +02:00
|
|
|
try {
|
|
|
|
// Update role in Strapi
|
|
|
|
await axiosInstance.put(`/${pluginId}/roles/${id}`, { ...data, users: [] });
|
|
|
|
// Notify success
|
|
|
|
onSubmitSucceeded({ name: data.name, description: data.description });
|
|
|
|
toggleNotification({
|
|
|
|
type: 'success',
|
|
|
|
message: {
|
|
|
|
id: getTrad('Settings.roles.edited'),
|
|
|
|
defaultMessage: 'Role edited',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
toggleNotification({
|
|
|
|
type: 'warning',
|
|
|
|
message: {
|
|
|
|
id: 'notification.error',
|
|
|
|
defaultMessage: 'An error occurred',
|
|
|
|
},
|
2020-08-06 16:27:51 +02:00
|
|
|
});
|
2021-09-08 18:34:55 +02:00
|
|
|
}
|
|
|
|
// Unset loading state
|
|
|
|
setIsSubmitting(false);
|
|
|
|
unlockApp();
|
2020-08-06 16:27:51 +02:00
|
|
|
};
|
|
|
|
|
2021-09-08 18:34:55 +02:00
|
|
|
if (isLoadingRole) {
|
|
|
|
return <LoadingIndicatorPage />;
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
>
|
2021-09-08 18:34:55 +02:00
|
|
|
{({ handleSubmit, values, handleBlur, handleChange, errors }) => (
|
|
|
|
<>
|
2021-09-06 14:23:40 +02:00
|
|
|
<HeaderLayout
|
|
|
|
id="title"
|
|
|
|
primaryAction={
|
2021-09-08 18:34:55 +02:00
|
|
|
!isLoadingPlugins && (
|
2021-09-06 14:23:40 +02:00
|
|
|
<Button
|
|
|
|
disabled={role.code === 'strapi-super-admin'}
|
|
|
|
onClick={handleSubmit}
|
|
|
|
loading={isSubmitting}
|
2021-09-08 18:34:55 +02:00
|
|
|
startIcon={<CheckIcon />}
|
2021-09-06 14:23:40 +02:00
|
|
|
>
|
|
|
|
{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-08 18:34:55 +02:00
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<CustomContentLayout>
|
|
|
|
<Stack size={7}>
|
|
|
|
<Box
|
|
|
|
background="neutral0"
|
|
|
|
hasRadius
|
|
|
|
shadow="filterShadow"
|
|
|
|
paddingTop={6}
|
|
|
|
paddingBottom={6}
|
|
|
|
paddingLeft={7}
|
|
|
|
paddingRight={7}
|
|
|
|
>
|
|
|
|
<Stack size={4}>
|
|
|
|
<H3>
|
|
|
|
{formatMessage({
|
|
|
|
id: getTrad('EditPage.form.roles'),
|
|
|
|
defaultMessage: 'Role details',
|
|
|
|
})}
|
|
|
|
</H3>
|
|
|
|
<Grid gap={4}>
|
|
|
|
<GridItem col={6}>
|
|
|
|
<TextInput
|
|
|
|
name="name"
|
|
|
|
value={values.name || ''}
|
|
|
|
onChange={handleChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
label={formatMessage({
|
|
|
|
id: 'Settings.roles.form.input.name',
|
|
|
|
defaultMessage: 'Name',
|
|
|
|
})}
|
|
|
|
error={errors.name ? { id: errors.name } : null}
|
|
|
|
/>
|
|
|
|
</GridItem>
|
|
|
|
<GridItem col={6}>
|
|
|
|
<Textarea
|
|
|
|
name="description"
|
|
|
|
value={values.description || ''}
|
|
|
|
onChange={handleChange}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
label={formatMessage({
|
|
|
|
id: 'Settings.roles.form.input.description',
|
|
|
|
defaultMessage: 'Description',
|
|
|
|
})}
|
|
|
|
error={errors.description ? { id: errors.description } : null}
|
|
|
|
/>
|
|
|
|
</GridItem>
|
|
|
|
</Grid>
|
|
|
|
</Stack>
|
|
|
|
</Box>
|
|
|
|
</Stack>
|
|
|
|
</CustomContentLayout>
|
|
|
|
</form>
|
|
|
|
</>
|
2021-09-06 14:23:40 +02:00
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
</Main>
|
2020-08-06 16:27:51 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EditPage;
|