-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5497 from LiskHQ/5495-implement-api-loader
Implement API loader
- Loading branch information
Showing
14 changed files
with
178 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/modules/common/components/notification/indexingNotification/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { useEffect, useContext } from 'react'; | ||
import { toast } from 'react-toastify'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { ApplicationBootstrapContext } from '@setup/react/app/ApplicationBootstrap'; | ||
import styles from './styles.css'; | ||
|
||
const IndexingNotification = () => { | ||
const { t } = useTranslation(); | ||
const { | ||
indexStatus: { percentageIndexed, isIndexingInProgress, chainLength, numBlocksIndexed }, | ||
} = useContext(ApplicationBootstrapContext); | ||
|
||
useEffect(() => { | ||
if (chainLength - numBlocksIndexed >= 5) { | ||
toast.info( | ||
<div className={styles.indexingInfo}> | ||
<p>{t(`Blockchain client syncing: ${isIndexingInProgress}`)}</p> | ||
<p>{t(`Service indexing progress: ${percentageIndexed}%`)}</p> | ||
</div>, | ||
{ | ||
autoClose: false, | ||
draggable: false, | ||
closeButton: false, | ||
delay: 1000, | ||
toastId: 'indexStatus', | ||
} | ||
); | ||
} | ||
}, [percentageIndexed]); | ||
|
||
return null; | ||
}; | ||
|
||
export default IndexingNotification; |
59 changes: 59 additions & 0 deletions
59
src/modules/common/components/notification/indexingNotification/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React from 'react'; | ||
import { screen, waitFor } from '@testing-library/react'; | ||
import { ToastContainer } from 'react-toastify'; | ||
import { ApplicationBootstrapContext } from '@setup/react/app/ApplicationBootstrap'; | ||
import { smartRender } from 'src/utils/testHelpers'; | ||
import IndexingNotification from '.'; | ||
|
||
describe('IndexingNotification', () => { | ||
it('renders properly', async () => { | ||
const Component = () => ( | ||
<ApplicationBootstrapContext.Provider | ||
value={{ | ||
indexStatus: { | ||
percentageIndexed: 98.69, | ||
isIndexingInProgress: true, | ||
chainLength: 263945, | ||
numBlocksIndexed: 263938, | ||
}, | ||
}} | ||
> | ||
<ToastContainer /> | ||
<IndexingNotification /> | ||
</ApplicationBootstrapContext.Provider> | ||
); | ||
smartRender(Component); | ||
|
||
await waitFor( | ||
() => { | ||
expect(screen.getByText('Blockchain client syncing: true')).toBeVisible(); | ||
expect(screen.getByText('Service indexing progress: 98.69%')).toBeVisible(); | ||
}, | ||
{ timeout: 3000 } | ||
); | ||
}); | ||
|
||
it('does not render if indexing is not in progress', async () => { | ||
const Component = () => ( | ||
<ApplicationBootstrapContext.Provider | ||
value={{ | ||
indexStatus: { | ||
percentageIndexed: 100, | ||
isIndexingInProgress: false, | ||
chainLength: 263945, | ||
numBlocksIndexed: 263945, | ||
}, | ||
}} | ||
> | ||
<ToastContainer /> | ||
<IndexingNotification /> | ||
</ApplicationBootstrapContext.Provider> | ||
); | ||
smartRender(Component); | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByText('Blockchain client syncing: false')).not.toBeInTheDocument(); | ||
expect(screen.queryByText('Service indexing progress: 100%')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
src/modules/common/components/notification/indexingNotification/styles.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.indexingInfo { | ||
font-size: 16px; | ||
|
||
& p { | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './mockNetworkStatus'; | ||
export * from './mockNetworkStatistics'; | ||
export * from './mockPeers'; | ||
export * from './mockIndexStatus'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const mockIndexStatus = { | ||
data: { | ||
genesisHeight: 20449415, | ||
lastBlockHeight: 20689260, | ||
lastIndexedBlockHeight: 20689224, | ||
chainLength: 239846, | ||
numBlocksIndexed: 239810, | ||
percentageIndexed: 99.98, | ||
isIndexingInProgress: false, | ||
}, | ||
meta: { | ||
lastUpdate: 1700834626, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { INDEX_STATUS } from 'src/const/queries'; | ||
import { API_VERSION } from 'src/const/config'; | ||
import { useCustomQuery } from 'src/modules/common/hooks'; | ||
|
||
export const useIndexStatus = ({ config: customConfig = {}, options, client } = {}) => { | ||
const config = { | ||
url: `/api/${API_VERSION}/index/status`, | ||
method: 'get', | ||
event: 'get.index.status', | ||
...customConfig, | ||
}; | ||
return useCustomQuery({ | ||
keys: [INDEX_STATUS], | ||
config, | ||
options, | ||
client, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { mockIndexStatus } from '@network/__fixtures__'; | ||
import { queryWrapper as wrapper } from 'src/utils/test/queryWrapper'; | ||
import { useIndexStatus } from '.'; | ||
|
||
jest.useRealTimers(); | ||
|
||
describe('useIndexStatus hook', () => { | ||
it('fetching data correctly', async () => { | ||
const { result, waitFor } = renderHook(() => useIndexStatus(), { wrapper }); | ||
expect(result.current.isLoading).toBeTruthy(); | ||
await waitFor(() => result.current.isFetched); | ||
expect(result.current.isSuccess).toBeTruthy(); | ||
expect(result.current.data).toEqual(mockIndexStatus); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters