Skip to content

Commit 81d1bd1

Browse files
committed
RI-6371 refactored code, added specs
1 parent 6b215bd commit 81d1bd1

File tree

7 files changed

+85
-23
lines changed

7 files changed

+85
-23
lines changed

redisinsight/ui/src/components/instance-header/components/instances-navigation-popover/InstancesNavigationPopover.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useHistory, useParams } from 'react-router-dom'
66
import { instancesSelector as rdiInstancesSelector } from 'uiSrc/slices/rdi/instances'
77
import { instancesSelector as dbInstancesSelector } from 'uiSrc/slices/instances/instances'
88
import Divider from 'uiSrc/components/divider/Divider'
9-
import { BrowserStorageItem, Pages } from 'uiSrc/constants'
9+
import { BrowserStorageItem, DEFAULT_SORT, Pages } from 'uiSrc/constants'
1010
import Down from 'uiSrc/assets/img/Down.svg?react'
1111
import Search from 'uiSrc/assets/img/Search.svg'
1212
import { Instance, RdiInstance } from 'uiSrc/slices/interfaces'
@@ -39,17 +39,11 @@ const InstancesNavigationPopover = ({ name }: Props) => {
3939
const history = useHistory()
4040

4141
useEffect(() => {
42-
const dbSort = localStorageService.get(BrowserStorageItem.instancesSorting) ?? {
43-
field: 'lastConnection',
44-
direction: 'asc'
45-
}
42+
const dbSort = localStorageService.get(BrowserStorageItem.instancesSorting) ?? DEFAULT_SORT
4643

4744
const dbFiltered = filterAndSort(dbInstances, searchFilter, dbSort)
4845

49-
const rdiSort = localStorageService.get(BrowserStorageItem.rdiInstancesSorting) ?? {
50-
field: 'lastConnection',
51-
direction: 'asc'
52-
}
46+
const rdiSort = localStorageService.get(BrowserStorageItem.rdiInstancesSorting) ?? DEFAULT_SORT
5347

5448
const rdiFiltered = filterAndSort(rdiInstances, searchFilter, rdiSort)
5549
setFilteredDbInstances(dbFiltered)

redisinsight/ui/src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ export * from './redisearch'
3333
export * from './browser/keyDetailsHeader'
3434
export * from './tutorials'
3535
export * from './datetime'
36+
export * from './sorting'
3637
export { ApiEndpoints, BrowserStorageItem, ApiStatusCode, apiErrors }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { PropertySort } from '@elastic/eui'
2+
3+
export const DEFAULT_SORT: PropertySort = {
4+
field: 'lastConnection',
5+
direction: 'asc'
6+
}

redisinsight/ui/src/pages/home/components/database-list-component/DatabasesListWrapper.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import RediStackLightLogo from 'uiSrc/assets/img/modules/redistack/RedisStackLog
2525
import CloudLinkIcon from 'uiSrc/assets/img/oauth/cloud_link.svg?react'
2626
import DatabaseListModules from 'uiSrc/components/database-list-modules/DatabaseListModules'
2727
import ItemList from 'uiSrc/components/item-list'
28-
import { BrowserStorageItem, FeatureFlags, Pages, Theme } from 'uiSrc/constants'
28+
import { BrowserStorageItem, DEFAULT_SORT, FeatureFlags, Pages, Theme } from 'uiSrc/constants'
2929
import { EXTERNAL_LINKS } from 'uiSrc/constants/links'
3030
import { ThemeContext } from 'uiSrc/contexts/themeContext'
3131
import PopoverDelete from 'uiSrc/pages/browser/components/popover-delete/PopoverDelete'
@@ -93,10 +93,7 @@ const DatabasesListWrapper = (props: Props) => {
9393
const [width, setWidth] = useState(0)
9494
const [, forceRerender] = useState({})
9595
const sortingRef = useRef<PropertySort>(
96-
localStorageService.get(BrowserStorageItem.instancesSorting) ?? {
97-
field: 'lastConnection',
98-
direction: 'asc'
99-
}
96+
localStorageService.get(BrowserStorageItem.instancesSorting) ?? DEFAULT_SORT
10097
)
10198

10299
const deletingIdRef = useRef('')

redisinsight/ui/src/pages/rdi/home/instance-list/RdiInstancesListWrapper.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useHistory, useLocation } from 'react-router-dom'
55

66
import cx from 'classnames'
77
import ItemList from 'uiSrc/components/item-list'
8-
import { BrowserStorageItem, Pages } from 'uiSrc/constants'
8+
import { BrowserStorageItem, DEFAULT_SORT, Pages } from 'uiSrc/constants'
99
import PopoverDelete from 'uiSrc/pages/browser/components/popover-delete/PopoverDelete'
1010
import { localStorageService } from 'uiSrc/services'
1111
import { Instance, RdiInstance } from 'uiSrc/slices/interfaces'
@@ -230,10 +230,7 @@ const RdiInstancesListWrapper = ({ width, onEditInstance, editedInstance, onDele
230230
}
231231
}
232232

233-
const sort: PropertySort = localStorageService.get(BrowserStorageItem.rdiInstancesSorting) ?? {
234-
field: 'lastConnection',
235-
direction: 'asc'
236-
}
233+
const sort: PropertySort = localStorageService.get(BrowserStorageItem.rdiInstancesSorting) ?? DEFAULT_SORT
237234

238235
return (
239236
<div className={styles.container}>

redisinsight/ui/src/utils/instance/instanceNavigation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { orderBy } from 'lodash'
2+
import { PropertySort } from '@elastic/eui'
23
import { Instance, RdiInstance } from 'uiSrc/slices/interfaces'
34
import { getDbIndex } from '../longNames'
45

56
export const filterAndSort = (
6-
arr: Instance[] | RdiInstance[],
7+
arr: Array<Instance | RdiInstance>,
78
search: string,
8-
sort: { field: string, direction: 'asc' | 'desc' }
9-
): (Instance | RdiInstance
10-
)[] => {
9+
sort: PropertySort
10+
): Array<Instance | RdiInstance> => {
1111
if (!arr?.length) return arr
1212
const filtered = arr.filter((instance) => {
1313
const label = `${instance.name} ${getDbIndex(instance.db)}`
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { filterAndSort } from 'uiSrc/utils'
2+
import { Instance, RdiInstance } from 'uiSrc/slices/interfaces'
3+
4+
const instances: Array<Instance | RdiInstance> = [
5+
{
6+
name: 'Instance12',
7+
db: 1,
8+
lastConnection: '2023-10-01',
9+
host: 'host4',
10+
port: 5432,
11+
},
12+
{
13+
name: 'Instance2',
14+
db: 2,
15+
lastConnection: '2023-10-02',
16+
host: 'host1',
17+
port: 5432,
18+
},
19+
{
20+
name: 'InstanceThree',
21+
db: 3,
22+
lastConnection: '2023-10-03',
23+
host: 'host2',
24+
port: 5433,
25+
},
26+
]
27+
28+
describe('filterAndSort', () => {
29+
it('should return an empty array if input array is empty', () => {
30+
expect(filterAndSort([], 'test', { field: 'name', direction: 'asc' })).toEqual([])
31+
})
32+
33+
it('should filter instances by name', () => {
34+
const result = filterAndSort(instances, 'instance2', { field: 'name', direction: 'asc' })
35+
expect(result).toEqual([instances[1]])
36+
})
37+
38+
it('should filter instances by db index', () => {
39+
const result = filterAndSort(instances, '3', { field: 'name', direction: 'asc' })
40+
expect(result).toEqual([instances[2]])
41+
})
42+
43+
it('should sort instances by lastConnection in ascending order', () => {
44+
const result = filterAndSort(instances, '', { field: 'lastConnection', direction: 'asc' })
45+
expect(result).toEqual([instances[2], instances[1], instances[0]])
46+
})
47+
48+
it('should sort instances by lastConnection in descending order', () => {
49+
const result = filterAndSort(instances, '', { field: 'lastConnection', direction: 'desc' })
50+
expect(result).toEqual([instances[0], instances[1], instances[2]])
51+
})
52+
53+
it('should sort instances by host in ascending order', () => {
54+
const result = filterAndSort(instances, '', { field: 'host', direction: 'asc' })
55+
expect(result).toEqual([instances[1], instances[2], instances[0]])
56+
})
57+
58+
it('should sort instances by host in descending order', () => {
59+
const result = filterAndSort(instances, '', { field: 'host', direction: 'desc' })
60+
expect(result).toEqual([instances[0], instances[2], instances[1]])
61+
})
62+
63+
it('should handle mixed filtering and sorting', () => {
64+
const result = filterAndSort(instances, '2', { field: 'lastConnection', direction: 'asc' })
65+
expect(result).toEqual([instances[1], instances[0]])
66+
})
67+
})

0 commit comments

Comments
 (0)