280 lines
7.7 KiB
JavaScript
Raw Normal View History

2019-10-18 10:28:21 +02:00
import React, { useState, useMemo } from 'react';
2019-10-15 15:12:53 +02:00
import PropTypes from 'prop-types';
2019-10-18 10:28:21 +02:00
import { get, isEqual, upperFirst } from 'lodash';
2019-10-15 15:12:53 +02:00
import { withRouter } from 'react-router-dom';
2019-10-16 16:00:24 +02:00
import { FormattedMessage } from 'react-intl';
2019-11-07 16:38:57 +01:00
import { Inputs as Input, Header } from '@buffetjs/custom';
2019-10-15 16:36:50 +02:00
import {
BackHeader,
LoadingIndicatorPage,
2019-10-18 10:28:21 +02:00
PopUpWarning,
// contexts
useGlobalContext,
2019-10-15 16:36:50 +02:00
} from 'strapi-helper-plugin';
2019-10-18 10:28:21 +02:00
import pluginId from '../../pluginId';
2019-10-15 15:12:53 +02:00
import Block from '../Block';
import Container from '../Container';
import SectionTitle from '../SectionTitle';
2019-10-15 16:36:50 +02:00
import Separator from '../Separator';
2019-10-15 15:12:53 +02:00
const SettingsViewWrapper = ({
2019-10-15 18:32:19 +02:00
children,
2019-10-15 15:12:53 +02:00
history: { goBack },
displayedFields,
2019-10-15 16:36:50 +02:00
inputs,
2019-10-18 10:28:21 +02:00
initialData,
isEditSettings,
2019-10-15 16:36:50 +02:00
isLoading,
modifiedData,
onChange,
2019-10-18 10:28:21 +02:00
onConfirmReset,
onConfirmSubmit,
2019-11-21 12:18:08 +01:00
name,
2019-10-15 15:12:53 +02:00
}) => {
2019-11-07 16:38:57 +01:00
const { emitEvent, formatMessage } = useGlobalContext();
2019-10-18 10:28:21 +02:00
const [showWarningCancel, setWarningCancel] = useState(false);
const [showWarningSubmit, setWarningSubmit] = useState(false);
const attributes = useMemo(() => {
return get(modifiedData, ['schema', 'attributes'], {});
2019-10-18 10:28:21 +02:00
}, [modifiedData]);
const toggleWarningCancel = () => setWarningCancel(prevState => !prevState);
const toggleWarningSubmit = () => setWarningSubmit(prevState => !prevState);
const getPluginHeaderActions = () => {
return [
{
2019-11-07 16:38:57 +01:00
color: 'cancel',
2019-10-18 10:28:21 +02:00
onClick: toggleWarningCancel,
2020-01-06 01:52:26 +01:00
label: formatMessage({
2019-11-07 16:38:57 +01:00
id: `${pluginId}.popUpWarning.button.cancel`,
}),
2019-10-18 10:28:21 +02:00
type: 'button',
disabled: isEqual(modifiedData, initialData),
2019-11-28 19:23:10 +01:00
style: {
fontWeight: 600,
paddingLeft: 15,
paddingRight: 15,
},
2019-10-18 10:28:21 +02:00
},
{
2019-11-07 16:38:57 +01:00
color: 'success',
2020-01-06 01:52:26 +01:00
label: formatMessage({
2019-11-07 16:38:57 +01:00
id: `${pluginId}.containers.Edit.submit`,
}),
2019-10-18 10:28:21 +02:00
type: 'submit',
disabled: isEqual(modifiedData, initialData),
2019-11-28 19:23:10 +01:00
style: {
minWidth: 150,
fontWeight: 600,
},
2019-10-18 10:28:21 +02:00
},
];
};
2019-11-07 16:38:57 +01:00
const headerProps = {
2019-10-18 10:28:21 +02:00
actions: getPluginHeaderActions(),
title: {
2019-11-07 16:38:57 +01:00
label: formatMessage(
{
id: `${pluginId}.components.SettingsViewWrapper.pluginHeader.title`,
},
2019-11-21 12:18:08 +01:00
{ name: upperFirst(name) }
2019-11-07 16:38:57 +01:00
),
2019-10-18 10:28:21 +02:00
},
2019-11-07 16:38:57 +01:00
content: formatMessage({
2019-10-18 10:28:21 +02:00
id: `${pluginId}.components.SettingsViewWrapper.pluginHeader.description.${
isEditSettings ? 'edit' : 'list'
}-settings`,
2019-11-07 16:38:57 +01:00
}),
2019-10-18 10:28:21 +02:00
};
const getSelectOptions = input => {
if (input.name === 'settings.defaultSortBy') {
return [
'id',
...displayedFields.filter(
2019-10-18 10:28:21 +02:00
name =>
get(attributes, [name, 'type'], '') !== 'media' &&
2019-10-18 10:28:21 +02:00
name !== 'id' &&
get(attributes, [name, 'type'], '') !== 'richtext'
2019-10-18 10:28:21 +02:00
),
];
}
if (input.name === 'settings.mainField') {
const options = Object.keys(attributes).filter(attr => {
const type = get(attributes, [attr, 'type'], '');
return (
![
'json',
'text',
'relation',
2019-10-28 11:31:37 +01:00
'component',
2019-10-18 10:28:21 +02:00
'boolean',
'date',
'media',
'richtext',
].includes(type) && !!type
);
});
return options;
}
return input.options;
};
const handleSubmit = e => {
e.preventDefault();
toggleWarningSubmit();
emitEvent('willSaveContentTypeLayout');
};
2019-10-15 16:36:50 +02:00
if (isLoading) {
return <LoadingIndicatorPage />;
}
2019-10-15 15:12:53 +02:00
return (
<>
<BackHeader onClick={goBack} />
<Container className="container-fluid">
2019-10-18 10:28:21 +02:00
<form onSubmit={handleSubmit}>
2019-11-07 16:38:57 +01:00
<Header {...headerProps} />
<div
className="row"
style={{
paddingTop: '3px',
}}
>
2019-10-15 15:12:53 +02:00
<Block
style={{
marginBottom: '13px',
paddingBottom: '30px',
paddingTop: '24px',
}}
>
<SectionTitle isSettings />
2019-10-15 16:36:50 +02:00
<div className="row">
{inputs.map(input => {
return (
2019-10-16 16:00:24 +02:00
<FormattedMessage key={input.name} id={input.label.id}>
{label => (
<div className={input.customBootstrapClass}>
2019-10-16 16:00:24 +02:00
<FormattedMessage
id={get(input, 'description.id', 'app.utils.defaultMessage')}
2019-10-16 16:00:24 +02:00
>
{description => (
<Input
{...input}
description={description}
label={label === ' ' ? null : label}
onChange={onChange}
options={getSelectOptions(input)}
value={get(modifiedData, input.name, '')}
/>
)}
</FormattedMessage>
</div>
)}
</FormattedMessage>
2019-10-15 16:36:50 +02:00
);
})}
<div className="col-12">
<Separator style={{ marginBottom: 20 }} />
2019-10-15 16:36:50 +02:00
</div>
</div>
2019-10-15 18:32:19 +02:00
<SectionTitle />
{children}
2019-10-15 15:12:53 +02:00
</Block>
</div>
2019-10-18 10:28:21 +02:00
<PopUpWarning
isOpen={showWarningCancel}
toggleModal={toggleWarningCancel}
content={{
title: `${pluginId}.popUpWarning.title`,
message: `${pluginId}.popUpWarning.warning.cancelAllSettings`,
cancel: `${pluginId}.popUpWarning.button.cancel`,
confirm: `${pluginId}.popUpWarning.button.confirm`,
}}
popUpWarningType="danger"
onConfirm={() => {
onConfirmReset();
toggleWarningCancel();
}}
/>
<PopUpWarning
isOpen={showWarningSubmit}
toggleModal={toggleWarningSubmit}
content={{
title: `${pluginId}.popUpWarning.title`,
message: `${pluginId}.popUpWarning.warning.updateAllSettings`,
cancel: `${pluginId}.popUpWarning.button.cancel`,
confirm: `${pluginId}.popUpWarning.button.confirm`,
}}
popUpWarningType="danger"
onConfirm={async () => {
await onConfirmSubmit();
toggleWarningSubmit();
}}
/>
2019-10-15 15:12:53 +02:00
</form>
</Container>
</>
);
};
SettingsViewWrapper.defaultProps = {
displayedFields: [],
2019-10-15 16:36:50 +02:00
inputs: [],
2019-10-18 10:28:21 +02:00
initialData: {},
isEditSettings: false,
2019-10-15 16:36:50 +02:00
modifiedData: {},
2019-11-26 16:24:53 +01:00
name: '',
2019-10-18 10:28:21 +02:00
onConfirmReset: () => {},
onConfirmSubmit: async () => {},
2019-10-15 15:12:53 +02:00
onSubmit: () => {},
pluginHeaderProps: {
actions: [],
description: {
id: 'app.utils.defaultMessage',
},
title: {
id: 'app.utils.defaultMessage',
values: {},
},
},
};
SettingsViewWrapper.propTypes = {
2019-10-15 18:32:19 +02:00
children: PropTypes.node.isRequired,
displayedFields: PropTypes.array,
2019-10-15 15:12:53 +02:00
history: PropTypes.shape({
goBack: PropTypes.func.isRequired,
}).isRequired,
2019-10-18 10:28:21 +02:00
initialData: PropTypes.object,
2019-10-15 16:36:50 +02:00
inputs: PropTypes.array,
2019-10-18 10:28:21 +02:00
isEditSettings: PropTypes.bool,
2019-10-15 16:36:50 +02:00
isLoading: PropTypes.bool.isRequired,
modifiedData: PropTypes.object,
2019-11-26 16:24:53 +01:00
name: PropTypes.string,
2019-10-15 16:36:50 +02:00
onChange: PropTypes.func.isRequired,
2019-10-18 10:28:21 +02:00
onConfirmReset: PropTypes.func,
onConfirmSubmit: PropTypes.func,
2019-10-15 15:12:53 +02:00
onSubmit: PropTypes.func,
pluginHeaderProps: PropTypes.shape({
actions: PropTypes.array,
description: PropTypes.shape({
id: PropTypes.string,
}),
title: PropTypes.shape({
id: PropTypes.string,
values: PropTypes.object,
}),
}),
};
export default withRouter(SettingsViewWrapper);