Skip to content

Commit f68c3dc

Browse files
RI-7160 Stop fetching key details for key that was opened in another … (#4648)
* RI-7160 Stop fetching key details for key that was opened in another database after switch RI-7132 fix duplicated calls on refresh on the details view for a json key * Update redisinsight/ui/src/pages/browser/modules/key-details/components/change-editor-type-button/useChangeEditorType.tsx Co-authored-by: Pavel Angelov <[email protected]> --------- Co-authored-by: Pavel Angelov <[email protected]>
1 parent 788ab7a commit f68c3dc

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

redisinsight/ui/src/pages/browser/modules/key-details/components/change-editor-type-button/useChangeEditorType.spec.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ import * as reactRedux from 'react-redux'
22
import { renderHook, act } from '@testing-library/react-hooks'
33
import { EditorType } from 'uiSrc/slices/interfaces'
44
import { FeatureFlags } from 'uiSrc/constants'
5-
5+
import { stringToBuffer } from 'uiSrc/utils'
66
import { useChangeEditorType } from './useChangeEditorType'
77

88
jest.mock('react-redux', () => ({
99
useDispatch: jest.fn(),
1010
useSelector: jest.fn(),
1111
}))
1212

13+
jest.mock('uiSrc/slices/browser/rejson', () => ({
14+
...jest.requireActual('uiSrc/slices/browser/rejson'),
15+
fetchReJSON: jest.fn((key) => ({ type: 'FETCH_REJSON', payload: key })),
16+
}))
17+
1318
const mockedUseDispatch = reactRedux.useDispatch as jest.Mock
1419
const mockedUseSelector = reactRedux.useSelector as jest.Mock
20+
const mockKeyName = stringToBuffer('test-key')
1521

1622
describe('useChangeEditorType', () => {
1723
const dispatchMock = jest.fn()
@@ -59,6 +65,50 @@ describe('useChangeEditorType', () => {
5965
})
6066
})
6167

68+
it('should fetch json when type switched', async () => {
69+
mockedUseSelector.mockReturnValue({
70+
editorType: EditorType.Default,
71+
}).mockReturnValue({
72+
[FeatureFlags.envDependent]: { flag: false },
73+
}).mockReturnValue({
74+
name: mockKeyName,
75+
})
76+
77+
const { result } = renderHook(() => useChangeEditorType())
78+
79+
act(() => {
80+
result.current.switchEditorType()
81+
})
82+
83+
expect(dispatchMock).toHaveBeenCalledWith({
84+
type: 'rejson/setEditorType',
85+
payload: EditorType.Default,
86+
})
87+
expect(dispatchMock).toHaveBeenCalledWith({
88+
type: 'FETCH_REJSON',
89+
payload: mockKeyName,
90+
})
91+
})
92+
93+
it('should not fetch json when there is no selected key', () => {
94+
mockedUseSelector.mockReturnValue({
95+
editorType: EditorType.Default,
96+
}).mockReturnValue({
97+
[FeatureFlags.envDependent]: { flag: false },
98+
})
99+
100+
const { result } = renderHook(() => useChangeEditorType())
101+
102+
act(() => {
103+
result.current.switchEditorType()
104+
})
105+
106+
expect(dispatchMock).not.toHaveBeenCalledWith({
107+
type: 'FETCH_REJSON',
108+
payload: expect.anything(),
109+
})
110+
})
111+
62112
describe('isTextEditorDisabled', () => {
63113
it('should be false when isWithinThreshold is true', () => {
64114
mockedUseSelector

redisinsight/ui/src/pages/browser/modules/key-details/components/change-editor-type-button/useChangeEditorType.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,32 @@ import { useCallback } from 'react'
22
import { useSelector, useDispatch } from 'react-redux'
33
import { FeatureFlags } from 'uiSrc/constants'
44
import { appFeatureFlagsFeaturesSelector } from 'uiSrc/slices/app/features'
5-
import { rejsonSelector, setEditorType } from 'uiSrc/slices/browser/rejson'
5+
import {
6+
fetchReJSON,
7+
rejsonSelector,
8+
setEditorType,
9+
} from 'uiSrc/slices/browser/rejson'
610
import { EditorType } from 'uiSrc/slices/interfaces'
11+
import { selectedKeyDataSelector } from 'uiSrc/slices/browser/keys'
712

813
export const useChangeEditorType = () => {
914
const dispatch = useDispatch()
1015
const { editorType, isWithinThreshold } = useSelector(rejsonSelector)
1116
const { [FeatureFlags.envDependent]: envDependentFeature } = useSelector(
1217
appFeatureFlagsFeaturesSelector,
1318
)
19+
const selectedKey = useSelector(selectedKeyDataSelector)?.name
1420

1521
const isTextEditorDisabled = !isWithinThreshold && !envDependentFeature?.flag
1622

1723
const switchEditorType = useCallback(() => {
1824
const opposite =
1925
editorType === EditorType.Default ? EditorType.Text : EditorType.Default
2026
dispatch(setEditorType(opposite))
27+
28+
if (selectedKey) {
29+
dispatch(fetchReJSON(selectedKey))
30+
}
2131
}, [dispatch, editorType])
2232

2333
return { switchEditorType, editorType, isTextEditorDisabled }

redisinsight/ui/src/pages/browser/modules/key-details/components/rejson-details/RejsonDetailsWrapper.spec.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react'
22
import { useDispatch, useSelector } from 'react-redux'
33
import { instance, mock } from 'ts-mockito'
44
import { render } from 'uiSrc/utils/test-utils'
5-
import { fetchReJSON } from 'uiSrc/slices/browser/rejson'
65
import { EditorType } from 'uiSrc/slices/interfaces'
76
import { stringToBuffer } from 'uiSrc/utils'
87

@@ -66,7 +65,7 @@ describe('RejsonDetailsWrapper', () => {
6665
).toBeTruthy()
6766
})
6867

69-
it('should dispatch fetchReJSON when editorType changes', () => {
68+
it('should not dispatch fetchReJSON on init', () => {
7069
let editorType = EditorType.Default
7170

7271
mockUseSelector.mockImplementation((selector) => {
@@ -83,7 +82,9 @@ describe('RejsonDetailsWrapper', () => {
8382
editorType = EditorType.Text
8483
rerender(<RejsonDetailsWrapper {...instance(mockedProps)} />)
8584

86-
const expectedKey = stringToBuffer('test-key')
87-
expect(mockDispatch).toHaveBeenCalledWith(fetchReJSON(expectedKey))
85+
expect(mockDispatch).not.toHaveBeenCalledWith({
86+
type: 'FETCH_REJSON',
87+
payload: expect.anything(),
88+
})
8889
})
8990
})

redisinsight/ui/src/pages/browser/modules/key-details/components/rejson-details/RejsonDetailsWrapper.tsx

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React, { useEffect, useState } from 'react'
2-
import { useDispatch, useSelector } from 'react-redux'
2+
import { useSelector } from 'react-redux'
33
import { EuiProgress } from '@elastic/eui'
44
import { isUndefined } from 'lodash'
55

66
import {
7-
fetchReJSON,
87
rejsonDataSelector,
98
rejsonSelector,
109
} from 'uiSrc/slices/browser/rejson'
@@ -36,7 +35,6 @@ export interface Props extends KeyDetailsHeaderProps {}
3635
const RejsonDetailsWrapper = (props: Props) => {
3736
const { loading, editorType } = useSelector(rejsonSelector)
3837
const { data, downloaded, type, path } = useSelector(rejsonDataSelector)
39-
const dispatch = useDispatch()
4038

4139
const {
4240
name: selectedKey,
@@ -54,20 +52,6 @@ const RejsonDetailsWrapper = (props: Props) => {
5452
setExpandedRows(new Set())
5553
}, [nameString])
5654

57-
// TODO: the whole workflow should be refactored
58-
// in a way that this component will not be responsible for fetching data
59-
// based on the editor type
60-
useEffect(() => {
61-
if (!selectedKey) return
62-
63-
// Not including `loading` in deps is intentional
64-
// This check avoids double fetching of data
65-
// which happens when new key is selected for example.
66-
if (loading) return
67-
68-
dispatch(fetchReJSON(selectedKey))
69-
}, [editorType, selectedKey, dispatch])
70-
7155
const reportJSONKeyCollapsed = (level: number) => {
7256
sendEventTelemetry({
7357
event: getBasedOnViewTypeEvent(

0 commit comments

Comments
 (0)