2020-08-04 18:10:35 +02:00
|
|
|
import React, { useCallback, useEffect, useMemo, useReducer } from 'react';
|
2020-08-04 14:33:43 +02:00
|
|
|
import { useIntl } from 'react-intl';
|
2020-08-04 15:59:19 +02:00
|
|
|
import { Header } from '@buffetjs/custom';
|
2020-08-04 18:10:35 +02:00
|
|
|
import {
|
|
|
|
FormBloc,
|
|
|
|
SettingsPageTitle,
|
|
|
|
SizedInput,
|
|
|
|
useUserPermissions,
|
|
|
|
request,
|
|
|
|
} from 'strapi-helper-plugin';
|
2020-08-04 17:45:45 +02:00
|
|
|
import { Container } from 'reactstrap';
|
|
|
|
import styled from 'styled-components';
|
2020-08-04 18:10:35 +02:00
|
|
|
import pluginPermissions from '../../permissions';
|
|
|
|
import { getTrad, getRequestURL } from '../../utils';
|
2020-08-04 17:45:45 +02:00
|
|
|
import ListBaselineAlignment from '../../components/ListBaselineAlignment';
|
|
|
|
import form from './utils/form';
|
2020-08-04 18:10:35 +02:00
|
|
|
import reducer, { initialState } from './reducer';
|
2020-08-04 17:45:45 +02:00
|
|
|
|
|
|
|
const ContainerFluid = styled(Container)`
|
|
|
|
padding: ${({ padding }) => padding};
|
|
|
|
`;
|
2020-08-03 16:49:40 +02:00
|
|
|
|
|
|
|
const AdvancedSettingsPage = () => {
|
2020-08-04 14:33:43 +02:00
|
|
|
const { formatMessage } = useIntl();
|
|
|
|
const pageTitle = formatMessage({ id: getTrad('HeaderNav.link.advancedSettings') });
|
2020-08-04 18:10:35 +02:00
|
|
|
const updatePermissions = useMemo(() => {
|
|
|
|
return { update: pluginPermissions.updateAdvancedSettings };
|
|
|
|
}, []);
|
|
|
|
const {
|
|
|
|
isLoading: isLoadingForPermissions,
|
|
|
|
allowedActions: { canUpdate },
|
|
|
|
} = useUserPermissions(updatePermissions);
|
|
|
|
const [{ isLoading, modifiedData, roles }, dispatch] = useReducer(reducer, initialState);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const getData = async () => {
|
|
|
|
try {
|
|
|
|
dispatch({
|
|
|
|
type: 'GET_DATA',
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await request(getRequestURL('advanced'), { method: 'GET' });
|
|
|
|
console.log({ data });
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'GET_DATA_SUCCEEDED',
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
dispatch({
|
|
|
|
type: 'GET_DATA_ERROR',
|
|
|
|
});
|
|
|
|
console.error(err);
|
|
|
|
strapi.notification.error('notification.error');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!isLoadingForPermissions) {
|
|
|
|
getData();
|
|
|
|
}
|
|
|
|
}, [isLoadingForPermissions]);
|
|
|
|
|
|
|
|
const handleChange = useCallback(({ target }) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'ON_CHANGE',
|
|
|
|
keys: target.name,
|
|
|
|
value: target.value,
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleSubmit = useCallback(e => {
|
2020-08-04 17:45:45 +02:00
|
|
|
e.preventDefault();
|
2020-08-04 18:10:35 +02:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const showLoader = isLoadingForPermissions || isLoading;
|
2020-08-04 14:33:43 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<SettingsPageTitle name={pageTitle} />
|
2020-08-04 15:59:19 +02:00
|
|
|
<div>
|
2020-08-04 17:45:45 +02:00
|
|
|
<form onSubmit={handleSubmit}>
|
2020-08-04 18:10:35 +02:00
|
|
|
<Header title={{ label: pageTitle }} isLoading={showLoader} />
|
2020-08-04 17:45:45 +02:00
|
|
|
<ContainerFluid padding="0">
|
|
|
|
<ListBaselineAlignment />
|
2020-08-04 18:10:35 +02:00
|
|
|
<FormBloc title="Settings" isLoading={showLoader}>
|
2020-08-04 17:45:45 +02:00
|
|
|
{form.map(input => {
|
2020-08-04 18:10:35 +02:00
|
|
|
return (
|
|
|
|
<SizedInput
|
|
|
|
key={input.name}
|
|
|
|
{...input}
|
|
|
|
disabled={!canUpdate}
|
|
|
|
onChange={handleChange}
|
|
|
|
options={roles}
|
|
|
|
value={modifiedData[input.name]}
|
|
|
|
/>
|
|
|
|
);
|
2020-08-04 17:45:45 +02:00
|
|
|
})}
|
|
|
|
</FormBloc>
|
|
|
|
</ContainerFluid>
|
|
|
|
</form>
|
2020-08-04 15:59:19 +02:00
|
|
|
</div>
|
2020-08-04 14:33:43 +02:00
|
|
|
</>
|
|
|
|
);
|
2020-08-03 16:49:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default AdvancedSettingsPage;
|