feat(validator-management): move filtered validators into redux

This commit is contained in:
RadoslavDimchev 2024-04-10 10:42:06 +03:00 committed by Emil Ivanichkov
parent e073990a72
commit 962461792d
6 changed files with 30 additions and 37 deletions

View File

@ -2,7 +2,6 @@ import type { Meta, StoryObj } from '@storybook/react'
import { withRouter } from 'storybook-addon-react-router-v6' import { withRouter } from 'storybook-addon-react-router-v6'
import ManagementTable from './ManagementTable' import ManagementTable from './ManagementTable'
import { VALIDATORS_DATA } from '../../../constants'
const meta = { const meta = {
title: 'ValidatorManagement/ManagementTable', title: 'ValidatorManagement/ManagementTable',
@ -18,7 +17,5 @@ export default meta
type Story = StoryObj<typeof meta> type Story = StoryObj<typeof meta>
export const Default: Story = { export const Default: Story = {
args: { args: {},
filteredValidators: VALIDATORS_DATA,
},
} }

View File

@ -2,19 +2,14 @@ import { YStack } from 'tamagui'
import ManagementTableHeader from './ManagementTableHeader' import ManagementTableHeader from './ManagementTableHeader'
import ManagementTableBody from './ManagementTableBody' import ManagementTableBody from './ManagementTableBody'
import { Validator } from '../ManagementTabs'
import './ManagementTable.module.css' import './ManagementTable.module.css'
type ManagementTableProps = { const ManagementTable = () => {
filteredValidators: Validator[]
}
const ManagementTable = ({ filteredValidators }: ManagementTableProps) => {
return ( return (
<YStack> <YStack>
<table className="validator-management-table"> <table className="validator-management-table">
<ManagementTableHeader /> <ManagementTableHeader />
<ManagementTableBody filteredValidators={filteredValidators} /> <ManagementTableBody />
</table> </table>
</YStack> </YStack>
) )

View File

@ -2,7 +2,6 @@ import type { Meta, StoryObj } from '@storybook/react'
import { withRouter } from 'storybook-addon-react-router-v6' import { withRouter } from 'storybook-addon-react-router-v6'
import ManagementTableBody from './ManagementTableBody' import ManagementTableBody from './ManagementTableBody'
import { VALIDATORS_DATA } from '../../../constants'
const meta = { const meta = {
title: 'ValidatorManagement/ManagementTableBody', title: 'ValidatorManagement/ManagementTableBody',
@ -18,7 +17,5 @@ export default meta
type Story = StoryObj<typeof meta> type Story = StoryObj<typeof meta>
export const Default: Story = { export const Default: Story = {
args: { args: {},
filteredValidators: VALIDATORS_DATA,
},
} }

View File

@ -1,15 +1,14 @@
import { Text } from '@status-im/components' import { Text } from '@status-im/components'
import { useSelector } from 'react-redux'
import ManagementTableRow from './ManagementTableRow' import ManagementTableRow from './ManagementTableRow'
import { Validator } from '../ManagementTabs' import { RootState } from '../../../redux/store'
type ManagementTableBodyProps = { const ManagementTableBody = () => {
filteredValidators: Validator[] const { filteredValidators } = useSelector(
} (state: RootState) => state.validatorManagement,
)
const ManagementTableBody = ({
filteredValidators,
}: ManagementTableBodyProps) => {
return ( return (
<tbody> <tbody>
{filteredValidators.map(validator => ( {filteredValidators.map(validator => (

View File

@ -1,5 +1,5 @@
import { Tabs } from '@status-im/components' import { Tabs } from '@status-im/components'
import { useEffect, useMemo, useState } from 'react' import { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux' import { useDispatch, useSelector } from 'react-redux'
import { VALIDATOR_TABS_MANAGEMENT, VALIDATORS_DATA } from '../../constants' import { VALIDATOR_TABS_MANAGEMENT, VALIDATORS_DATA } from '../../constants'
@ -28,20 +28,13 @@ const isValidNameOrAddress = (
validatorName: string, validatorName: string,
validatorAddress: string, validatorAddress: string,
searchValue: string, searchValue: string,
) => { ) =>
if ( validatorName.includes(searchValue) || validatorAddress.includes(searchValue)
validatorName.includes(searchValue) ||
validatorAddress.includes(searchValue)
) {
return true
}
return false
}
const ManagementTabs = () => { const ManagementTabs = () => {
const [searchValue, setSearchValue] = useState('') const [searchValue, setSearchValue] = useState('')
const [validators, setValidators] = useState<Validator[]>([]) const [validators, setValidators] = useState<Validator[]>([])
const { currentTab } = useSelector( const { currentTab, filteredValidators } = useSelector(
(state: RootState) => state.validatorManagement, (state: RootState) => state.validatorManagement,
) )
const dispatch = useDispatch() const dispatch = useDispatch()
@ -50,12 +43,17 @@ const ManagementTabs = () => {
setValidators(VALIDATORS_DATA) setValidators(VALIDATORS_DATA)
}, []) }, [])
const filteredValidators = useMemo(() => { useEffect(() => {
return validators const newValidators = validators
.filter(validator => isValidStatus(validator.status, currentTab)) .filter(validator => isValidStatus(validator.status, currentTab))
.filter(validator => .filter(validator =>
isValidNameOrAddress(validator.name, validator.address, searchValue), isValidNameOrAddress(validator.name, validator.address, searchValue),
) )
dispatch({
type: 'validatorManagement/setFilteredValidators',
payload: newValidators,
})
}, [validators, searchValue, currentTab]) }, [validators, searchValue, currentTab])
const changeSearchValue = (value: string) => { const changeSearchValue = (value: string) => {
@ -96,7 +94,7 @@ const ManagementTabs = () => {
</div> </div>
{VALIDATOR_TABS_MANAGEMENT.map(tab => ( {VALIDATOR_TABS_MANAGEMENT.map(tab => (
<Tabs.Content key={tab} value={tab} style={{ marginTop: '8px' }}> <Tabs.Content key={tab} value={tab} style={{ marginTop: '8px' }}>
<ManagementTable filteredValidators={filteredValidators} /> <ManagementTable />
</Tabs.Content> </Tabs.Content>
))} ))}
</Tabs> </Tabs>

View File

@ -1,13 +1,16 @@
import { createSlice } from '@reduxjs/toolkit' import { createSlice } from '@reduxjs/toolkit'
import { VALIDATOR_TABS_MANAGEMENT } from '../../constants' import { VALIDATOR_TABS_MANAGEMENT } from '../../constants'
import { Validator } from '../../pages/ValidatorManagement/ManagementTabs'
type ValidatorManagementType = { type ValidatorManagementType = {
currentTab: string currentTab: string
filteredValidators: Validator[]
} }
const initialState: ValidatorManagementType = { const initialState: ValidatorManagementType = {
currentTab: VALIDATOR_TABS_MANAGEMENT[0], currentTab: VALIDATOR_TABS_MANAGEMENT[0],
filteredValidators: [],
} }
const ValidatorManagementSlice = createSlice({ const ValidatorManagementSlice = createSlice({
@ -17,9 +20,13 @@ const ValidatorManagementSlice = createSlice({
setCurrentTab: (state, action) => { setCurrentTab: (state, action) => {
state.currentTab = action.payload state.currentTab = action.payload
}, },
setFilteredValidators: (state, action) => {
state.filteredValidators = action.payload
},
}, },
}) })
export const { setCurrentTab } = ValidatorManagementSlice.actions export const { setCurrentTab, setFilteredValidators } =
ValidatorManagementSlice.actions
export default ValidatorManagementSlice.reducer export default ValidatorManagementSlice.reducer