Skip to content

Commit e330919

Browse files
refactor: migrate node reducer to ts (#414)
1 parent ae95ecf commit e330919

File tree

8 files changed

+228
-162
lines changed

8 files changed

+228
-162
lines changed

src/containers/Node/Node.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import NodeStructure from './NodeStructure/NodeStructure';
1515
import {Loader} from '../../components/Loader';
1616
import {BasicNodeViewer} from '../../components/BasicNodeViewer';
1717

18-
import {getNodeInfo, resetNode} from '../../store/reducers/node';
18+
import {getNodeInfo, resetNode} from '../../store/reducers/node/node';
1919
import routes, {CLUSTER_PAGES, createHref} from '../../routes';
2020
import {setHeader} from '../../store/reducers/header';
2121
import {AutoFetcher} from '../../utils/autofetcher';

src/containers/Node/NodeStructure/NodeStructure.tsx

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import {useEffect, useRef, useMemo} from 'react';
2-
import {useDispatch, useSelector} from 'react-redux';
2+
import {useDispatch} from 'react-redux';
33
import url from 'url';
44
import _ from 'lodash';
55

66
import cn from 'bem-cn-lite';
77

8-
import {PDisk} from './Pdisk';
98
import {Loader} from '../.././../components/Loader';
109

11-
import {getNodeStructure, selectNodeStructure} from '../../../store/reducers/node';
10+
import {getNodeStructure} from '../../../store/reducers/node/node';
11+
import {selectNodeStructure} from '../../../store/reducers/node/selectors';
1212

1313
import {AutoFetcher} from '../../../utils/autofetcher';
14+
import {useTypedSelector} from '../../../utils/hooks';
15+
16+
import {PDisk} from './Pdisk';
1417

1518
import './NodeStructure.scss';
1619

@@ -32,20 +35,19 @@ interface NodeStructureProps {
3235

3336
const autofetcher = new AutoFetcher();
3437

35-
function NodeStructure(props: NodeStructureProps) {
38+
function NodeStructure({nodeId, className, additionalNodesInfo}: NodeStructureProps) {
3639
const dispatch = useDispatch();
3740

38-
const nodeStructure: any = useSelector(selectNodeStructure);
41+
const nodeStructure = useTypedSelector(selectNodeStructure);
3942

40-
const loadingStructure = useSelector((state: any) => state.node.loadingStructure);
41-
const wasLoadedStructure = useSelector((state: any) => state.node.wasLoadedStructure);
42-
const nodeData = useSelector((state: any) => state.node?.data?.SystemStateInfo?.[0]);
43+
const {loadingStructure, wasLoadedStructure} = useTypedSelector((state) => state.node);
44+
const nodeData = useTypedSelector((state) => state.node?.data?.SystemStateInfo?.[0]);
4345

4446
const nodeHref = useMemo(() => {
45-
return props.additionalNodesInfo?.getNodeRef
46-
? props.additionalNodesInfo.getNodeRef(nodeData)
47+
return additionalNodesInfo?.getNodeRef
48+
? additionalNodesInfo.getNodeRef(nodeData)
4749
: undefined;
48-
}, [nodeData, props.additionalNodesInfo]);
50+
}, [nodeData, additionalNodesInfo]);
4951

5052
const {pdiskId: pdiskIdFromUrl, vdiskId: vdiskIdFromUrl} = url.parse(
5153
window.location.href,
@@ -71,16 +73,16 @@ function NodeStructure(props: NodeStructureProps) {
7173
}, []);
7274

7375
useEffect(() => {
74-
dispatch(getNodeStructure(props.nodeId));
76+
dispatch(getNodeStructure(nodeId));
7577
autofetcher.start();
76-
autofetcher.fetch(() => dispatch(getNodeStructure(props.nodeId)));
78+
autofetcher.fetch(() => dispatch(getNodeStructure(nodeId)));
7779

7880
return () => {
7981
scrolled.current = false;
8082
isReady.current = false;
8183
autofetcher.stop();
8284
};
83-
}, [props.nodeId, dispatch]);
85+
}, [nodeId, dispatch]);
8486

