feat(management): move validators and filtering

This commit is contained in:
RadoslavDimchev 2024-04-09 17:18:56 +03:00 committed by Emil Ivanichkov
parent 24b958bc68
commit d52652ecb7
4 changed files with 60 additions and 61 deletions

View File

@ -1,6 +1,7 @@
import type { Meta, StoryObj } from '@storybook/react' import type { Meta, StoryObj } from '@storybook/react'
import ManagementTable from './ManagementTable' import ManagementTable from './ManagementTable'
import { VALIDATORS_DATA } from '../../../constants'
const meta = { const meta = {
title: 'ValidatorManagement/ManagementTable', title: 'ValidatorManagement/ManagementTable',
@ -16,7 +17,6 @@ type Story = StoryObj<typeof meta>
export const Default: Story = { export const Default: Story = {
args: { args: {
searchValue: '', filteredValidators: VALIDATORS_DATA,
tab: 'Active',
}, },
} }

View File

@ -1,67 +1,15 @@
import { useEffect, useMemo, useState } from 'react'
import { YStack } from 'tamagui' import { YStack } from 'tamagui'
import { VALIDATORS_DATA, VALIDATOR_TABS_MANAGEMENT } from '../../../constants'
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 = { type ManagementTableProps = {
tab: string filteredValidators: Validator[]
searchValue: string
} }
export type Validator = { const ManagementTable = ({ filteredValidators }: ManagementTableProps) => {
name: string
address: string
balance: number
income: number
proposals: string
attestations: string
effectiveness: number
status: string
}
const isValidStatus = (validatorStatus: string, tabStatus: string) => {
if (
validatorStatus === tabStatus ||
tabStatus ===
VALIDATOR_TABS_MANAGEMENT[VALIDATOR_TABS_MANAGEMENT.length - 1]
) {
return true
}
return false
}
const isValidNameOrAddress = (
validatorName: string,
validatorAddress: string,
searchValue: string,
) => {
if (
validatorName.includes(searchValue) ||
validatorAddress.includes(searchValue)
) {
return true
}
return false
}
const ManagementTable = ({ tab, searchValue }: ManagementTableProps) => {
const [validators, setValidators] = useState<Validator[]>([])
useEffect(() => {
setValidators(VALIDATORS_DATA)
}, [])
const filteredValidators = useMemo(() => {
return validators
.filter(validator => isValidStatus(validator.status, tab))
.filter(validator =>
isValidNameOrAddress(validator.name, validator.address, searchValue),
)
}, [validators, tab, searchValue])
return ( return (
<YStack> <YStack>
<table className="validator-management-table"> <table className="validator-management-table">

View File

@ -1,7 +1,7 @@
import { Text } from '@status-im/components' import { Text } from '@status-im/components'
import { Validator } from './ManagementTable'
import ManagementTableRow from './ManagementTableRow' import ManagementTableRow from './ManagementTableRow'
import { Validator } from '../ManagementTabs'
type ManagementTableBodyProps = { type ManagementTableBodyProps = {
filteredValidators: Validator[] filteredValidators: Validator[]

View File

@ -1,14 +1,65 @@
import { Tabs } from '@status-im/components' import { Tabs } from '@status-im/components'
import { Stack, XStack } from 'tamagui' import { Stack, XStack } from 'tamagui'
import { useState } from 'react' import { useEffect, useMemo, useState } from 'react'
import { VALIDATOR_TABS_MANAGEMENT } from '../../constants' import { VALIDATOR_TABS_MANAGEMENT, VALIDATORS_DATA } from '../../constants'
import ManagementTable from './ManagementTable/ManagementTable' import ManagementTable from './ManagementTable/ManagementTable'
import SearchManagement from './ManagementTable/SearchManagement' import SearchManagement from './ManagementTable/SearchManagement'
import DropdownFilter from './ManagementTable/DropdownFilter' import DropdownFilter from './ManagementTable/DropdownFilter'
export type Validator = {
name: string
address: string
balance: number
income: number
proposals: string
attestations: string
effectiveness: number
status: string
}
const isValidStatus = (validatorStatus: string, tabStatus: string) => {
if (
validatorStatus === tabStatus ||
tabStatus ===
VALIDATOR_TABS_MANAGEMENT[VALIDATOR_TABS_MANAGEMENT.length - 1]
) {
return true
}
return false
}
const isValidNameOrAddress = (
validatorName: string,
validatorAddress: string,
searchValue: string,
) => {
if (
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[]>([])
useEffect(() => {
setValidators(VALIDATORS_DATA)
}, [])
const filteredValidators = useMemo(() => {
return (
validators
// .filter(validator => isValidStatus(validator.status, tab))
.filter(validator =>
isValidNameOrAddress(validator.name, validator.address, searchValue),
)
)
}, [validators, searchValue])
const changeSearchValue = (value: string) => { const changeSearchValue = (value: string) => {
setSearchValue(value) setSearchValue(value)
@ -60,7 +111,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 tab={tab} searchValue={searchValue} /> <ManagementTable filteredValidators={filteredValidators} />
</Tabs.Content> </Tabs.Content>
))} ))}
</Tabs> </Tabs>