Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions redisinsight/ui/src/constants/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum BrowserStorageItem {
dbConfig = 'dbConfig_',
RunQueryMode = 'RunQueryMode',
wbCleanUp = 'wbCleanUp',
viewFormat = 'viewFormat',
}

export default BrowserStorageItem
Expand Down
14 changes: 9 additions & 5 deletions redisinsight/ui/src/slices/browser/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const initialState: KeysStore = {
error: '',
data: null,
length: 0,
viewFormat: defaultViewFormat,
viewFormat: localStorageService?.get(BrowserStorageItem.viewFormat) ?? defaultViewFormat,
},
addKey: {
loading: false,
Expand All @@ -86,6 +86,9 @@ export const initialKeyInfo = {
length: 0,
}

const getInitialSelectedKeyState = (state: KeysStore) =>
({ ...initialState.selectedKey, viewFormat: state.selectedKey.viewFormat })

// A slice for recipes
const keysSlice = createSlice({
name: 'keys',
Expand Down Expand Up @@ -213,7 +216,6 @@ const keysSlice = createSlice({
state.selectedKey = {
...state.selectedKey,
loading: false,
viewFormat: defaultViewFormat,
// data: null,
}
},
Expand Down Expand Up @@ -313,11 +315,13 @@ const keysSlice = createSlice({
},

resetKeyInfo: (state) => {
state.selectedKey = cloneDeep(initialState.selectedKey)
state.selectedKey = cloneDeep(getInitialSelectedKeyState(state as KeysStore))
},

// reset keys for keys slice
resetKeys: () => cloneDeep(initialState),
resetKeys: (state) => cloneDeep(
{ ...initialState, selectedKey: getInitialSelectedKeyState(state as KeysStore) }
),

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

setViewFormat: (state, { payload }: PayloadAction<KeyValueFormat>) => {
state.selectedKey.viewFormat = payload
localStorageService?.set(BrowserStorageItem.viewFormat, payload)
}
},
})
Expand Down Expand Up @@ -392,7 +397,6 @@ export let sourceKeysFetch: Nullable<CancelTokenSource> = null

export function setInitialStateByType(type: string) {
return (dispatch: AppDispatch) => {
dispatch(setViewFormat(defaultViewFormat))

if (type === KeyTypes.Hash) {
dispatch(setHashInitialState())
Expand Down
50 changes: 49 additions & 1 deletion redisinsight/ui/src/slices/tests/browser/keys.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cloneDeep } from 'lodash'
import { AxiosError } from 'axios'
import { KeyTypes } from 'uiSrc/constants'
import { KeyTypes, KeyValueFormat } from 'uiSrc/constants'
import { apiService } from 'uiSrc/services'
import { parseKeysListResponse, stringToBuffer } from 'uiSrc/utils'
import { cleanup, initialStateDefault, mockedStore } from 'uiSrc/utils/test-utils'
Expand Down Expand Up @@ -56,6 +56,8 @@ import reducer, {
addZsetKey,
setLastBatchKeys,
updateSelectedKeyRefreshTime,
resetKeyInfo,
resetKeys,
} from '../../browser/keys'
import { getString } from '../../browser/string'

Expand Down Expand Up @@ -780,6 +782,52 @@ describe('keys slice', () => {
})
})

describe('resetKeyInfo', () => {
it('should properly save viewFormat', () => {
// Arrange
const viewFormat = KeyValueFormat.HEX
const initialStateMock = {
...initialState,
selectedKey: {
...initialState.selectedKey,
viewFormat
}
}

// Act
const nextState = reducer(initialStateMock, resetKeyInfo())

// Assert
const rootState = Object.assign(initialStateDefault, {
browser: { keys: nextState },
})
expect(keysSelector(rootState)).toEqual(initialStateMock)
})
})

describe('resetKeys', () => {
it('should properly save viewFormat', () => {
// Arrange
const viewFormat = KeyValueFormat.HEX
const initialStateMock = {
...initialState,
selectedKey: {
...initialState.selectedKey,
viewFormat
}
}

// Act
const nextState = reducer(initialStateMock, resetKeys())

// Assert
const rootState = Object.assign(initialStateDefault, {
browser: { keys: nextState },
})
expect(keysSelector(rootState)).toEqual(initialStateMock)
})
})

describe('thunks', () => {
describe('fetchKeys', () => {
it('call both loadKeys and loadKeysSuccess when fetch is successed', async () => {
Expand Down