Skip to content

Commit 9e0dfb5

Browse files
authored
Merge pull request #1130 from RedisInsight/feature/RI-3417_save-selected-formatters
#RI-3417 - save selected formatter
2 parents b6c2587 + 8b8c4cf commit 9e0dfb5

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

redisinsight/ui/src/constants/storage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum BrowserStorageItem {
1717
dbConfig = 'dbConfig_',
1818
RunQueryMode = 'RunQueryMode',
1919
wbCleanUp = 'wbCleanUp',
20+
viewFormat = 'viewFormat',
2021
}
2122

2223
export default BrowserStorageItem

redisinsight/ui/src/slices/browser/keys.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const initialState: KeysStore = {
6969
error: '',
7070
data: null,
7171
length: 0,
72-
viewFormat: defaultViewFormat,
72+
viewFormat: localStorageService?.get(BrowserStorageItem.viewFormat) ?? defaultViewFormat,
7373
},
7474
addKey: {
7575
loading: false,
@@ -86,6 +86,9 @@ export const initialKeyInfo = {
8686
length: 0,
8787
}
8888

89+
const getInitialSelectedKeyState = (state: KeysStore) =>
90+
({ ...initialState.selectedKey, viewFormat: state.selectedKey.viewFormat })
91+
8992
// A slice for recipes
9093
const keysSlice = createSlice({
9194
name: 'keys',
@@ -213,7 +216,6 @@ const keysSlice = createSlice({
213216
state.selectedKey = {
214217
...state.selectedKey,
215218
loading: false,
216-
viewFormat: defaultViewFormat,
217219
// data: null,
218220
}
219221
},
@@ -313,11 +315,13 @@ const keysSlice = createSlice({
313315
},
314316

315317
resetKeyInfo: (state) => {
316-
state.selectedKey = cloneDeep(initialState.selectedKey)
318+
state.selectedKey = cloneDeep(getInitialSelectedKeyState(state as KeysStore))
317319
},
318320

319321
// reset keys for keys slice
320-
resetKeys: () => cloneDeep(initialState),
322+
resetKeys: (state) => cloneDeep(
323+
{ ...initialState, selectedKey: getInitialSelectedKeyState(state as KeysStore) }
324+
),
321325

322326
resetKeysData: (state) => {
323327
// state.data.keys = []
@@ -334,6 +338,7 @@ const keysSlice = createSlice({
334338

335339
setViewFormat: (state, { payload }: PayloadAction<KeyValueFormat>) => {
336340
state.selectedKey.viewFormat = payload
341+
localStorageService?.set(BrowserStorageItem.viewFormat, payload)
337342
}
338343
},
339344
})
@@ -392,7 +397,6 @@ export let sourceKeysFetch: Nullable<CancelTokenSource> = null
392397

393398
export function setInitialStateByType(type: string) {
394399
return (dispatch: AppDispatch) => {
395-
dispatch(setViewFormat(defaultViewFormat))
396400

397401
if (type === KeyTypes.Hash) {
398402
dispatch(setHashInitialState())

redisinsight/ui/src/slices/tests/browser/keys.spec.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { cloneDeep } from 'lodash'
22
import { AxiosError } from 'axios'
3-
import { KeyTypes } from 'uiSrc/constants'
3+
import { KeyTypes, KeyValueFormat } from 'uiSrc/constants'
44
import { apiService } from 'uiSrc/services'
55
import { parseKeysListResponse, stringToBuffer } from 'uiSrc/utils'
66
import { cleanup, initialStateDefault, mockedStore } from 'uiSrc/utils/test-utils'
@@ -56,6 +56,8 @@ import reducer, {
5656
addZsetKey,
5757
setLastBatchKeys,
5858
updateSelectedKeyRefreshTime,
59+
resetKeyInfo,
60+
resetKeys,
5961
} from '../../browser/keys'
6062
import { getString } from '../../browser/string'
6163

@@ -780,6 +782,52 @@ describe('keys slice', () => {
780782
})
781783
})
782784

785+
describe('resetKeyInfo', () => {
786+
it('should properly save viewFormat', () => {
787+
// Arrange
788+
const viewFormat = KeyValueFormat.HEX
789+
const initialStateMock = {
790+
...initialState,
791+
selectedKey: {
792+
...initialState.selectedKey,
793+
viewFormat
794+
}
795+
}
796+
797+
// Act
798+
const nextState = reducer(initialStateMock, resetKeyInfo())
799+
800+
// Assert
801+
const rootState = Object.assign(initialStateDefault, {
802+
browser: { keys: nextState },
803+
})
804+
expect(keysSelector(rootState)).toEqual(initialStateMock)
805+
})
806+
})
807+
808+
describe('resetKeys', () => {
809+
it('should properly save viewFormat', () => {
810+
// Arrange
811+
const viewFormat = KeyValueFormat.HEX
812+
const initialStateMock = {
813+
...initialState,
814+
selectedKey: {
815+
...initialState.selectedKey,
816+
viewFormat
817+
}
818+
}
819+
820+
// Act
821+
const nextState = reducer(initialStateMock, resetKeys())
822+
823+
// Assert
824+
const rootState = Object.assign(initialStateDefault, {
825+
browser: { keys: nextState },
826+
})
827+
expect(keysSelector(rootState)).toEqual(initialStateMock)
828+
})
829+
})
830+
783831
describe('thunks', () => {
784832
describe('fetchKeys', () => {
785833
it('call both loadKeys and loadKeysSuccess when fetch is successed', async () => {

0 commit comments

Comments
 (0)