2020-08-07 09:56:06 +02:00
|
|
|
import React, { useCallback, useEffect, useMemo, useReducer, useRef, useState } 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-05 15:18:22 +02:00
|
|
|
import { isEqual } from 'lodash';
|
2020-08-04 18:10:35 +02:00
|
|
|
import {
|
|
|
|
FormBloc,
|
2020-08-05 15:18:22 +02:00
|
|
|
PopUpWarning,
|
2020-08-04 18:10:35 +02:00
|
|
|
SettingsPageTitle,
|
|
|
|
SizedInput,
|
|
|
|
useUserPermissions,
|
|
|
|
request,
|
|
|
|
} from 'strapi-helper-plugin';
|
|
|
|
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
|
|
|
|
2020-08-03 16:49:40 +02:00
|
|
|
const AdvancedSettingsPage = () => {
|
2020-08-04 14:33:43 +02:00
|
|
|
const { formatMessage } = useIntl();
|
2020-08-05 15:18:22 +02:00
|
|
|
const [showModalWarning, setShowModalWarning] = useState(false);
|
2020-08-04 14:33:43 +02:00
|
|
|
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);
|
2020-08-05 15:18:22 +02:00
|
|
|
const [
|
|
|
|
{ initialData, isConfirmButtonLoading, isLoading, modifiedData, roles },
|
|
|
|
dispatch,
|
|
|
|
] = useReducer(reducer, initialState);
|
2020-08-07 09:56:06 +02:00
|
|
|
const isMounted = useRef(true);
|
|
|
|
const abortController = new AbortController();
|
|
|
|
const { signal } = abortController;
|
2020-08-04 18:10:35 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const getData = async () => {
|
|
|
|
try {
|
|
|
|
dispatch({
|
|
|
|
type: 'GET_DATA',
|
|
|
|
});
|
|
|
|
|
2020-08-07 09:56:06 +02:00
|
|
|
const data = await request(getRequestURL('advanced'), { method: 'GET', signal });
|
2020-08-04 18:10:35 +02:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'GET_DATA_SUCCEEDED',
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2020-08-07 09:56:06 +02:00
|
|
|
if (isMounted.current) {
|
|
|
|
dispatch({
|
|
|
|
type: 'GET_DATA_ERROR',
|
|
|
|
});
|
|
|
|
console.error(err);
|
2020-10-02 18:58:15 +02:00
|
|
|
strapi.notification.toggle({
|
|
|
|
type: 'warning',
|
|
|
|
message: { id: 'notification.error' },
|
|
|
|
});
|
2020-08-07 09:56:06 +02:00
|
|
|
}
|
2020-08-04 18:10:35 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!isLoadingForPermissions) {
|
|
|
|
getData();
|
|
|
|
}
|
2020-08-07 09:56:06 +02:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
abortController.abort();
|
|
|
|
isMounted.current = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2020-08-04 18:10:35 +02:00
|
|
|
}, [isLoadingForPermissions]);
|
|
|
|
|
|
|
|
const handleChange = useCallback(({ target }) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'ON_CHANGE',
|
|
|
|
keys: target.name,
|
|
|
|
value: target.value,
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2020-08-05 15:18:22 +02:00
|
|
|
const handleSubmit = useCallback(
|
|
|
|
async e => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
try {
|
|
|
|
dispatch({
|
|
|
|
type: 'ON_SUBMIT',
|
|
|
|
});
|
|
|
|
|
2020-08-07 09:28:29 +02:00
|
|
|
strapi.lockAppWithOverlay();
|
2020-08-05 15:18:22 +02:00
|
|
|
await request(getRequestURL('advanced'), { method: 'PUT', body: modifiedData });
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'ON_SUBMIT_SUCCEEDED',
|
|
|
|
});
|
|
|
|
|
2020-10-02 18:58:15 +02:00
|
|
|
strapi.notification.toggle({
|
|
|
|
type: 'success',
|
|
|
|
message: { id: getTrad('notification.success.submit') },
|
|
|
|
});
|
2020-08-05 15:18:22 +02:00
|
|
|
} catch (err) {
|
|
|
|
dispatch({
|
|
|
|
type: 'ON_SUBMIT_ERROR',
|
|
|
|
});
|
|
|
|
console.error(err);
|
2020-10-02 18:58:15 +02:00
|
|
|
strapi.notification.toggle({
|
|
|
|
type: 'warning',
|
|
|
|
message: { id: 'notification.error' },
|
|
|
|
});
|
2020-08-05 15:18:22 +02:00
|
|
|
}
|
2020-08-07 09:28:29 +02:00
|
|
|
|
|
|
|
strapi.unlockApp();
|
2020-08-05 15:18:22 +02:00
|
|
|
},
|
|
|
|
[modifiedData]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleConfirmReset = useCallback(() => {
|
|
|
|
dispatch({
|
|
|
|
type: 'ON_RESET',
|
|
|
|
});
|
|
|
|
|
|
|
|
setShowModalWarning(false);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleToggleModal = useCallback(() => {
|
|
|
|
setShowModalWarning(prev => !prev);
|
2020-08-04 18:10:35 +02:00
|
|
|
}, []);
|
|
|
|
|
2020-08-05 15:18:22 +02:00
|
|
|
const headerActions = useMemo(() => {
|
|
|
|
const isDisabled = isEqual(initialData, modifiedData);
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
disabled: isDisabled,
|
|
|
|
onClick: () => {
|
|
|
|
handleToggleModal();
|
|
|
|
},
|
|
|
|
color: 'cancel',
|
|
|
|
label: formatMessage({
|
|
|
|
id: 'app.components.Button.reset',
|
|
|
|
}),
|
|
|
|
|
|
|
|
type: 'button',
|
|
|
|
style: {
|
|
|
|
paddingLeft: 15,
|
|
|
|
paddingRight: 15,
|
|
|
|
fontWeight: 600,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
disabled: isDisabled,
|
|
|
|
color: 'success',
|
|
|
|
label: formatMessage({
|
|
|
|
id: 'app.components.Button.save',
|
|
|
|
}),
|
|
|
|
isLoading: isConfirmButtonLoading,
|
|
|
|
type: 'submit',
|
|
|
|
style: {
|
|
|
|
minWidth: 150,
|
|
|
|
fontWeight: 600,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}, [initialData, isConfirmButtonLoading, modifiedData, formatMessage, handleToggleModal]);
|
|
|
|
|
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-05 15:18:22 +02:00
|
|
|
<Header actions={headerActions} title={{ label: pageTitle }} isLoading={showLoader} />
|
|
|
|
<ListBaselineAlignment />
|
|
|
|
<FormBloc title="Settings" isLoading={showLoader}>
|
|
|
|
{form.map(input => {
|
|
|
|
return (
|
|
|
|
<SizedInput
|
|
|
|
key={input.name}
|
|
|
|
{...input}
|
|
|
|
disabled={!canUpdate}
|
|
|
|
onChange={handleChange}
|
|
|
|
options={roles}
|
|
|
|
value={modifiedData[input.name]}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</FormBloc>
|
2020-08-04 17:45:45 +02:00
|
|
|
</form>
|
2020-08-04 15:59:19 +02:00
|
|
|
</div>
|
2020-08-05 15:18:22 +02:00
|
|
|
<PopUpWarning
|
|
|
|
isOpen={showModalWarning}
|
|
|
|
toggleModal={handleToggleModal}
|
2020-08-07 10:00:31 +02:00
|
|
|
content={{
|
|
|
|
title: getTrad('popUpWarning.title'),
|
|
|
|
message: getTrad('popUpWarning.warning.cancel'),
|
|
|
|
cancel: getTrad('popUpWarning.button.cancel'),
|
|
|
|
confirm: getTrad('popUpWarning.button.confirm'),
|
|
|
|
}}
|
2020-08-05 15:18:22 +02:00
|
|
|
popUpWarningType="danger"
|
|
|
|
onConfirm={handleConfirmReset}
|
|
|
|
/>
|
2020-08-04 14:33:43 +02:00
|
|
|
</>
|
|
|
|
);
|
2020-08-03 16:49:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default AdvancedSettingsPage;
|