Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed count values from child groups with text #4

Merged
merged 2 commits into from
Jan 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions src/components/manage/Blocks/Group/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Icon } from '@plone/volto/components';
import delightedSVG from '@plone/volto/icons/delighted.svg';
import dissatisfiedSVG from '@plone/volto/icons/dissatisfied.svg';
import { isEmpty } from 'lodash';
import PropTypes from 'prop-types';
import React, { useState } from 'react';
import './editor.less';

Expand Down Expand Up @@ -42,12 +43,24 @@ const Edit = (props) => {
const blockState = {};
let charCount = 0;

const countTextInBlocks = (blocksObject) => {
let groupCharCount = 0;

Object.keys(blocksObject).forEach((blockId) => {
const charCountTemp = blocksObject[blockId]?.text?.blocks[0]?.text
? blocksObject[blockId].text.blocks[0].text.length
: blocksObject[blockId]['@type'] === 'group'
? countTextInBlocks(blocksObject[blockId]?.data?.blocks)
: 0;
groupCharCount = groupCharCount + charCountTemp;
});

return groupCharCount;
};

const showCharCounter = () => {
if (props.data?.data?.blocks) {
Object.keys(props.data?.data?.blocks).forEach((blockId) => {
charCount =
charCount + props.data.data.blocks[blockId]?.plaintext?.length || 0;
});
charCount = countTextInBlocks(props.data?.data?.blocks);
}
};
showCharCounter();
Expand All @@ -62,26 +75,23 @@ const Edit = (props) => {
: colors.danger,
textAlign: 'end',
};

const counterComponent = props.data.maxChars ? (
<p style={counterStyle} className="counter">
{props.data.maxChars ? (
props.data.maxChars - charCount < 0 ? (
<>
<span>{`${
charCount - props.data.maxChars
} characters over the limit`}</span>
<Icon name={dissatisfiedSVG} size="24px" />
</>
) : (
<>
<span>{`${
props.data.maxChars - charCount
} characters remaining out of ${props.data.maxChars}`}</span>
<Icon name={delightedSVG} size="24px" />
</>
)
{props.data.maxChars - charCount < 0 ? (
<>
<span>{`${
charCount - props.data.maxChars
} characters over the limit`}</span>
<Icon name={dissatisfiedSVG} size="24px" />
</>
) : (
charCount
<>
<span>{`${
props.data.maxChars - charCount
} characters remaining out of ${props.data.maxChars}`}</span>
<Icon name={delightedSVG} size="24px" />
</>
)}
</p>
) : null;
Expand Down Expand Up @@ -123,4 +133,13 @@ const Edit = (props) => {
);
};

Edit.propTypes = {
block: PropTypes.object.isRequired,
data: PropTypes.object.isRequired,
onChangeBlock: PropTypes.func.isRequired,
pathname: PropTypes.string.isRequired,
selected: PropTypes.bool.isRequired,
manage: PropTypes.bool.isRequired,
};

export default Edit;