From 962461792ddccf7185a1cc15d9e4468abbe86c35 Mon Sep 17 00:00:00 2001 From: RadoslavDimchev Date: Wed, 10 Apr 2024 10:42:06 +0300 Subject: [PATCH] feat(validator-management): move filtered validators into redux --- .../ManagementTable.stories.tsx | 5 +--- .../ManagementTable/ManagementTable.tsx | 9 ++----- .../ManagementTableBody.stories.tsx | 5 +--- .../ManagementTable/ManagementTableBody.tsx | 13 +++++----- .../ValidatorManagement/ManagementTabs.tsx | 26 +++++++++---------- src/redux/ValidatorManagement/slice.ts | 9 ++++++- 6 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/pages/ValidatorManagement/ManagementTable/ManagementTable.stories.tsx b/src/pages/ValidatorManagement/ManagementTable/ManagementTable.stories.tsx index 9ab3ea40..93342b70 100644 --- a/src/pages/ValidatorManagement/ManagementTable/ManagementTable.stories.tsx +++ b/src/pages/ValidatorManagement/ManagementTable/ManagementTable.stories.tsx @@ -2,7 +2,6 @@ import type { Meta, StoryObj } from '@storybook/react' import { withRouter } from 'storybook-addon-react-router-v6' import ManagementTable from './ManagementTable' -import { VALIDATORS_DATA } from '../../../constants' const meta = { title: 'ValidatorManagement/ManagementTable', @@ -18,7 +17,5 @@ export default meta type Story = StoryObj export const Default: Story = { - args: { - filteredValidators: VALIDATORS_DATA, - }, + args: {}, } diff --git a/src/pages/ValidatorManagement/ManagementTable/ManagementTable.tsx b/src/pages/ValidatorManagement/ManagementTable/ManagementTable.tsx index d9d32731..16ac054e 100644 --- a/src/pages/ValidatorManagement/ManagementTable/ManagementTable.tsx +++ b/src/pages/ValidatorManagement/ManagementTable/ManagementTable.tsx @@ -2,19 +2,14 @@ import { YStack } from 'tamagui' import ManagementTableHeader from './ManagementTableHeader' import ManagementTableBody from './ManagementTableBody' -import { Validator } from '../ManagementTabs' import './ManagementTable.module.css' -type ManagementTableProps = { - filteredValidators: Validator[] -} - -const ManagementTable = ({ filteredValidators }: ManagementTableProps) => { +const ManagementTable = () => { return ( - +
) diff --git a/src/pages/ValidatorManagement/ManagementTable/ManagementTableBody.stories.tsx b/src/pages/ValidatorManagement/ManagementTable/ManagementTableBody.stories.tsx index 4cfbc863..c957fee9 100644 --- a/src/pages/ValidatorManagement/ManagementTable/ManagementTableBody.stories.tsx +++ b/src/pages/ValidatorManagement/ManagementTable/ManagementTableBody.stories.tsx @@ -2,7 +2,6 @@ import type { Meta, StoryObj } from '@storybook/react' import { withRouter } from 'storybook-addon-react-router-v6' import ManagementTableBody from './ManagementTableBody' -import { VALIDATORS_DATA } from '../../../constants' const meta = { title: 'ValidatorManagement/ManagementTableBody', @@ -18,7 +17,5 @@ export default meta type Story = StoryObj export const Default: Story = { - args: { - filteredValidators: VALIDATORS_DATA, - }, + args: {}, } diff --git a/src/pages/ValidatorManagement/ManagementTable/ManagementTableBody.tsx b/src/pages/ValidatorManagement/ManagementTable/ManagementTableBody.tsx index 872852ca..0d5aee9d 100644 --- a/src/pages/ValidatorManagement/ManagementTable/ManagementTableBody.tsx +++ b/src/pages/ValidatorManagement/ManagementTable/ManagementTableBody.tsx @@ -1,15 +1,14 @@ import { Text } from '@status-im/components' +import { useSelector } from 'react-redux' import ManagementTableRow from './ManagementTableRow' -import { Validator } from '../ManagementTabs' +import { RootState } from '../../../redux/store' -type ManagementTableBodyProps = { - filteredValidators: Validator[] -} +const ManagementTableBody = () => { + const { filteredValidators } = useSelector( + (state: RootState) => state.validatorManagement, + ) -const ManagementTableBody = ({ - filteredValidators, -}: ManagementTableBodyProps) => { return ( {filteredValidators.map(validator => ( diff --git a/src/pages/ValidatorManagement/ManagementTabs.tsx b/src/pages/ValidatorManagement/ManagementTabs.tsx index 62fc6b02..9e601f86 100644 --- a/src/pages/ValidatorManagement/ManagementTabs.tsx +++ b/src/pages/ValidatorManagement/ManagementTabs.tsx @@ -1,5 +1,5 @@ import { Tabs } from '@status-im/components' -import { useEffect, useMemo, useState } from 'react' +import { useEffect, useState } from 'react' import { useDispatch, useSelector } from 'react-redux' import { VALIDATOR_TABS_MANAGEMENT, VALIDATORS_DATA } from '../../constants' @@ -28,20 +28,13 @@ const isValidNameOrAddress = ( validatorName: string, validatorAddress: string, searchValue: string, -) => { - if ( - validatorName.includes(searchValue) || - validatorAddress.includes(searchValue) - ) { - return true - } - return false -} +) => + validatorName.includes(searchValue) || validatorAddress.includes(searchValue) const ManagementTabs = () => { const [searchValue, setSearchValue] = useState('') const [validators, setValidators] = useState([]) - const { currentTab } = useSelector( + const { currentTab, filteredValidators } = useSelector( (state: RootState) => state.validatorManagement, ) const dispatch = useDispatch() @@ -50,12 +43,17 @@ const ManagementTabs = () => { setValidators(VALIDATORS_DATA) }, []) - const filteredValidators = useMemo(() => { - return validators + useEffect(() => { + const newValidators = validators .filter(validator => isValidStatus(validator.status, currentTab)) .filter(validator => isValidNameOrAddress(validator.name, validator.address, searchValue), ) + + dispatch({ + type: 'validatorManagement/setFilteredValidators', + payload: newValidators, + }) }, [validators, searchValue, currentTab]) const changeSearchValue = (value: string) => { @@ -96,7 +94,7 @@ const ManagementTabs = () => { {VALIDATOR_TABS_MANAGEMENT.map(tab => ( - + ))} diff --git a/src/redux/ValidatorManagement/slice.ts b/src/redux/ValidatorManagement/slice.ts index 524ed0d1..a5e31184 100644 --- a/src/redux/ValidatorManagement/slice.ts +++ b/src/redux/ValidatorManagement/slice.ts @@ -1,13 +1,16 @@ import { createSlice } from '@reduxjs/toolkit' import { VALIDATOR_TABS_MANAGEMENT } from '../../constants' +import { Validator } from '../../pages/ValidatorManagement/ManagementTabs' type ValidatorManagementType = { currentTab: string + filteredValidators: Validator[] } const initialState: ValidatorManagementType = { currentTab: VALIDATOR_TABS_MANAGEMENT[0], + filteredValidators: [], } const ValidatorManagementSlice = createSlice({ @@ -17,9 +20,13 @@ const ValidatorManagementSlice = createSlice({ setCurrentTab: (state, action) => { 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