Skip to content

Commit 8aa4dfa

Browse files
#RI-2450-fix pagination
1 parent 1239adc commit 8aa4dfa

File tree

7 files changed

+68
-21
lines changed

7 files changed

+68
-21
lines changed

redisinsight/ui/src/pages/workbench/components/enablement-area/EnablementArea/EnablementArea.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { EnablementAreaComponent, IEnablementAreaItem } from 'uiSrc/slices/inter
77
import { EnablementAreaProvider, IInternalPage } from 'uiSrc/pages/workbench/contexts/enablementAreaContext'
88
import { appContextWorkbenchEA, resetWorkbenchEAItem } from 'uiSrc/slices/app/context'
99
import { ApiEndpoints } from 'uiSrc/constants'
10+
import { getWBSourcePath } from './utils/getFileInfo'
1011
import {
1112
CodeButton,
1213
Group,
@@ -44,7 +45,6 @@ const EnablementArea = ({
4445
if (pagePath) {
4546
setIsInternalPageVisible(true)
4647
setInternalPage({ path: pagePath })
47-
4848
return
4949
}
5050
if (itemFromContext) {
@@ -140,6 +140,7 @@ const EnablementArea = ({
140140
onClose={handleCloseInternalPage}
141141
title={internalPage?.label}
142142
path={internalPage?.path}
143+
sourcePath={getWBSourcePath(internalPage?.path)}
143144
/>
144145
)}
145146
</div>

redisinsight/ui/src/pages/workbench/components/enablement-area/EnablementArea/components/InternalPage/InternalPage.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface Props {
3535
onScroll?: (top: number) => void;
3636
id: string;
3737
path: string;
38+
sourcePath: string;
3839
pagination?: IEnablementAreaItem[]
3940
}
4041
const InternalPage = (props: Props) => {
@@ -51,6 +52,7 @@ const InternalPage = (props: Props) => {
5152
pagination,
5253
id,
5354
path,
55+
sourcePath
5456
} = props
5557
const components: any = { LazyCodeButton, Image, Code }
5658
const containerRef = useRef<HTMLDivElement>(null)
@@ -146,10 +148,10 @@ const InternalPage = (props: Props) => {
146148
{!!pagination?.length && (
147149
<>
148150
<div className={cx(styles.footer, 'eui-showFor--xl')}>
149-
<Pagination items={pagination} activePageId={id} />
151+
<Pagination sourcePath={sourcePath} items={pagination} activePageId={id} />
150152
</div>
151153
<div className={cx(styles.footer, 'eui-hideFor--xl')}>
152-
<Pagination items={pagination} activePageId={id} compressed />
154+
<Pagination sourcePath={sourcePath} items={pagination} activePageId={id} compressed />
153155
</div>
154156
</>
155157
)}

redisinsight/ui/src/pages/workbench/components/enablement-area/EnablementArea/components/LazyInternalPage/LazyInternalPage.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState } from 'react'
1+
import React, { useEffect, useState, useCallback } from 'react'
22
import { startCase } from 'lodash'
33
import { useHistory } from 'react-router-dom'
44
import { useDispatch, useSelector } from 'react-redux'
@@ -7,6 +7,7 @@ import axios from 'axios'
77
import { getApiErrorMessage, isStatusSuccessful } from 'uiSrc/utils'
88
import { resourcesService } from 'uiSrc/services'
99
import { IS_ABSOLUTE_PATH } from 'uiSrc/constants/regex'
10+
import { ApiEndpoints } from 'uiSrc/constants'
1011
import {
1112
setWorkbenchEAItem,
1213
resetWorkbenchEAItem,
@@ -15,6 +16,7 @@ import {
1516
} from 'uiSrc/slices/app/context'
1617
import { IEnablementAreaItem } from 'uiSrc/slices/interfaces'
1718
import { workbenchGuidesSelector } from 'uiSrc/slices/workbench/wb-guides'
19+
import { workbenchTutorialsSelector } from 'uiSrc/slices/workbench/wb-tutorials'
1820

1921
import InternalPage from '../InternalPage'
2022
import { getFileInfo, getPagesInsideGroup, IFileInfo } from '../../utils/getFileInfo'
@@ -30,23 +32,38 @@ export interface Props {
3032
onClose: () => void;
3133
title?: string;
3234
path: string;
35+
sourcePath: string;
3336
}
3437

35-
const LazyInternalPage = ({ onClose, title, path }: Props) => {
38+
const LazyInternalPage = ({ onClose, title, path, sourcePath }: Props) => {
3639
const history = useHistory()
3740
const { itemScrollTop } = useSelector(appContextWorkbenchEA)
3841
const guides = useSelector(workbenchGuidesSelector)
42+
const tutorials = useSelector(workbenchTutorialsSelector)
3943
const [isLoading, setLoading] = useState<boolean>(false)
4044
const [error, setError] = useState<string>('')
4145
const [pageData, setPageData] = useState<IPageData>(DEFAULT_PAGE_DATA)
4246
const dispatch = useDispatch()
4347
const fetchService = IS_ABSOLUTE_PATH.test(path) ? axios : resourcesService
4448

49+
const getRelatedPages = useCallback((sourcePath: string): IEnablementAreaItem[] => {
50+
const pageInfo = getFileInfo(path)
51+
52+
switch (sourcePath) {
53+
case ApiEndpoints.GUIDES_PATH:
54+
return getPagesInsideGroup(guides.items, pageInfo.location)
55+
case ApiEndpoints.TUTORIALS_PATH:
56+
return getPagesInsideGroup(tutorials.items, pageInfo.location)
57+
default:
58+
return []
59+
}
60+
}, [sourcePath])
61+
4562
const loadContent = async () => {
4663
setLoading(true)
4764
setError('')
4865
const pageInfo = getFileInfo(path)
49-
const relatedPages = getPagesInsideGroup(guides.items, pageInfo.location)
66+
const relatedPages = getRelatedPages(sourcePath)
5067
setPageData({ ...DEFAULT_PAGE_DATA, ...pageInfo, relatedPages })
5168
try {
5269
const formatter = FormatSelector.selectFor(pageInfo.extension)
@@ -81,10 +98,11 @@ const LazyInternalPage = ({ onClose, title, path }: Props) => {
8198
<InternalPage
8299
id={pageData.name}
83100
path={path}
101+
sourcePath={sourcePath}
84102
onClose={onClose}
85103
title={startCase(title || pageData.name)}
86104
backTitle={startCase(pageData?.parent)}
87-
isLoading={isLoading || guides.loading}
105+
isLoading={isLoading || guides.loading || tutorials.loading}
88106
content={pageData.content}
89107
error={error}
90108
onScroll={handlePageScroll}

redisinsight/ui/src/pages/workbench/components/enablement-area/EnablementArea/components/Pagination/Pagination.spec.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react'
22
import { fireEvent, render, screen, waitFor } from 'uiSrc/utils/test-utils'
3-
import { MOCK_GUIDES_ITEMS } from 'uiSrc/constants'
3+
import { ApiEndpoints, MOCK_GUIDES_ITEMS } from 'uiSrc/constants'
44
import { defaultValue, EnablementAreaProvider } from 'uiSrc/pages/workbench/contexts/enablementAreaContext'
55
import Pagination from './Pagination'
66

77
const paginationItems = Object.values(MOCK_GUIDES_ITEMS['quick-guides']?.children || {})
88

99
describe('Pagination', () => {
1010
it('should render', () => {
11-
const component = render(<Pagination items={paginationItems} />)
11+
const component = render(<Pagination sourcePath={ApiEndpoints.GUIDES_PATH} items={paginationItems} />)
1212
const { queryByTestId } = component
1313

1414
expect(component).toBeTruthy()
@@ -17,7 +17,7 @@ describe('Pagination', () => {
1717
})
1818
it('should correctly open popover', () => {
1919
const { queryByTestId } = render(
20-
<Pagination items={paginationItems} activePageId={paginationItems[0].id} />
20+
<Pagination sourcePath={ApiEndpoints.GUIDES_PATH} items={paginationItems} activePageId={paginationItems[0].id} />
2121
)
2222
fireEvent.click(screen.getByTestId('enablement-area__pagination-popover-btn'))
2323
const popover = queryByTestId('enablement-area__pagination-popover')
@@ -32,31 +32,43 @@ describe('Pagination', () => {
3232

3333
render(
3434
<EnablementAreaProvider value={{ ...defaultValue, openPage }}>
35-
<Pagination items={paginationItems} activePageId={paginationItems[pageIndex].id} />
35+
<Pagination
36+
sourcePath={ApiEndpoints.GUIDES_PATH}
37+
items={paginationItems}
38+
activePageId={paginationItems[pageIndex].id}
39+
/>
3640
</EnablementAreaProvider>
3741
)
3842
fireEvent.click(screen.getByTestId('enablement-area__next-page-btn'))
3943

40-
expect(openPage).toBeCalledWith({ path: paginationItems[pageIndex + 1]?.args?.path })
44+
expect(openPage).toBeCalledWith({ path: ApiEndpoints.GUIDES_PATH + paginationItems[pageIndex + 1]?.args?.path })
4145
})
4246
it('should correctly open previous page', () => {
4347
const openPage = jest.fn()
4448
const pageIndex = 1
4549

4650
render(
4751
<EnablementAreaProvider value={{ ...defaultValue, openPage }}>
48-
<Pagination items={paginationItems} activePageId={paginationItems[pageIndex].id} />
52+
<Pagination
53+
sourcePath={ApiEndpoints.GUIDES_PATH}
54+
items={paginationItems}
55+
activePageId={paginationItems[pageIndex].id}
56+
/>
4957
</EnablementAreaProvider>
5058
)
5159
fireEvent.click(screen.getByTestId('enablement-area__prev-page-btn'))
5260

53-
expect(openPage).toBeCalledWith({ path: paginationItems[pageIndex - 1]?.args?.path })
61+
expect(openPage).toBeCalledWith({ path: ApiEndpoints.GUIDES_PATH + paginationItems[pageIndex - 1]?.args?.path })
5462
})
5563
it('should correctly open by using pagination popover', async () => {
5664
const openPage = jest.fn()
5765
const { queryByTestId } = render(
5866
<EnablementAreaProvider value={{ ...defaultValue, openPage }}>
59-
<Pagination items={paginationItems} activePageId={paginationItems[0].id} />
67+
<Pagination
68+
sourcePath={ApiEndpoints.GUIDES_PATH}
69+
items={paginationItems}
70+
activePageId={paginationItems[0].id}
71+
/>
6072
</EnablementAreaProvider>
6173
)
6274

@@ -70,6 +82,6 @@ describe('Pagination', () => {
7082

7183
expect(openPage).toBeCalledTimes(paginationItems.length - 1) // -1 because active item should not be clickable
7284
expect(openPage)
73-
.lastCalledWith({ path: paginationItems[paginationItems.length - 1]?.args?.path })
85+
.lastCalledWith({ path: ApiEndpoints.GUIDES_PATH + paginationItems[paginationItems.length - 1]?.args?.path })
7486
})
7587
})

redisinsight/ui/src/pages/workbench/components/enablement-area/EnablementArea/components/Pagination/Pagination.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import styles from './styles.module.scss'
1313

1414
export interface Props {
1515
items: IEnablementAreaItem[];
16+
sourcePath: string;
1617
activePageId?: string;
1718
compressed?: boolean;
1819
}
1920

20-
const Pagination = ({ items = [], activePageId, compressed }: Props) => {
21+
const Pagination = ({ items = [], sourcePath, activePageId, compressed }: Props) => {
2122
const [isPopoverOpen, setPopover] = useState(false)
2223
const [activePage, setActivePage] = useState(0)
2324
const { openPage } = useContext(EnablementAreaContext)
@@ -41,7 +42,7 @@ const Pagination = ({ items = [], activePageId, compressed }: Props) => {
4142
const path = items[index]?.args?.path
4243
closePopover()
4344
if (index !== activePage && openPage && path) {
44-
openPage({ path })
45+
openPage({ path: sourcePath + path })
4546
}
4647
}
4748

redisinsight/ui/src/pages/workbench/components/enablement-area/EnablementArea/utils/getFileInfo.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('getFileInfo', () => {
5252

5353
const getPagesInsideGroupTests = [
5454
{
55-
input: [MOCK_GUIDES_ITEMS, '/static/workbench/quick-guides'],
55+
input: [MOCK_GUIDES_ITEMS, '/static/guides/quick-guides'],
5656
expected: Object.values(MOCK_GUIDES_ITEMS['quick-guides'].children || {})
5757
},
5858
{

redisinsight/ui/src/pages/workbench/components/enablement-area/EnablementArea/utils/getFileInfo.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { get } from 'lodash'
2-
import { API_URL } from 'uiSrc/constants'
2+
import { API_URL, ApiEndpoints } from 'uiSrc/constants'
33
import { IS_ABSOLUTE_PATH } from 'uiSrc/constants/regex'
44
import { EnablementAreaComponent, IEnablementAreaItem } from 'uiSrc/slices/interfaces'
55

@@ -37,17 +37,20 @@ export const getFileInfo = (path: string): IFileInfo => {
3737

3838
const EA_STATIC_PATH_REGEX = /^\/?static\/(workbench|guides|tutorials)\//
3939
const EA_STATIC_ROOT_PATH = /^\/?static\/(workbench|guides|tutorials)\/?$/
40+
const EA_GUIDES_PATH = '/static/guides/'
41+
const EA_TUTORIALS_PATH = '/static/'
4042

4143
export const getPagesInsideGroup = (
4244
structure: Record<string, IEnablementAreaItem>,
4345
path: string
4446
): IEnablementAreaItem[] => {
4547
try {
4648
if (EA_STATIC_PATH_REGEX.test(path)) {
47-
let groupPath = path.replace(EA_STATIC_PATH_REGEX, '')
49+
let groupPath = path.replace(EA_GUIDES_PATH, '').replace(EA_TUTORIALS_PATH, '')
4850
let groupChildren
4951
if (!EA_STATIC_ROOT_PATH.test(path)) {
5052
groupPath = groupPath.replace('/', '.children.')
53+
// groupPath = 'tutorials.children.redis_stack'
5154
groupChildren = get(structure, groupPath, undefined)?.children
5255
} else {
5356
groupChildren = structure
@@ -62,3 +65,13 @@ export const getPagesInsideGroup = (
6265
return []
6366
}
6467
}
68+
69+
export const getWBSourcePath = (path: string): string => {
70+
if (path.includes(ApiEndpoints.TUTORIALS_PATH)) {
71+
return ApiEndpoints.TUTORIALS_PATH
72+
}
73+
if (path.includes(ApiEndpoints.GUIDES_PATH)) {
74+
return ApiEndpoints.GUIDES_PATH
75+
}
76+
return ''
77+
}

0 commit comments

Comments
 (0)