Skip to content

907: Sort productBlock tree #1187

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

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ export const WfoSubscriptionDetailTree = ({
<EuiFlexItem grow={true}>
{!tree && <WfoLoading />}
{tree && (
<WfoTree data={[tree]} depthList={depthList} />
<WfoTree
treeBlocks={[tree]}
depthList={depthList}
/>
)}
</EuiFlexItem>
</EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import React, { FC, useEffect } from 'react';
import { TreeContext, TreeContextType } from '../../contexts';
import { TreeBlock } from '../../types';
import { WfoTreeBranch } from './WfoTreeBranch';
import { sortTreeBlockByLabel } from './treeUtils';

type WfoTreeProps = {
data: TreeBlock[];
treeBlocks: TreeBlock[];
depthList: number[];
};

export const WfoTree: FC<WfoTreeProps> = ({ data, depthList }) => {
export const WfoTree: FC<WfoTreeProps> = ({ treeBlocks, depthList }) => {
const { setDepths } = React.useContext(TreeContext) as TreeContextType;

useEffect(() => {
Expand All @@ -19,7 +20,7 @@ export const WfoTree: FC<WfoTreeProps> = ({ data, depthList }) => {

return (
<div style={{ width: '500px' }}>
{data.map((item) => (
{treeBlocks.sort(sortTreeBlockByLabel).map((item) => (
<WfoTreeBranch key={item.id} item={item} level={0} />
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { EuiListGroup } from '@elastic/eui';
import { TreeContext, TreeContextType } from '../../contexts';
import { TreeBlock } from '../../types';
import { WfoTreeNode } from './WfoTreeNode';
import { sortTreeBlockByLabel } from './treeUtils';

type WfoTreeBranchProps = {
item: TreeBlock;
Expand All @@ -21,9 +22,15 @@ export const WfoTreeBranch: FC<WfoTreeBranchProps> = ({ item, level }) => {
if (hasChildren) {
const newLevel = level + 1;

return item.children.map((child) => (
<WfoTreeBranch key={child.id} item={child} level={newLevel} />
));
return item.children
.sort(sortTreeBlockByLabel)
.map((child) => (
<WfoTreeBranch
key={child.id}
item={child}
level={newLevel}
/>
));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ export function getWfoTreeNodeDepth(
}
}
}

export const sortTreeBlockByLabel = (
{ label: labelA }: TreeBlock,
{ label: labelB }: TreeBlock,
): number => {
if (labelA < labelB) {
return -1;
}
if (labelA > labelB) {
return 1;
}
return 0;
};