Skip to content

Commit

Permalink
prevent deleting first accordion block child
Browse files Browse the repository at this point in the history
  • Loading branch information
nileshgulia1 committed Nov 18, 2020
1 parent 3ee30e0 commit f99fd66
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/components/manage/Blocks/Accordion/Edit.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from 'react';
import { isEmpty } from 'lodash';
import { BlocksForm } from '@eeacms/volto-blocks-form/components';
import { Segment, Button } from 'semantic-ui-react';
import { Segment, Button, Grid } from 'semantic-ui-react';
import { emptyBlocksForm } from '@eeacms/volto-blocks-form/helpers';
import { SidebarPortal, Icon } from '@plone/volto/components';
import InlineForm from '@plone/volto/components/manage/Form/InlineForm';
Expand Down Expand Up @@ -125,8 +125,22 @@ const Edit = (props) => {
>
{({ draginfo }, editBlock, blockProps) => (
<EditBlockWrapper
accordionData={coldata}
colId={colId}
draginfo={draginfo}
blockProps={blockProps}
onChangeFormData={(newFormData) => {
onChangeBlock(block, {
...data,
data: {
...coldata,
blocks: {
...coldata.blocks,
[colId]: newFormData,
},
},
});
}}
extraControls={
<>
<Button
Expand Down
24 changes: 22 additions & 2 deletions src/components/manage/Blocks/Accordion/EditBlockWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import includes from 'lodash/includes';
import isBoolean from 'lodash/isBoolean';
import { defineMessages, injectIntl } from 'react-intl';
import { doesNodeContainClick } from 'semantic-ui-react/dist/commonjs/lib';
import { deleteBlock } from './util';
import { previousBlockId } from '@eeacms/volto-blocks-form/helpers';

import dragSVG from '@plone/volto/icons/drag.svg';
import addSVG from '@plone/volto/icons/circle-plus.svg';
Expand Down Expand Up @@ -58,22 +60,40 @@ class EditBlockWrapper extends React.Component {
blockNode = React.createRef();

render() {
const { intl, blockProps, draginfo, extraControls, children } = this.props;
const {
intl,
blockProps,
draginfo,
extraControls,
children,
onChangeFormData,
accordionData,
colId,
} = this.props;

const {
allowedBlocks,
block,
data,
onDeleteBlock,
onMutateBlock,
selected,
onSelectBlock,
} = blockProps;
const type = data['@type'];
const { disableNewBlocks } = data;

// const visible = selected && blockHasValue(block) && !block.fixed;
// visibility: visible ? 'visible' : 'hidden',

const onDeleteBlock = (id, selectPrev) => {
const previous = previousBlockId(accordionData, id, colId);

const newFormData = deleteBlock(accordionData, id, colId);
onChangeFormData(newFormData);

onSelectBlock(selectPrev ? previous : null);
};

const required = isBoolean(data.required)
? data.required
: includes(blocks.requiredBlocks, type);
Expand Down
6 changes: 6 additions & 0 deletions src/components/manage/Blocks/Accordion/editor.less
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
.accordion-title {
background: #f3f4f5;
}

.block {
.block-add-button {
display: none;
}
}
}

.align-arrow-left {
Expand Down
23 changes: 22 additions & 1 deletion src/components/manage/Blocks/Accordion/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { v4 as uuid } from 'uuid';
import { emptyBlocksForm } from '@eeacms/volto-blocks-form/helpers';
import { map } from 'lodash';
import { map, omit, without, keys } from 'lodash';

import {
getBlocksFieldname,
getBlocksLayoutFieldname,
Expand Down Expand Up @@ -40,3 +41,23 @@ export const GroupblockHasValue = (content) => {
});
return blockValue.length === 1 ? blockValue[0] : true;
};

export const deleteBlock = (formData, blockId, colId) => {
const blocksFieldname = getBlocksFieldname(formData);
const blocksLayoutFieldname = getBlocksLayoutFieldname(formData);
const panelData = formData[blocksFieldname][colId];

return {
...panelData,
[blocksLayoutFieldname]: {
items:
panelData[blocksLayoutFieldname].items.length > 1
? without(panelData[blocksLayoutFieldname].items, blockId)
: panelData[blocksLayoutFieldname].items,
},
[blocksFieldname]:
keys(panelData[blocksFieldname]).length > 2
? omit(panelData[blocksFieldname], [blockId])
: panelData[blocksFieldname],
};
};

0 comments on commit f99fd66

Please sign in to comment.