2021-09-15 14:29:59 +02:00
|
|
|
import React, { useMemo } from 'react';
|
|
|
|
import { useQuery, useMutation, useQueryClient } from 'react-query';
|
2020-08-04 14:33:43 +02:00
|
|
|
import { useIntl } from 'react-intl';
|
2021-09-15 14:29:59 +02:00
|
|
|
import { Formik } from 'formik';
|
2020-08-04 18:10:35 +02:00
|
|
|
import {
|
2021-07-28 08:16:15 +02:00
|
|
|
CheckPagePermissions,
|
2021-09-15 14:29:59 +02:00
|
|
|
Form,
|
|
|
|
GenericInput,
|
2021-09-15 15:10:26 +02:00
|
|
|
LoadingIndicatorPage,
|
|
|
|
SettingsPageTitle,
|
2021-09-15 14:29:59 +02:00
|
|
|
useFocusWhenNavigate,
|
2021-09-15 15:10:26 +02:00
|
|
|
useNotification,
|
|
|
|
useOverlayBlocker,
|
|
|
|
useRBAC,
|
2021-04-29 13:51:12 +02:00
|
|
|
} from '@strapi/helper-plugin';
|
2021-09-15 14:29:59 +02:00
|
|
|
import { useNotifyAT } from '@strapi/parts/LiveRegions';
|
|
|
|
import { Main } from '@strapi/parts/Main';
|
|
|
|
import { HeaderLayout, ContentLayout } from '@strapi/parts/Layout';
|
|
|
|
import { Button } from '@strapi/parts/Button';
|
|
|
|
import { Box } from '@strapi/parts/Box';
|
|
|
|
import { Stack } from '@strapi/parts/Stack';
|
|
|
|
import { Select, Option } from '@strapi/parts/Select';
|
|
|
|
import { H3 } from '@strapi/parts/Text';
|
|
|
|
import { Grid, GridItem } from '@strapi/parts/Grid';
|
|
|
|
import CheckIcon from '@strapi/icons/CheckIcon';
|
2020-08-04 18:10:35 +02:00
|
|
|
import pluginPermissions from '../../permissions';
|
2021-09-15 14:29:59 +02:00
|
|
|
import { getTrad } from '../../utils';
|
|
|
|
import layout from './utils/layout';
|
|
|
|
import schema from './utils/schema';
|
|
|
|
import { fetchData, putAdvancedSettings } from './utils/api';
|
2020-08-04 17:45:45 +02:00
|
|
|
|
2021-08-19 09:42:19 +02:00
|
|
|
const ProtectedAdvancedSettingsPage = () => (
|
2021-07-28 08:16:15 +02:00
|
|
|
<CheckPagePermissions permissions={pluginPermissions.readAdvancedSettings}>
|
|
|
|
<AdvancedSettingsPage />
|
|
|
|
</CheckPagePermissions>
|
|
|
|
);
|
|
|
|
|
2020-08-03 16:49:40 +02:00
|
|
|
const AdvancedSettingsPage = () => {
|
2020-08-04 14:33:43 +02:00
|
|
|
const { formatMessage } = useIntl();
|
2021-05-17 15:55:46 +02:00
|
|
|
const toggleNotification = useNotification();
|
2021-05-17 18:38:00 +02:00
|
|
|
const { lockApp, unlockApp } = useOverlayBlocker();
|
2021-09-15 14:29:59 +02:00
|
|
|
const { notifyStatus } = useNotifyAT();
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
useFocusWhenNavigate();
|
2021-08-04 19:24:10 +02:00
|
|
|
|
2021-09-15 14:29:59 +02:00
|
|
|
const updatePermissions = useMemo(
|
|
|
|
() => ({ update: pluginPermissions.updateAdvancedSettings }),
|
|
|
|
[]
|
|
|
|
);
|
2020-08-04 18:10:35 +02:00
|
|
|
const {
|
|
|
|
isLoading: isLoadingForPermissions,
|
|
|
|
allowedActions: { canUpdate },
|
2021-05-24 15:49:15 +02:00
|
|
|
} = useRBAC(updatePermissions);
|
2021-09-15 14:29:59 +02:00
|
|
|
|
|
|
|
const { status: isLoadingData, data } = useQuery('advanced', () => fetchData(), {
|
|
|
|
onSuccess: () => {
|
|
|
|
notifyStatus(
|
|
|
|
formatMessage({
|
2021-09-17 17:16:31 +02:00
|
|
|
id: getTrad('Form.advancedSettings.data.loaded'),
|
2021-09-15 14:29:59 +02:00
|
|
|
defaultMessage: 'Advanced settings data has been loaded',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onError: () => {
|
|
|
|
toggleNotification({
|
|
|
|
type: 'warning',
|
2021-09-17 17:16:31 +02:00
|
|
|
message: { id: getTrad('notification.error'), defaultMessage: 'An error occured' },
|
2021-09-15 14:29:59 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const isLoading = isLoadingForPermissions || isLoadingData !== 'success';
|
|
|
|
|
|
|
|
const submitMutation = useMutation(body => putAdvancedSettings(body), {
|
|
|
|
onSuccess: async () => {
|
|
|
|
await queryClient.invalidateQueries('advanced');
|
|
|
|
toggleNotification({
|
|
|
|
type: 'success',
|
2021-09-17 17:16:31 +02:00
|
|
|
message: { id: getTrad('notification.success.saved'), defaultMessage: 'Saved' },
|
2021-09-15 14:29:59 +02:00
|
|
|
});
|
2020-08-07 09:28:29 +02:00
|
|
|
|
2021-05-17 18:38:00 +02:00
|
|
|
unlockApp();
|
2020-08-05 15:18:22 +02:00
|
|
|
},
|
2021-09-15 14:29:59 +02:00
|
|
|
onError: () => {
|
|
|
|
toggleNotification({
|
|
|
|
type: 'warning',
|
2021-09-17 17:16:31 +02:00
|
|
|
message: { id: getTrad('notification.error'), defaultMessage: 'An error occured' },
|
2021-09-15 14:29:59 +02:00
|
|
|
});
|
|
|
|
unlockApp();
|
|
|
|
},
|
|
|
|
refetchActive: true,
|
|
|
|
});
|
2020-08-05 15:18:22 +02:00
|
|
|
|
2021-09-15 14:29:59 +02:00
|
|
|
const { isLoading: isSubmittingForm } = submitMutation;
|
|
|
|
|
|
|
|
const handleSubmit = async body => {
|
|
|
|
lockApp();
|
|
|
|
|
|
|
|
const urlConfirmation = body.email_confirmation ? body.email_confirmation_redirection : '';
|
|
|
|
|
|
|
|
await submitMutation.mutateAsync({ ...body, email_confirmation_redirection: urlConfirmation });
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
return (
|
|
|
|
<Main aria-busy="true">
|
|
|
|
<SettingsPageTitle
|
|
|
|
name={formatMessage({
|
|
|
|
id: getTrad('HeaderNav.link.advancedSettings'),
|
|
|
|
defaultMessage: 'Advanced Settings',
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<HeaderLayout
|
|
|
|
title={formatMessage({
|
|
|
|
id: getTrad('HeaderNav.link.advancedSettings'),
|
|
|
|
defaultMessage: 'Advanced Settings',
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<ContentLayout>
|
|
|
|
<LoadingIndicatorPage />
|
|
|
|
</ContentLayout>
|
|
|
|
</Main>
|
|
|
|
);
|
|
|
|
}
|
2020-08-04 14:33:43 +02:00
|
|
|
|
|
|
|
return (
|
2021-09-15 14:29:59 +02:00
|
|
|
<Main aria-busy={isSubmittingForm}>
|
|
|
|
<SettingsPageTitle
|
|
|
|
name={formatMessage({
|
|
|
|
id: getTrad('HeaderNav.link.advancedSettings'),
|
|
|
|
defaultMessage: 'Advanced Settings',
|
|
|
|
})}
|
2020-08-05 15:18:22 +02:00
|
|
|
/>
|
2021-09-15 14:29:59 +02:00
|
|
|
<Formik
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
initialValues={data.settings}
|
|
|
|
validateOnChange={false}
|
|
|
|
validationSchema={schema}
|
|
|
|
enableReinitialize
|
|
|
|
>
|
|
|
|
{({ errors, values, handleChange, isSubmitting }) => {
|
|
|
|
return (
|
|
|
|
<Form>
|
|
|
|
<HeaderLayout
|
|
|
|
title={formatMessage({
|
|
|
|
id: getTrad('HeaderNav.link.advancedSettings'),
|
|
|
|
defaultMessage: 'Advanced Settings',
|
|
|
|
})}
|
|
|
|
primaryAction={
|
|
|
|
<Button
|
|
|
|
loading={isSubmitting}
|
|
|
|
type="submit"
|
|
|
|
disabled={!canUpdate}
|
|
|
|
startIcon={<CheckIcon />}
|
2021-09-20 17:09:52 +02:00
|
|
|
size="L"
|
2021-09-15 14:29:59 +02:00
|
|
|
>
|
2021-09-16 10:27:16 +02:00
|
|
|
{formatMessage({ id: getTrad('Form.save'), defaultMessage: 'Save' })}
|
2021-09-15 14:29:59 +02:00
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<ContentLayout>
|
|
|
|
<Box
|
|
|
|
background="neutral0"
|
|
|
|
hasRadius
|
|
|
|
shadow="filterShadow"
|
|
|
|
paddingTop={6}
|
|
|
|
paddingBottom={6}
|
|
|
|
paddingLeft={7}
|
|
|
|
paddingRight={7}
|
|
|
|
>
|
|
|
|
<Stack size={4}>
|
|
|
|
<H3>
|
|
|
|
{formatMessage({
|
|
|
|
id: getTrad('Form.title.advancedSettings'),
|
|
|
|
defaultMessage: 'Settings',
|
|
|
|
})}
|
|
|
|
</H3>
|
|
|
|
<Grid gap={6}>
|
|
|
|
<GridItem col={6} s={12}>
|
|
|
|
<Select
|
|
|
|
label={formatMessage({
|
|
|
|
id: getTrad('EditForm.inputSelect.label.role'),
|
|
|
|
defaultMessage: 'Default role for authenticated users',
|
|
|
|
})}
|
|
|
|
value={values.default_role}
|
|
|
|
hint={formatMessage({
|
|
|
|
id: getTrad('EditForm.inputSelect.description.role'),
|
|
|
|
defaultMessage:
|
|
|
|
'It will attach the new authenticated user to the selected role.',
|
|
|
|
})}
|
|
|
|
onChange={e =>
|
|
|
|
handleChange({ target: { name: 'default_role', value: e } })}
|
|
|
|
>
|
|
|
|
{data.roles.map(role => {
|
|
|
|
return (
|
|
|
|
<Option key={role.type} value={role.type}>
|
|
|
|
{role.name}
|
|
|
|
</Option>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Select>
|
|
|
|
</GridItem>
|
|
|
|
{layout.map(input => {
|
|
|
|
let value = values[input.name];
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
value = input.type === 'bool' ? false : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<GridItem key={input.name} {...input.size}>
|
|
|
|
<GenericInput
|
|
|
|
{...input}
|
|
|
|
value={value}
|
|
|
|
error={errors[input.name]}
|
|
|
|
disabled={
|
|
|
|
input.name === 'email_confirmation_redirection' &&
|
|
|
|
values.email_confirmation === false
|
|
|
|
}
|
|
|
|
onChange={handleChange}
|
|
|
|
/>
|
|
|
|
</GridItem>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Grid>
|
|
|
|
</Stack>
|
|
|
|
</Box>
|
|
|
|
</ContentLayout>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</Formik>
|
|
|
|
</Main>
|
2020-08-04 14:33:43 +02:00
|
|
|
);
|
2020-08-03 16:49:40 +02:00
|
|
|
};
|
|
|
|
|
2021-08-19 09:42:19 +02:00
|
|
|
export default ProtectedAdvancedSettingsPage;
|