Skip to content

Commit 5d308cd

Browse files
fix(Tablet): clear tablet data on unmount (#425)
1 parent 3eb21f3 commit 5d308cd

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

src/containers/Tablet/Tablet.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import cn from 'bem-cn-lite';
55
import {Link as ExternalLink} from '@gravity-ui/uikit';
66

77
import {backend} from '../../store';
8-
import {getTablet, getTabletDescribe} from '../../store/reducers/tablet';
8+
import {getTablet, getTabletDescribe, clearTabletData} from '../../store/reducers/tablet';
99
import {setHeader} from '../../store/reducers/header';
1010
import routes, {createHref} from '../../routes';
1111

@@ -49,6 +49,13 @@ export const Tablet = () => {
4949
error,
5050
} = useTypedSelector((state) => state.tablet);
5151

52+
// NOTE: should be reviewed when migrating to React 18
53+
useEffect(() => {
54+
return () => {
55+
dispatch(clearTabletData());
56+
};
57+
}, [dispatch]);
58+
5259
useEffect(() => {
5360
if (isFirstDataFetchRef.current && tablet && tablet.TenantId) {
5461
dispatch(getTabletDescribe(tablet.TenantId));

src/containers/Tablet/TabletInfo/TabletInfo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {b} from '../Tablet';
1212

1313
interface TabletInfoProps {
1414
tablet: TTabletStateInfo;
15-
tenantPath: string;
15+
tenantPath?: string;
1616
}
1717

1818
export const TabletInfo = ({tablet, tenantPath}: TabletInfoProps) => {
@@ -30,7 +30,7 @@ export const TabletInfo = ({tablet, tenantPath}: TabletInfoProps) => {
3030
const hasHiveId = HiveId && HiveId !== '0';
3131
const hasUptime = State === ETabletState.Active;
3232

33-
const tabletInfo: InfoViewerItem[] = [{label: 'Database', value: tenantPath}];
33+
const tabletInfo: InfoViewerItem[] = [{label: 'Database', value: tenantPath || '-'}];
3434

3535
if (hasHiveId) {
3636
tabletInfo.push({

src/store/reducers/tablet.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import {prepareNodesMap} from '../../utils/nodes';
1616
export const FETCH_TABLET = createRequestActionTypes('TABLET', 'FETCH_TABLET');
1717
export const FETCH_TABLET_DESCRIBE = createRequestActionTypes('TABLET', 'FETCH_TABLET_DESCRIBE');
1818

19+
const CLEAR_TABLET_DATA = 'tablet/CLEAR_TABLET_DATA';
20+
1921
const initialState = {
2022
loading: false,
21-
tenantPath: '-',
23+
tenantPath: undefined,
2224
};
2325

2426
const tablet: Reducer<ITabletState, ITabletAction> = (state = initialState, action) => {
@@ -57,6 +59,15 @@ const tablet: Reducer<ITabletState, ITabletAction> = (state = initialState, acti
5759
error: undefined,
5860
};
5961
}
62+
case CLEAR_TABLET_DATA: {
63+
return {
64+
...state,
65+
id: undefined,
66+
tenantPath: undefined,
67+
data: undefined,
68+
history: undefined,
69+
};
70+
}
6071
default:
6172
return state;
6273
}
@@ -122,4 +133,10 @@ export const getTabletDescribe = (tenantId: TDomainKey = {}) => {
122133
});
123134
};
124135

136+
export const clearTabletData = () => {
137+
return {
138+
type: CLEAR_TABLET_DATA,
139+
} as const;
140+
};
141+
125142
export default tablet;

src/types/store/tablet.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import type {ApiRequestAction} from '../../store/utils';
12
import type {IResponseError} from '../api/error';
23
import type {ETabletState, TTabletStateInfo} from '../api/tablet';
34

4-
import {FETCH_TABLET, FETCH_TABLET_DESCRIBE} from '../../store/reducers/tablet';
5-
import {ApiRequestAction} from '../../store/utils';
5+
import {FETCH_TABLET, FETCH_TABLET_DESCRIBE, clearTabletData} from '../../store/reducers/tablet';
66

77
export interface ITabletPreparedHistoryItem {
88
nodeId: string;
@@ -16,7 +16,7 @@ export interface ITabletPreparedHistoryItem {
1616

1717
export interface ITabletState {
1818
loading: boolean;
19-
tenantPath: string;
19+
tenantPath?: string;
2020
error?: IResponseError;
2121
id?: string;
2222
history?: ITabletPreparedHistoryItem[];
@@ -44,7 +44,10 @@ type ITabletDescribeApiRequestAction = ApiRequestAction<
4444
IResponseError
4545
>;
4646

47-
export type ITabletAction = ITabletApiRequestAction | ITabletDescribeApiRequestAction;
47+
export type ITabletAction =
48+
| ITabletApiRequestAction
49+
| ITabletDescribeApiRequestAction
50+
| ReturnType<typeof clearTabletData>;
4851

4952
export interface ITabletRootStateSlice {
5053
tablet: ITabletState;

0 commit comments

Comments
 (0)