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

fix: fix types conflict between content_transformer and page_content #463

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/containers/Loadable/Loadable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import blockCn from 'bem-cn-lite';

import ErrorWrapper from '../../components/ErrorWrapper/ErrorWrapper';
import {
ConstructorItem,
ConstructorBlock,
CustomItem,
FetchLoadableData,
LoadableData,
Expand All @@ -27,7 +27,7 @@ export interface LoadableState {
export interface LoadableComponentsProps extends Omit<PropsWithChildren<LoadableProps>, 'source'> {
Component: CustomItem;
ChildComponent: CustomItem;
block: ConstructorItem;
block: ConstructorBlock;
blockKey: string;
fetch: FetchLoadableData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import _ from 'lodash';

import BlockBase from '../../../../components/BlockBase/BlockBase';
import {BlockDecoration} from '../../../../customization/BlockDecoration';
import {Block, BlockDecorationProps, WithChildren} from '../../../../models';
import {
BlockDecorationProps,
ConstructorBlock as ConstructorBlockType,
WithChildren,
} from '../../../../models';
import {block} from '../../../../utils';

interface ConstructorBlockProps extends Pick<BlockDecorationProps, 'index'> {
data: Block;
data: ConstructorBlockType;
}

const b = block('constructor-block');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ import _ from 'lodash';

import {InnerContext} from '../../../../context/innerContext';
import {BlockDecoration} from '../../../../customization/BlockDecoration';
import {Block, BlockType, ConstructorItem as ConstructorItemType} from '../../../../models';
import {
BlockType,
ConstructorBlock as ConstructorBlockType,
LoadableProps,
SubBlock,
} from '../../../../models';
import {getBlockKey} from '../../../../utils';
import {ConstructorBlock} from '../ConstructorBlock/ConstructorBlock';
import {ConstructorItem} from '../ConstructorItem';
import {ConstructorLoadable} from '../ConstructorLoadable';

export interface ConstructorBlocksProps {
items: ConstructorItemType[];
items: ConstructorBlockType[];
}

export const ConstructorBlocks: React.FC<ConstructorBlocksProps> = ({items}) => {
const {blockTypes, loadables, itemMap, shouldRenderBlock} = useContext(InnerContext);

const renderer = (
parentId = '',
item: ConstructorItemType,
item: ConstructorBlockType,
index: number,
): ReactElement | null => {
if (!itemMap[item.type]) {
Expand All @@ -38,7 +43,7 @@ export const ConstructorBlocks: React.FC<ConstructorBlocksProps> = ({items}) =>
}

if ('loadable' in item && item.loadable) {
const {source, serviceId, params} = item.loadable;
const {source, serviceId, params} = item.loadable as LoadableProps;
const config = _.get(loadables, source);

if (!config) {
Expand All @@ -58,7 +63,7 @@ export const ConstructorBlocks: React.FC<ConstructorBlocksProps> = ({items}) =>
} else {
let children;
if ('children' in item && item.children) {
children = item.children.map(renderer.bind(null, blockId));
children = (item.children as SubBlock[]).map(renderer.bind(null, blockId));
}

itemElement = (
Expand All @@ -71,7 +76,7 @@ export const ConstructorBlocks: React.FC<ConstructorBlocksProps> = ({items}) =>
return blockTypes.includes(item.type) ? (
//TODO: replace ConstructorBlock (and delete it) with BlockBase when all
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was it removed?

Copy link
Collaborator Author

@NikitaCG NikitaCG Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

// components relying on constructor inner structure like Slider or blog-constructor will be refactored
<ConstructorBlock data={item as Block} key={blockId} index={index}>
<ConstructorBlock data={item} key={blockId} index={index}>
{itemElement}
</ConstructorBlock>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import React, {useContext} from 'react';
import {BlockIdContext} from '../../../../context/blockIdContext';
import {InnerContext} from '../../../../context/innerContext';
import {BlockDecoration} from '../../../../customization/BlockDecoration';
import {BlockType, ConstructorItem as ConstructorItemType, WithChildren} from '../../../../models';
import {BlockType, ConstructorBlock, WithChildren} from '../../../../models';

export interface ConstructorItemProps {
data: ConstructorItemType;
data: ConstructorBlock;
blockKey: string;
}

Expand Down
4 changes: 2 additions & 2 deletions src/editor/components/BlockForm/BlockForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {DynamicField, SimpleVerticalAccordeon, Spec} from '@gravity-ui/dynamic-f
import _ from 'lodash';
import {Form as FinalForm, FormSpy} from 'react-final-form';

import {Block} from '../../../models';
import {Block, ConstructorBlock} from '../../../models';
import {dynamicConfig} from '../../dynamic-forms-custom/config';
import {CustomSpec} from '../../dynamic-forms-custom/parser/types';
import usePreviousValue from '../../hooks/usePreviousValue';

interface BlockFormProps {
data: Block;
data: ConstructorBlock;
spec: CustomSpec;
onChange: (data: Block) => void;
onSelect: () => void;
Expand Down
10 changes: 6 additions & 4 deletions src/editor/store/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Block, PageContent, Theme} from '../../models';
import {ConstructorBlock, PageContent, Theme} from '../../models';
import {ViewModeItem} from '../types';

import {addBlock, changeBlocksOrder, duplicateBlock, getErrorBoundaryState} from './utils';
Expand All @@ -19,7 +19,7 @@ interface OrderBlockParams {
}

interface AddBlockParams {
block: Block;
block: ConstructorBlock;
index: number;
}

Expand Down Expand Up @@ -87,7 +87,7 @@ export type EditorAction =
// reducer
export const reducer = (state: EditorState, action: EditorAction): EditorState => {
const {content} = state;
const getNewState = (blocks: Block[], activeBlockIndex: number) => ({
const getNewState = (blocks: ConstructorBlock[], activeBlockIndex: number) => ({
...state,
content: {
...content,
Expand All @@ -112,7 +112,9 @@ export const reducer = (state: EditorState, action: EditorAction): EditorState =
const blockId = action.payload;

return getNewState(
content.blocks.filter((_block: Block, index: number) => index !== blockId),
content.blocks.filter(
(_block: ConstructorBlock, index: number) => index !== blockId,
),
-1,
);
}
Expand Down
12 changes: 8 additions & 4 deletions src/editor/store/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import _ from 'lodash';

import {Block, PageContent} from '../../models';
import {ConstructorBlock, PageContent} from '../../models';

import {EditorBlockId} from './reducer';

export const changeBlocksOrder = (array: Block[], oldIndex: number, newIndex: number) => {
export const changeBlocksOrder = (
array: ConstructorBlock[],
oldIndex: number,
newIndex: number,
) => {
const result = [...array];
const element = result.splice(oldIndex, 1)[0];
result.splice(newIndex, 0, element);

return result;
};

export const duplicateBlock = (array: Block[], index: number) => {
export const duplicateBlock = (array: ConstructorBlock[], index: number) => {
const result = [...array];
result.splice(index + 1, 0, _.cloneDeep(result[index]));

Expand All @@ -28,7 +32,7 @@ export const getNewBlockIndex = (id: EditorBlockId, orderedBlocksCount: number)
return typeof id === 'string' ? 0 : id + 1;
};

export const addBlock = (array: Block[], block: Block, index: number) => {
export const addBlock = (array: ConstructorBlock[], block: ConstructorBlock, index: number) => {
const result = [...array];
result.splice(index, 0, block);

Expand Down
7 changes: 3 additions & 4 deletions src/models/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, {PropsWithChildren} from 'react';

import {
Animatable,
Block,
BlockDecorationProps,
ConstructorItem,
ThemedMediaProps,
Expand All @@ -17,8 +16,10 @@ export interface Menu {
title: string;
}

export type ConstructorBlock = ConstructorItem | CustomBlock;

export interface PageContent extends Animatable {
blocks: Block[];
blocks: ConstructorBlock[];
menu?: Menu;
background?: ThemedMediaProps;
}
Expand All @@ -33,8 +34,6 @@ export interface CustomBlock {
[key: string]: unknown;
}

export type ConstructorBlock = ConstructorItem | CustomBlock;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type LoadableData = any;
export type FetchLoadableDataParams = {
Expand Down
2 changes: 1 addition & 1 deletion src/models/customization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {PropsWithChildren} from 'react';
import {BlockBaseProps, BlockType} from './constructor-items';

export interface BlockDecorationProps extends PropsWithChildren, BlockBaseProps {
type: BlockType;
type: BlockType | string;
index?: number;
}
export type BlockDecorator = (props: BlockDecorationProps) => React.ReactElement;
7 changes: 3 additions & 4 deletions src/utils/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Block, CustomConfig, PCShareSocialNetwork, TextSize} from '../models';
import {ConstructorBlock} from '../models/constructor';
import {ConstructorBlock, CustomConfig, PCShareSocialNetwork, TextSize} from '../models';

const BLOCK_ELEMENTS = [
'div',
Expand Down Expand Up @@ -98,11 +97,11 @@ const getShareUrlWithParams = (url: string, params: Record<string, string | unde
return result.toString();
};

export const getOrderedBlocks = (blocks: Block[], headerBlockTypes: string[] = []) => {
export const getOrderedBlocks = (blocks: ConstructorBlock[], headerBlockTypes: string[] = []) => {
return blocks.filter(({type}) => !headerBlockTypes.includes(type));
};

export const getHeaderBlock = (blocks: Block[], headerBlockTypes: string[] = []) => {
export const getHeaderBlock = (blocks: ConstructorBlock[], headerBlockTypes: string[] = []) => {
return blocks.find(({type}) => headerBlockTypes.includes(type));
};

Expand Down
Loading