Skip to content

Commit f73ccde

Browse files
authored
fix: constructor items inner data structure (#386)
1 parent 18df983 commit f73ccde

File tree

8 files changed

+33
-31
lines changed

8 files changed

+33
-31
lines changed

src/components/BlockBase/BlockBase.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import './BlockBase.scss';
1111
const b = block('block-base');
1212

1313
const BlockBase = (props: PropsWithChildren<BlockBaseProps & ClassNameProps>) => {
14-
const {anchor, visible, children, className, resetPaddings, qa} = props;
14+
const {anchor, visible, children, className, resetPaddings, qa, index = 0} = props;
1515

1616
return (
1717
<Col
@@ -20,7 +20,7 @@ const BlockBase = (props: PropsWithChildren<BlockBaseProps & ClassNameProps>) =>
2020
reset={true}
2121
dataQa={qa}
2222
>
23-
<BlockDecoration>
23+
<BlockDecoration id={index}>
2424
{anchor && <Anchor id={anchor.url} className={b('anchor')} />}
2525
{children}
2626
</BlockDecoration>

src/containers/PageConstructor/components/ConstructorBlock/ConstructorBlock.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import React from 'react';
22

33
import BlockBase from '../../../../components/BlockBase/BlockBase';
4-
import {Block, WithChildren} from '../../../../models';
4+
import {Block, BlockBaseProps, WithChildren} from '../../../../models';
55
import {block} from '../../../../utils';
66

7-
const b = block('constructor-block');
8-
interface ConstructorBlockProps {
7+
interface ConstructorBlockProps extends Pick<BlockBaseProps, 'index'> {
98
data: Block;
109
}
1110

11+
const b = block('constructor-block');
12+
1213
export const ConstructorBlock: React.FC<WithChildren<ConstructorBlockProps>> = ({
14+
index = 0,
1315
data,
1416
children,
1517
}) => {
1618
const {anchor, visible, type} = data;
1719

1820
return (
1921
<BlockBase
22+
index={index}
2023
className={b({type})}
2124
anchor={anchor}
2225
visible={visible}

src/containers/PageConstructor/components/ConstructorBlocks/ConstructorBlocks.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, {Fragment, ReactElement, useContext} from 'react';
22

33
import _ from 'lodash';
44

5-
import {BlockIdContext} from '../../../../context/blockIdContext';
65
import {InnerContext} from '../../../../context/innerContext';
76
import {Block, ConstructorItem as ConstructorItemType} from '../../../../models';
87
import {getBlockKey} from '../../../../utils';
@@ -58,20 +57,20 @@ export const ConstructorBlocks: React.FC<ConstructorBlocksProps> = ({items}) =>
5857
}
5958

6059
itemElement = (
61-
<ConstructorItem data={item} blockKey={blockId}>
60+
<ConstructorItem data={item} key={blockId} blockKey={blockId}>
6261
{children}
6362
</ConstructorItem>
6463
);
6564
}
6665

67-
return (
68-
<BlockIdContext.Provider value={blockId} key={blockId}>
69-
{blockTypes.includes(item.type) ? (
70-
<ConstructorBlock data={item as Block}>{itemElement}</ConstructorBlock>
71-
) : (
72-
itemElement
73-
)}
74-
</BlockIdContext.Provider>
66+
return blockTypes.includes(item.type) ? (
67+
//TODO: replace ConstructorBlock (and delete it) with BlockBase when all
68+
// components relying on constructor inner structure like Slider or blog-constructor will be refactored
69+
<ConstructorBlock data={item as Block} key={blockId} index={index}>
70+
{itemElement}
71+
</ConstructorBlock>
72+
) : (
73+
itemElement
7574
);
7675
};
7776

src/containers/PageConstructor/components/ConstructorItem/ConstructorItem.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, {useContext} from 'react';
22

3+
import {BlockIdContext} from '../../../../context/blockIdContext';
34
import {InnerContext} from '../../../../context/innerContext';
45
import {BlockDecoration} from '../../../../customization/BlockDecoration';
56
import {ConstructorItem as ConstructorItemType, WithChildren} from '../../../../models';
@@ -9,15 +10,19 @@ export interface ConstructorItemProps {
910
blockKey: string;
1011
}
1112

12-
export const ConstructorItem = ({data, children}: WithChildren<ConstructorItemProps>) => {
13+
export const ConstructorItem = ({data, blockKey, children}: WithChildren<ConstructorItemProps>) => {
1314
const {itemMap} = useContext(InnerContext);
1415
const {type, ...rest} = data;
1516

1617
const Component = itemMap[type] as React.ComponentType<
1718
React.ComponentProps<typeof itemMap[typeof type]>
1819
>;
1920

20-
return <Component {...rest}>{children}</Component>;
21+
return (
22+
<BlockIdContext.Provider value={blockKey}>
23+
<Component {...rest}>{children}</Component>
24+
</BlockIdContext.Provider>
25+
);
2126
};
2227

2328
export const ConstructorHeader = ({
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
import React, {Fragment, PropsWithChildren, useContext} from 'react';
22

3-
import {BlockIdContext} from '../context/blockIdContext';
43
import {InnerContext} from '../context/innerContext';
54
import {BlockDecorationProps} from '../models';
6-
import {getBlockIndexFromId} from '../utils';
75

8-
export const BlockDecoration = (props: PropsWithChildren<BlockDecorationProps>) => {
9-
const blockContenxtId = getBlockIndexFromId(useContext(BlockIdContext));
6+
export const BlockDecoration = ({
7+
id,
8+
children: blockChildren,
9+
}: PropsWithChildren<BlockDecorationProps>) => {
1010
const {headerBlockTypes} = useContext(InnerContext);
11-
const blockId = props.id || blockContenxtId;
12-
const isHeader = Boolean(props.id && headerBlockTypes.includes(props.id));
11+
const isHeader = Boolean(typeof id === 'string' && headerBlockTypes.includes(id));
1312

14-
const block = <Fragment>{props.children}</Fragment>;
13+
const block = <Fragment>{blockChildren}</Fragment>;
1514
const blockDecorators = useContext(InnerContext).customization?.decorators?.block;
1615

1716
if (!blockDecorators) {
1817
return block;
1918
}
2019

2120
return blockDecorators.reduce(
22-
(children, decorator) => (
23-
<Fragment>{decorator({children, id: blockId, isHeader})}</Fragment>
24-
),
21+
(children, decorator) => <Fragment>{decorator({children, id, isHeader})}</Fragment>,
2522
block,
2623
);
2724
};

src/models/constructor-items/blocks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface Childable {
6363

6464
//block props
6565
export interface BlockBaseProps {
66+
index?: number;
6667
anchor?: AnchorProps;
6768
visible?: GridColumnSize;
6869
resetPaddings?: boolean;

src/models/customization.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {PropsWithChildren} from 'react';
22

33
export interface BlockDecorationProps extends PropsWithChildren {
4-
id?: string;
4+
id: string | number;
55
}
66

77
export interface BlockDecoratorProps extends PropsWithChildren {

src/utils/blocks.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,3 @@ export const getShareLink = (
9696
return undefined;
9797
}
9898
};
99-
100-
export const getBlockIndexFromId = (blockId?: string) =>
101-
Number(blockId?.split('-')?.slice(-1).pop());

0 commit comments

Comments
 (0)