From d51746e7b5758ef61803500ab22c53caa192227c Mon Sep 17 00:00:00 2001 From: soupette Date: Thu, 11 Feb 2021 18:56:14 +0100 Subject: [PATCH] Add checkbox state to all checkboxes Signed-off-by: soupette --- .../ContentTypeCollapse/Collapse/index.js | 41 ++++++++++++++++--- .../CollapsePropertyMatrix/ActionRow/index.js | 23 ++++++++--- .../utils/getRowLabelCheckboxeState.js | 37 +++++++++++++++++ .../SubActionRow/index.js | 10 ++--- .../CreatePage/ContentTypeCollapse/index.js | 1 + .../utils/getCheckboxState.js | 19 --------- .../ContentTypeCollapse/utils/index.js | 1 - .../Roles/CreatePage/ContentTypes/index.js | 2 +- .../Roles/CreatePage/GlobalActions/index.js | 18 +++++--- .../utils/getRowLabelCheckboxesState.js | 32 +++++++++++++++ .../CreatePage/GlobalActions/utils/index.js | 2 + .../Roles/CreatePage/Permissions/reducer.js | 40 ++++++++++++++++++ .../Roles/CreatePage/RowLabel/index.js | 17 +++++++- .../Roles/CreatePage/temp/fakeData.js | 2 +- .../utils/createArrayOfValues.js | 0 .../CreatePage/utils/getCheckboxState.js | 16 ++++++++ .../Roles/CreatePage/utils/index.js | 2 + .../utils/tests/createArrayOfValues.test.js | 0 18 files changed, 216 insertions(+), 47 deletions(-) create mode 100644 packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/CollapsePropertyMatrix/ActionRow/utils/getRowLabelCheckboxeState.js delete mode 100644 packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/utils/getCheckboxState.js create mode 100644 packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/utils/getRowLabelCheckboxesState.js create mode 100644 packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/utils/index.js rename packages/strapi-admin/ee/admin/containers/Roles/CreatePage/{ContentTypeCollapse => }/utils/createArrayOfValues.js (100%) create mode 100644 packages/strapi-admin/ee/admin/containers/Roles/CreatePage/utils/getCheckboxState.js create mode 100644 packages/strapi-admin/ee/admin/containers/Roles/CreatePage/utils/index.js rename packages/strapi-admin/ee/admin/containers/Roles/CreatePage/{ContentTypeCollapse => }/utils/tests/createArrayOfValues.test.js (100%) diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/Collapse/index.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/Collapse/index.js index 01a4af5206..8fc1da63e4 100644 --- a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/Collapse/index.js +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/Collapse/index.js @@ -1,29 +1,57 @@ import React from 'react'; +import { get } from 'lodash'; import PropTypes from 'prop-types'; import { Flex, Padded } from '@buffetjs/core'; +import { usePermissionsDataManager } from '../../contexts/PermissionsDataManagerContext'; import CheckboxWithCondition from '../../CheckboxWithCondition'; import Chevron from '../../Chevron'; import ConditionsButton from '../../ConditionsButton'; import HiddenAction from '../../HiddenAction'; import Wrapper from './Wrapper'; import RowLabel from '../../RowLabel'; +import { getCheckboxState } from '../../utils'; + +const Collapse = ({ availableActions, isActive, isGrey, name, onClickToggle, pathToData }) => { + const { modifiedData } = usePermissionsDataManager(); + + // This corresponds to the data related to the CT left checkboxe + // modifiedData: { collectionTypes: { [ctuid]: {create: {fields: {f1: true} } } } } + const mainData = get(modifiedData, pathToData.split('..'), {}); + const { hasAllActionsSelected, hasSomeActionsSelected } = getCheckboxState(mainData); -const Collapse = ({ availableActions, isActive, isGrey, name, onClickToggle }) => { return ( - + - {availableActions.map(action => { - if (!action.isDisplayed) { - return ; + {availableActions.map(({ actionId, isDisplayed }) => { + if (!isDisplayed) { + return ; } - return ; + const checkboxName = [...pathToData.split('..'), actionId]; + const mainData = get(modifiedData, checkboxName, null); + + const { hasAllActionsSelected, hasSomeActionsSelected } = getCheckboxState(mainData); + + return ( + + ); })} console.log('todo')} /> @@ -38,6 +66,7 @@ Collapse.propTypes = { isGrey: PropTypes.bool.isRequired, name: PropTypes.string.isRequired, onClickToggle: PropTypes.func.isRequired, + pathToData: PropTypes.string.isRequired, }; export default Collapse; diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/CollapsePropertyMatrix/ActionRow/index.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/CollapsePropertyMatrix/ActionRow/index.js index f6e072a723..9eda848ab1 100644 --- a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/CollapsePropertyMatrix/ActionRow/index.js +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/CollapsePropertyMatrix/ActionRow/index.js @@ -3,14 +3,15 @@ import PropTypes from 'prop-types'; import { get } from 'lodash'; import { Padded, Flex } from '@buffetjs/core'; import { usePermissionsDataManager } from '../../../contexts/PermissionsDataManagerContext'; +import { getCheckboxState } from '../../../utils'; import CheckboxWithCondition from '../../../CheckboxWithCondition'; import Chevron from '../../../Chevron'; import HiddenAction from '../../../HiddenAction'; import RequiredSign from '../../../RequiredSign'; -import { getCheckboxState } from '../../utils'; import RowLabel from '../../../RowLabel'; import SubActionRow from '../SubActionRow'; import Wrapper from './Wrapper'; +import getRowLabelCheckboxeState from './utils/getRowLabelCheckboxeState'; const ActionRow = ({ childrenForm, @@ -48,12 +49,23 @@ const ActionRow = ({ } }, [isCollapsable, name]); + const { hasAllActionsSelected, hasSomeActionsSelected } = useMemo(() => { + return getRowLabelCheckboxeState(propertyActions, modifiedData, pathToData, propertyName, name); + }, [propertyActions, modifiedData, pathToData, propertyName, name]); + return ( <> - + {required && } @@ -78,10 +90,9 @@ const ActionRow = ({ ); } - const { hasAllActionsSelected, hasSomeActionsSelected } = getCheckboxState( - checkboxName, - modifiedData - ); + const data = get(modifiedData, checkboxName, {}); + + const { hasAllActionsSelected, hasSomeActionsSelected } = getCheckboxState(data); return ( { + const actionIds = propertyActions.reduce((acc, current) => { + if (current.isActionRelatedToCurrentProperty) { + acc.push(current.actionId); + } + + return acc; + }, []); + + return actionIds; +}; + +const getRowLabelCheckboxeState = ( + propertyActions, + modifiedData, + pathToContentType, + propertyToCheck, + targetKey +) => { + const actionIds = getActionIdsFromPropertyActions(propertyActions); + + const data = actionIds.reduce((acc, current) => { + const pathToData = [...pathToContentType.split('..'), current, propertyToCheck, targetKey]; + const mainData = get(modifiedData, pathToData, false); + + acc[current] = mainData; + + return acc; + }, {}); + + return getCheckboxState(data); +}; + +export default getRowLabelCheckboxeState; diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/CollapsePropertyMatrix/SubActionRow/index.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/CollapsePropertyMatrix/SubActionRow/index.js index fa23a681cb..1fdee93552 100644 --- a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/CollapsePropertyMatrix/SubActionRow/index.js +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/CollapsePropertyMatrix/SubActionRow/index.js @@ -4,13 +4,13 @@ import { get } from 'lodash'; import { Flex, Text } from '@buffetjs/core'; import styled from 'styled-components'; import { usePermissionsDataManager } from '../../../contexts/PermissionsDataManagerContext'; +import { getCheckboxState } from '../../../utils'; import CheckboxWithCondition from '../../../CheckboxWithCondition'; import Chevron from '../../../Chevron'; import CollapseLabel from '../../../CollapseLabel'; import Curve from '../../../Curve'; import HiddenAction from '../../../HiddenAction'; import RequiredSign from '../../../RequiredSign'; -import { getCheckboxState } from '../../utils'; import { RowStyle, RowWrapper } from './row'; import { LeftBorderTimeline, TopTimeline } from './timeline'; import Wrapper from './Wrapper'; @@ -27,7 +27,6 @@ const SubActionRow = ({ parentName, propertyName, }) => { - console.log({ pathToDataFromActionRow }); const { modifiedData, onChangeSimpleCheckbox } = usePermissionsDataManager(); const [rowToOpen, setRowToOpen] = useState(null); const handleClickToggleSubLevel = useCallback(name => { @@ -101,9 +100,9 @@ const SubActionRow = ({ value, ]; - if (!subChildrenForm) { - const checkboxValue = get(modifiedData, checkboxName, 'test'); + const checkboxValue = get(modifiedData, checkboxName, 'test'); + if (!subChildrenForm) { return ( {isActive && properties.map(({ label, value, children: childrenForm }, i) => { diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/utils/getCheckboxState.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/utils/getCheckboxState.js deleted file mode 100644 index 740228f752..0000000000 --- a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/utils/getCheckboxState.js +++ /dev/null @@ -1,19 +0,0 @@ -import { get } from 'lodash'; -import createArrayOfValues from './createArrayOfValues'; - -const getValueFromModifiedData = (pathToData, modifiedData) => { - const data = get(modifiedData, pathToData, {}); - - return createArrayOfValues(data); -}; - -const getCheckboxState = (pathToData, modifiedData) => { - const arrayOfValues = getValueFromModifiedData(pathToData, modifiedData); - - const hasAllActionsSelected = arrayOfValues.every(val => val); - const hasSomeActionsSelected = arrayOfValues.some(val => val) && !hasAllActionsSelected; - - return { hasAllActionsSelected, hasSomeActionsSelected }; -}; - -export default getCheckboxState; diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/utils/index.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/utils/index.js index 631432ddc2..2aaa8770eb 100644 --- a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/utils/index.js +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/utils/index.js @@ -1,3 +1,2 @@ export { default as activeStyle } from './activeStyle'; export { default as getAvailableActions } from './getAvailableActions'; -export { default as getCheckboxState } from './getCheckboxState'; diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypes/index.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypes/index.js index 55618dce3b..753f09424b 100644 --- a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypes/index.js +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypes/index.js @@ -9,7 +9,7 @@ const ContentTypes = ({ kind, layout: { actions, subjects } }) => { return ( - + diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/index.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/index.js index a2a13e46cd..355377a1a8 100644 --- a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/index.js +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/index.js @@ -1,19 +1,25 @@ import React, { memo, useMemo } from 'react'; +import { get } from 'lodash'; import PropTypes from 'prop-types'; import { Flex } from '@buffetjs/core'; import { useIntl } from 'react-intl'; +import { usePermissionsDataManager } from '../contexts/PermissionsDataManagerContext'; import CheckboxWithCondition from '../CheckboxWithCondition'; +import { findDisplayedActions, getCheckboxesState } from './utils'; import Wrapper from './Wrapper'; -const GlobalActions = ({ actions }) => { +const GlobalActions = ({ actions, kind }) => { const { formatMessage } = useIntl(); + const { modifiedData } = usePermissionsDataManager(); const displayedActions = useMemo(() => { - return actions.filter(({ subjects }) => { - return subjects.length; - }); + return findDisplayedActions(actions); }, [actions]); + const checkboxesState = useMemo(() => { + return getCheckboxesState(displayedActions, modifiedData[kind]); + }, [modifiedData, displayedActions, kind]); + return ( @@ -26,7 +32,8 @@ const GlobalActions = ({ actions }) => { defaultMessage: label, })} name={actionId} - value={false} + value={get(checkboxesState, [actionId, 'hasAllActionsSelected'], false)} + someChecked={get(checkboxesState, [actionId, 'hasSomeActionsSelected'], false)} /> ); })} @@ -47,6 +54,7 @@ GlobalActions.propTypes = { subjects: PropTypes.array.isRequired, }) ), + kind: PropTypes.string.isRequired, }; export default memo(GlobalActions); diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/utils/getRowLabelCheckboxesState.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/utils/getRowLabelCheckboxesState.js new file mode 100644 index 0000000000..d2d8d52027 --- /dev/null +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/utils/getRowLabelCheckboxesState.js @@ -0,0 +1,32 @@ +import { get } from 'lodash'; +import { getCheckboxState } from '../../utils'; + +const getCheckboxesState = (properties, modifiedData) => { + const actionsIds = properties.map(({ actionId }) => actionId); + + const relatedActionsData = actionsIds.reduce((acc, actionId) => { + Object.keys(modifiedData).forEach(ctUid => { + const actionIdData = get(modifiedData, [ctUid, actionId], {}); + + const actionIdState = { [ctUid]: actionIdData }; + + if (!acc[actionId]) { + acc[actionId] = actionIdState; + } else { + acc[actionId] = { ...acc[actionId], ...actionIdState }; + } + }); + + return acc; + }, {}); + + const checkboxesState = Object.keys(relatedActionsData).reduce((acc, current) => { + acc[current] = getCheckboxState(relatedActionsData[current]); + + return acc; + }, {}); + + return checkboxesState; +}; + +export default getCheckboxesState; diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/utils/index.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/utils/index.js new file mode 100644 index 0000000000..1240bb1d18 --- /dev/null +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/GlobalActions/utils/index.js @@ -0,0 +1,2 @@ +export { default as findDisplayedActions } from './findDisplayedActions'; +export { default as getCheckboxesState } from './getRowLabelCheckboxesState'; diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/Permissions/reducer.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/Permissions/reducer.js index e965fc0b9b..d3de1112c2 100644 --- a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/Permissions/reducer.js +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/Permissions/reducer.js @@ -15,6 +15,24 @@ const initialState = { city: true, }, }, + 'content-manager.explorer.read': { + fields: { + postal_coder: true, + categories: false, + cover: true, + images: true, + city: true, + }, + }, + 'content-manager.explorer.update': { + fields: { + postal_coder: true, + categories: false, + cover: true, + images: true, + city: true, + }, + }, }, restaurant: { 'content-manager.explorer.create': { @@ -31,6 +49,28 @@ const initialState = { }, }, dz: true, + relation: true, + }, + locales: { + fr: true, + en: true, + }, + }, + 'content-manager.explorer.read': { + fields: { + f1: true, + f2: true, + services: { + name: true, + media: true, + closing: { + name: { + test: true, + }, + }, + }, + dz: true, + relation: true, }, locales: { fr: true, diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/RowLabel/index.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/RowLabel/index.js index 9d66aa9f97..b3e2006a96 100644 --- a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/RowLabel/index.js +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/RowLabel/index.js @@ -4,10 +4,19 @@ import { Checkbox, Text } from '@buffetjs/core'; import CollapseLabel from '../CollapseLabel'; import Wrapper from './Wrapper'; -const RowLabel = ({ isCollapsable, label, children, onClick, textColor, width }) => { +const RowLabel = ({ + value, + someChecked, + isCollapsable, + label, + children, + onClick, + textColor, + width, +}) => { return ( - + { + const arrayOfValues = createArrayOfValues(data); + + if (!arrayOfValues.length) { + return { hasAllActionsSelected: false, hasSomeActionsSelected: false }; + } + + const hasAllActionsSelected = arrayOfValues.every(val => val); + const hasSomeActionsSelected = arrayOfValues.some(val => val) && !hasAllActionsSelected; + + return { hasAllActionsSelected, hasSomeActionsSelected }; +}; + +export default getCheckboxState; diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/utils/index.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/utils/index.js new file mode 100644 index 0000000000..b78386af5f --- /dev/null +++ b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/utils/index.js @@ -0,0 +1,2 @@ +export { default as getCheckboxState } from './getCheckboxState'; +export { default as createArrayOfValues } from './createArrayOfValues'; diff --git a/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/utils/tests/createArrayOfValues.test.js b/packages/strapi-admin/ee/admin/containers/Roles/CreatePage/utils/tests/createArrayOfValues.test.js similarity index 100% rename from packages/strapi-admin/ee/admin/containers/Roles/CreatePage/ContentTypeCollapse/utils/tests/createArrayOfValues.test.js rename to packages/strapi-admin/ee/admin/containers/Roles/CreatePage/utils/tests/createArrayOfValues.test.js