2021-02-16 15:16:16 +01:00
|
|
|
import React, { useMemo, useState } from 'react';
|
2021-02-16 11:26:42 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2021-02-16 15:16:16 +01:00
|
|
|
import { cloneDeep, get, groupBy, set } from 'lodash';
|
2021-02-16 11:26:42 +01:00
|
|
|
import { Modal, ModalHeader, ModalFooter } from 'strapi-helper-plugin';
|
|
|
|
import { Button, Text, Padded } from '@buffetjs/core';
|
|
|
|
import { useIntl } from 'react-intl';
|
2021-02-16 15:16:16 +01:00
|
|
|
import createDefaultConditionsForm from './utils/createDefaultConditionsForm';
|
|
|
|
import ActionRow from './ActionRow';
|
2021-02-16 11:26:42 +01:00
|
|
|
import Separator from './Separator';
|
2021-02-16 15:16:16 +01:00
|
|
|
import { usePermissionsDataManager } from '../contexts/PermissionsDataManagerContext';
|
|
|
|
import updateValues from '../Permissions/utils/updateValues';
|
2021-02-16 11:26:42 +01:00
|
|
|
|
2021-02-16 15:16:16 +01:00
|
|
|
const ConditionsModal = ({ actions, headerBreadCrumbs, isOpen, onClosed, onToggle }) => {
|
2021-02-16 11:26:42 +01:00
|
|
|
const { formatMessage } = useIntl();
|
2021-02-16 15:16:16 +01:00
|
|
|
const { availableConditions, modifiedData } = usePermissionsDataManager();
|
|
|
|
|
|
|
|
const arrayOfOptionsGroupedByCategory = useMemo(() => {
|
|
|
|
return Object.entries(groupBy(availableConditions, 'category'));
|
|
|
|
}, [availableConditions]);
|
|
|
|
|
|
|
|
const actionsToDisplay = actions.filter(
|
|
|
|
({ isDisplayed, hasSomeActionsSelected, hasAllActionsSelected }) =>
|
|
|
|
isDisplayed && (hasSomeActionsSelected || hasAllActionsSelected)
|
|
|
|
);
|
|
|
|
|
|
|
|
const initState = useMemo(() => {
|
|
|
|
return createDefaultConditionsForm(
|
|
|
|
actionsToDisplay,
|
|
|
|
modifiedData,
|
|
|
|
arrayOfOptionsGroupedByCategory
|
|
|
|
);
|
|
|
|
}, [actionsToDisplay, modifiedData, arrayOfOptionsGroupedByCategory]);
|
|
|
|
|
|
|
|
const [state, setState] = useState(initState);
|
|
|
|
|
|
|
|
const handleCategoryChange = ({ keys, value }) => {
|
|
|
|
setState(prevState => {
|
|
|
|
const updatedState = cloneDeep(prevState);
|
|
|
|
const objToUpdate = get(prevState, keys, {});
|
|
|
|
const updatedValues = updateValues(objToUpdate, value);
|
|
|
|
|
|
|
|
set(updatedState, keys, updatedValues);
|
|
|
|
|
|
|
|
return updatedState;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleChange = ({ keys, value }) => {
|
|
|
|
setState(prevState => {
|
|
|
|
const updatedState = cloneDeep(prevState);
|
|
|
|
|
|
|
|
set(updatedState, keys, value);
|
|
|
|
|
|
|
|
return updatedState;
|
|
|
|
});
|
|
|
|
};
|
2021-02-16 11:26:42 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal withoverflow="true" onClosed={onClosed} isOpen={isOpen} onToggle={onToggle}>
|
|
|
|
<ModalHeader headerBreadcrumbs={headerBreadCrumbs} />
|
|
|
|
<Padded top left right bottom size="md">
|
|
|
|
<Text fontSize="lg" fontWeight="bold">
|
|
|
|
{formatMessage({
|
|
|
|
id: 'Settings.permissions.conditions.define-conditions',
|
|
|
|
})}
|
|
|
|
</Text>
|
|
|
|
<Separator />
|
2021-02-16 15:16:16 +01:00
|
|
|
{actionsToDisplay.length === 0 && (
|
2021-02-16 11:26:42 +01:00
|
|
|
<Text fontSize="md" color="grey">
|
|
|
|
{formatMessage({ id: 'Settings.permissions.conditions.no-actions' })}
|
|
|
|
</Text>
|
|
|
|
)}
|
2021-02-16 15:16:16 +01:00
|
|
|
{actionsToDisplay.map(({ actionId, label, pathToConditionsObject }, index) => {
|
|
|
|
const name = pathToConditionsObject.join('..');
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ActionRow
|
|
|
|
key={actionId}
|
|
|
|
arrayOfOptionsGroupedByCategory={arrayOfOptionsGroupedByCategory}
|
|
|
|
label={label}
|
|
|
|
isGrey={index % 2 === 0}
|
|
|
|
name={name}
|
|
|
|
onCategoryChange={handleCategoryChange}
|
|
|
|
onChange={handleChange}
|
|
|
|
value={get(state, name, {})}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
2021-02-16 11:26:42 +01:00
|
|
|
</Padded>
|
|
|
|
<ModalFooter>
|
|
|
|
<section>
|
|
|
|
<Button type="button" color="cancel" onClick={onToggle}>
|
|
|
|
{formatMessage({ id: 'app.components.Button.cancel' })}
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button type="button" color="success" onClick={() => console.log('todo')}>
|
|
|
|
{formatMessage({
|
|
|
|
id: 'Settings.permissions.conditions.apply',
|
|
|
|
})}
|
|
|
|
</Button>
|
|
|
|
</section>
|
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ConditionsModal.propTypes = {
|
2021-02-16 15:16:16 +01:00
|
|
|
actions: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
actionId: PropTypes.string.isRequired,
|
|
|
|
checkboxName: PropTypes.string,
|
|
|
|
hasSomeActionsSelected: PropTypes.bool.isRequired,
|
|
|
|
hasAllActionsSelected: PropTypes.bool,
|
|
|
|
isDisplayed: PropTypes.bool.isRequired,
|
|
|
|
label: PropTypes.string,
|
|
|
|
})
|
|
|
|
).isRequired,
|
2021-02-16 11:26:42 +01:00
|
|
|
headerBreadCrumbs: PropTypes.arrayOf(PropTypes.string).isRequired,
|
|
|
|
isOpen: PropTypes.bool.isRequired,
|
|
|
|
onClosed: PropTypes.func.isRequired,
|
|
|
|
onToggle: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ConditionsModal;
|