Skip to content

Commit baf552a

Browse files
fix(Describe): render loader on path change
1 parent 1146f13 commit baf552a

File tree

3 files changed

+94
-37
lines changed

3 files changed

+94
-37
lines changed

src/containers/Tenant/Diagnostics/Describe/Describe.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {Loader} from '@gravity-ui/uikit';
99

1010
import {prepareQueryError} from '../../../../utils/query';
1111
import {useAutofetcher, useTypedSelector} from '../../../../utils/hooks';
12-
import {getDescribe} from '../../../../store/reducers/describe';
12+
import {
13+
getDescribe,
14+
setDataWasNotLoaded,
15+
setCurrentDescribePath,
16+
} from '../../../../store/reducers/describe';
1317

1418
import './Describe.scss';
1519

@@ -30,11 +34,19 @@ const Describe = ({tenant}: IDescribeProps) => {
3034

3135
const {autorefresh, currentSchemaPath} = useTypedSelector((state) => state.schema);
3236

33-
const fetchData = useCallback(() => {
34-
const path = currentSchemaPath || tenant;
37+
const fetchData = useCallback(
38+
(isBackground: boolean) => {
39+
if (!isBackground) {
40+
dispatch(setDataWasNotLoaded());
41+
}
3542

36-
dispatch(getDescribe({path}));
37-
}, [currentSchemaPath, tenant, dispatch]);
43+
const path = currentSchemaPath || tenant;
44+
45+
dispatch(setCurrentDescribePath(path));
46+
dispatch(getDescribe({path}));
47+
},
48+
[currentSchemaPath, tenant, dispatch],
49+
);
3850

3951
useAutofetcher(fetchData, [fetchData], autorefresh);
4052

src/store/reducers/describe.ts

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@ import {createSelector, Selector} from 'reselect';
22
import {Reducer} from 'redux';
33

44
import '../../services/api';
5-
import {TEvDescribeSchemeResult} from '../../types/api/schema';
65
import {IConsumer} from '../../types/api/consumers';
7-
import {IDescribeRootStateSlice, IDescribeState} from '../../types/store/describe';
8-
import {IResponseError} from '../../types/api/error';
9-
import {createRequestActionTypes, createApiRequest, ApiRequestAction} from '../utils';
6+
import {IDescribeRootStateSlice, IDescribeState, IDescribeAction} from '../../types/store/describe';
7+
import {createRequestActionTypes, createApiRequest} from '../utils';
108

11-
const FETCH_DESCRIBE = createRequestActionTypes('describe', 'FETCH_DESCRIBE');
9+
export const FETCH_DESCRIBE = createRequestActionTypes('describe', 'FETCH_DESCRIBE');
10+
const SET_CURRENT_DESCRIBE_PATH = 'describe/SET_CURRENT_DESCRIBE_PATH';
11+
const SET_DATA_WAS_NOT_LOADED = 'describe/SET_DATA_WAS_NOT_LOADED';
1212

1313
const initialState = {
1414
loading: false,
1515
wasLoaded: false,
1616
data: {},
17+
currentDescribe: undefined,
18+
currentDescribePath: undefined,
1719
};
1820

19-
const describe: Reducer<
20-
IDescribeState,
21-
ApiRequestAction<typeof FETCH_DESCRIBE, TEvDescribeSchemeResult, IResponseError>
22-
> = (state = initialState, action) => {
21+
const describe: Reducer<IDescribeState, IDescribeAction> = (state = initialState, action) => {
2322
switch (action.type) {
2423
case FETCH_DESCRIBE.REQUEST: {
2524
return {
@@ -28,53 +27,79 @@ const describe: Reducer<
2827
};
2928
}
3029
case FETCH_DESCRIBE.SUCCESS: {
31-
let newData;
30+
const data = action.data;
3231

33-
if (action.data.Path) {
32+
const isCurrentDescribePath = data.Path === state.currentDescribePath;
33+
34+
let newData = state.data;
35+
36+
if (data.Path) {
3437
newData = JSON.parse(JSON.stringify(state.data));
35-
newData[action.data.Path] = action.data;
36-
} else {
37-
newData = state.data;
38+
newData[data.Path] = data;
39+
}
40+
41+
if (!isCurrentDescribePath) {
42+
return {
43+
...state,
44+
data: newData,
45+
};
3846
}
3947

4048
return {
4149
...state,
4250
data: newData,
43-
currentDescribe: action.data,
51+
currentDescribe: data,
4452
loading: false,
4553
wasLoaded: true,
4654
error: undefined,
4755
};
4856
}
57+
4958
case FETCH_DESCRIBE.FAILURE: {
5059
return {
5160
...state,
5261
error: action.error,
5362
loading: false,
54-
wasLoaded: true,
63+
};
64+
}
65+
case SET_CURRENT_DESCRIBE_PATH: {
66+
return {
67+
...state,
68+
currentDescribePath: action.data,
69+
};
70+
}
71+
case SET_DATA_WAS_NOT_LOADED: {
72+
return {
73+
...state,
74+
wasLoaded: false,
5575
};
5676
}
5777
default:
5878
return state;
5979
}
6080
};
6181

82+
export const setCurrentDescribePath = (path: string) => {
83+
return {
84+
type: SET_CURRENT_DESCRIBE_PATH,
85+
data: path,
86+
} as const;
87+
};
88+
89+
export const setDataWasNotLoaded = () => {
90+
return {
91+
type: SET_DATA_WAS_NOT_LOADED,
92+
} as const;
93+
};
94+
6295
// Consumers selectors
63-
const selectConsumersNames: Selector<IDescribeRootStateSlice, string[] | undefined, [string]> = (
64-
state,
65-
path,
66-
) => state.describe.data[path]?.PathDescription?.PersQueueGroup?.PQTabletConfig?.ReadRules;
67-
68-
export const selectConsumers: Selector<IDescribeRootStateSlice, IConsumer[]> = createSelector(
69-
selectConsumersNames,
70-
(names = []) => {
71-
const consumers = names.map((name) => {
72-
return {name};
73-
});
74-
75-
return consumers;
76-
},
77-
);
96+
const selectConsumersNames = (state: IDescribeRootStateSlice, path: string | undefined) =>
97+
path
98+
? state.describe.data[path]?.PathDescription?.PersQueueGroup?.PQTabletConfig?.ReadRules
99+
: undefined;
100+
101+
export const selectConsumers: Selector<IDescribeRootStateSlice, IConsumer[], [string | undefined]> =
102+
createSelector(selectConsumersNames, (names = []) => names.map((name) => ({name})));
78103

79104
export function getDescribe({path}: {path: string}) {
80105
return createApiRequest({

src/types/store/describe.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1+
import {
2+
FETCH_DESCRIBE,
3+
setCurrentDescribePath,
4+
setDataWasNotLoaded,
5+
} from '../../store/reducers/describe';
6+
import {ApiRequestAction} from '../../store/utils';
17
import {IResponseError} from '../api/error';
28
import {TEvDescribeSchemeResult} from '../api/schema';
39

10+
export type IDescribeData = Record<string, TEvDescribeSchemeResult>;
11+
412
export interface IDescribeState {
513
loading: boolean;
614
wasLoaded: boolean;
7-
data: Record<string, TEvDescribeSchemeResult>;
15+
data: IDescribeData;
816
currentDescribe?: TEvDescribeSchemeResult;
17+
currentDescribePath?: string;
918
error?: IResponseError;
1019
}
1120

21+
type IDescribeApiRequestAction = ApiRequestAction<
22+
typeof FETCH_DESCRIBE,
23+
TEvDescribeSchemeResult,
24+
IResponseError
25+
>;
26+
27+
export type IDescribeAction =
28+
| IDescribeApiRequestAction
29+
| ReturnType<typeof setCurrentDescribePath>
30+
| ReturnType<typeof setDataWasNotLoaded>;
31+
1232
export interface IDescribeRootStateSlice {
1333
describe: IDescribeState;
1434
}

0 commit comments

Comments
 (0)