8587
useEffect(() => {
8688
if (!_.isEmpty(nodeStructure) && scrollContainer) {
@@ -98,9 +100,9 @@ function NodeStructure(props: NodeStructureProps) {
98100

99101
if (vdiskIdFromUrl) {
100102
const vDisks = nodeStructure[pdiskIdFromUrl as string]?.vDisks;
101-
const vDisk = vDisks?.find((el: any) => el.id === vdiskIdFromUrl);
103+
const vDisk = vDisks?.find((el) => el.id === vdiskIdFromUrl);
102104
const dataTable = vDisk ? document.querySelector('.data-table') : undefined;
103-
const order = vDisk?.order;
105+
const order = vDisk?.order || 0;
104106

105107
if (dataTable) {
106108
scrollToVdisk += (dataTable as HTMLElement).offsetTop + 40 * order;
@@ -147,7 +149,7 @@ function NodeStructure(props: NodeStructureProps) {
147149

148150
return (
149151
<div className={b()} ref={scrollContainerRef}>
150-
<div className={props.className}>{renderContent()}</div>
152+
<div className={className}>{renderContent()}</div>
151153
</div>
152154
);
153155
}

src/services/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ export class YdbEmbeddedAPI extends AxiosWrapper {
9999
filter,
100100
nodeId,
101101
}: {
102-
tenant: string;
103-
filter: string;
102+
tenant?: string;
103+
filter?: string;
104104
nodeId: string;
105105
},
106106
{concurrentId}: AxiosOptions = {},

src/store/reducers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import cluster from './cluster/cluster';
55
import clusterNodes from './clusterNodes/clusterNodes';
66
import tenant from './tenant/tenant';
77
import storage from './storage';
8-
import node from './node';
8+
import node from './node/node';
99
import tooltip from './tooltip';
1010
import tablets from './tablets';
1111
import heatmap from './heatmap';

src/store/reducers/node.js

Lines changed: 0 additions & 141 deletions
This file was deleted.

src/store/reducers/node/node.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import {Reducer} from 'redux';
2+
3+
import '../../../services/api';
4+
import {createRequestActionTypes, createApiRequest} from '../../utils';
5+
6+
import type {NodeAction, NodeState} from './types';
7+
8+
export const FETCH_NODE = createRequestActionTypes('node', 'FETCH_NODE');
9+
export const FETCH_NODE_STRUCTURE = createRequestActionTypes('node', 'FETCH_NODE_STRUCTURE');
10+
11+
const RESET_NODE = 'node/RESET_NODE';
12+
13+
const initialState = {
14+
data: {},
15+
loading: true,
16+
wasLoaded: false,
17+
nodeStructure: {},
18+
loadingStructure: true,
19+
wasLoadedStructure: false,
20+
};
21+
22+
const node: Reducer<NodeState, NodeAction> = (state = initialState, action) => {
23+
switch (action.type) {
24+
case FETCH_NODE.REQUEST: {
25+
return {
26+
...state,
27+
loading: true,
28+
};
29+
}
30+
case FETCH_NODE.SUCCESS: {
31+
return {
32+
...state,
33+
data: action.data,
34+
loading: false,
35+
wasLoaded: true,
36+
error: undefined,
37+
};
38+
}
39+
case FETCH_NODE.FAILURE: {
40+
return {
41+
...state,
42+
error: action.error,
43+
loading: false,
44+
};
45+
}
46+
case FETCH_NODE_STRUCTURE.REQUEST: {
47+
return {
48+
...state,
49+
loadingStructure: true,
50+
};
51+
}
52+
case FETCH_NODE_STRUCTURE.SUCCESS: {
53+
return {
54+
...state,
55+
nodeStructure: action.data,
56+
loadingStructure: false,
57+
wasLoadedStructure: true,
58+
errorStructure: undefined,
59+
};
60+
}
61+
case FETCH_NODE_STRUCTURE.FAILURE: {
62+
return {
63+
...state,
64+
errorStructure: action.error,
65+
loadingStructure: false,
66+
};
67+
}
68+
case RESET_NODE: {
69+
return {
70+
...state,
71+
data: {},
72+
wasLoaded: false,
73+
nodeStructure: {},
74+
wasLoadedStructure: false,
75+
};
76+
}
77+
default:
78+
return state;
79+
}
80+
};
81+
82+
export const getNodeInfo = (id: string) => {
83+
return createApiRequest({
84+
request: window.api.getNodeInfo(id),
85+
actions: FETCH_NODE,
86+
});
87+
};
88+
89+
export const getNodeStructure = (nodeId: string) => {
90+
return createApiRequest({
91+
request: window.api.getStorageInfo({nodeId}, {concurrentId: 'getNodeStructure'}),
92+
actions: FETCH_NODE_STRUCTURE,
93+
});
94+
};
95+
96+
export function resetNode() {
97+
return {
98+
type: RESET_NODE,
99+
} as const;
100+
}
101+
102+
export default node;

0 commit comments

Comments
 (0)