Skip to content

Commit

Permalink
Merge pull request #205 from chain4travel/feat-validator-badge
Browse files Browse the repository at this point in the history
fix fetching validators data and updating it after network change
  • Loading branch information
aeddaqqa authored Apr 9, 2024
2 parents 6ac3508 + 85ba48f commit a96e1c0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 35 deletions.
16 changes: 9 additions & 7 deletions src/components/Partners/PartnerCard.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Box, Typography } from '@mui/material'

import React, { useEffect, useState } from 'react'
import { PartnerDataType, Validator } from '../../@types/partners'
import { PartnerDataType } from '../../@types/partners'
import { useAppSelector } from '../../hooks/reduxHooks'
import { useEffectOnce } from '../../hooks/useEffectOnce'
import useWallet from '../../hooks/useWallet'
import { selectValidators } from '../../redux/slices/app-config'
import PartnerBusinessFields from './PartnerBusinessFields'
import PartnerFlag from './PartnerFlag'
import PartnerLogo from './PartnerLogo'
Expand All @@ -13,17 +15,17 @@ interface PartnerCardProps {
clickable: boolean
partner: PartnerDataType
index: number
validators: Validator[]
}

const PartnerCard: React.FC<PartnerCardProps> = ({ partner, clickable, onClick, validators }) => {
const PartnerCard: React.FC<PartnerCardProps> = ({ partner, clickable, onClick }) => {
useEffectOnce(() => {})
const { getRegisteredNode } = useWallet()
const [isValidator, setIsValidator] = useState(false)

const validators = useAppSelector(selectValidators)
const { getAddress } = useWallet()
const chackValidatorStatus = async (address: string) => {
if (!pChainAddress) setIsValidator(false)
let nodeID = await getRegisteredNode(address)
let nodeID = await getRegisteredNode(getAddress(address))
setIsValidator(!!validators.find(v => v.nodeID === nodeID))
}
const {
Expand All @@ -38,8 +40,8 @@ const PartnerCard: React.FC<PartnerCardProps> = ({ partner, clickable, onClick,
},
} = partner
useEffect(() => {
chackValidatorStatus(pChainAddress)
}, [partner])
if (pChainAddress) chackValidatorStatus(pChainAddress)
}, [partner, validators])

Check warning on line 44 in src/components/Partners/PartnerCard.tsx

View workflow job for this annotation

GitHub Actions / yarn-build

React Hook useEffect has missing dependencies: 'chackValidatorStatus' and 'pChainAddress'. Either include them or remove the dependency array
return (
<Box
onClick={onClick}
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useStore } from 'Explorer/useStore'
import { AvaNetwork } from 'wallet/AvaNetwork'
import store from 'wallet/store'
import { Status } from '../@types'
import { getCurrentValidators } from '../redux/slices/utils'

const useNetwork = (): {
handleChangeNetwork: (arg: string) => void
Expand Down Expand Up @@ -73,6 +74,7 @@ const useNetwork = (): {
severity: 'success',
}),
)
dispatch(getCurrentValidators())
localStorage.setItem('selectedNetwork', network.name.toLowerCase())
} catch (e) {
store.state.Network.selectedNetwork = null
Expand Down
14 changes: 10 additions & 4 deletions src/hooks/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import { useAppDispatch } from './reduxHooks'
const useWallet = () => {
const dispatch = useAppDispatch()

async function getCurrentValidators() {
return caminoClient.PChain().getCurrentValidators()
}
async function getRegisteredNode(address: string): Promise<string> {
return await caminoClient.PChain().getRegisteredShortIDLink(address)
}
const getAddress = address => {
if (address) {
let res = caminoClient
.PChain()
.addressFromBuffer(caminoClient.PChain().parseAddress(address))
return res
}
return ''
}
const updateStore = (type, params) => {
switch (type) {
case 'updateName':
Expand All @@ -23,7 +29,7 @@ const useWallet = () => {
)
}
}
return { updateStore, getRegisteredNode, getCurrentValidators }
return { updateStore, getRegisteredNode, getAddress }
}

export default useWallet
2 changes: 0 additions & 2 deletions src/layout/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { useEffectOnce } from '../hooks/useEffectOnce'
import useNetwork from '../hooks/useNetwork'
// @ts-ignore
import { useStore } from 'Explorer/useStore'
import { getCurrentValidators } from '../redux/slices/utils'

const MainLayout = ({ children }) => {
const [loadNetworks, setLoadNetworks] = useState(true)
Expand Down Expand Up @@ -67,7 +66,6 @@ const MainLayout = ({ children }) => {
useEffectOnce(() => {
init()
})
dispatch(getCurrentValidators())
useEffect(() => {
const html = document.documentElement
if (loading || loadNetworks) html.style.overflow = 'hidden'
Expand Down
5 changes: 2 additions & 3 deletions src/redux/slices/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAsyncThunk } from '@reduxjs/toolkit'
import useWallet from '../../hooks/useWallet'
import { ava as caminoClient } from 'wallet/caminoClient'

export const updateAuthStatus = createAsyncThunk(
'appConfig/updateAuthStatus',
Expand All @@ -12,8 +12,7 @@ export const updateAuthStatus = createAsyncThunk(
)

export const getCurrentValidators = createAsyncThunk('appConfig/getCurrentValidators', async () => {
const { getCurrentValidators } = useWallet()
try {
return (await getCurrentValidators()).validators
return (await caminoClient.PChain().getCurrentValidators()).validators
} catch (e) {}
})
14 changes: 8 additions & 6 deletions src/views/partners/ListPartners.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { PartnersResponseType } from '../../@types/partners'

import { Box } from '@mui/material'
import React from 'react'
import React, { useEffect } from 'react'
import { useNavigate } from 'react-router'
import PartnerCard from '../../components/Partners/PartnerCard'
import { useAppSelector } from '../../hooks/reduxHooks'
import { selectValidators } from '../../redux/slices/app-config'
import { useAppDispatch } from '../../hooks/reduxHooks'
import { getCurrentValidators } from '../../redux/slices/utils'

interface ListPartnersProps {
partners: PartnersResponseType
}

const ListPartners: React.FC<ListPartnersProps> = ({ partners }) => {
const navigate = useNavigate()
const validators = useAppSelector(selectValidators)
const dispatch = useAppDispatch()
useEffect(() => {
dispatch(getCurrentValidators())
}, [])

Check warning on line 19 in src/views/partners/ListPartners.tsx

View workflow job for this annotation

GitHub Actions / yarn-build

React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array
return (
<Box
sx={{
Expand All @@ -25,7 +28,6 @@ const ListPartners: React.FC<ListPartnersProps> = ({ partners }) => {
>
{partners.data.map((partner, index) => (
<PartnerCard
validators={validators}
onClick={() => {
if (
!!(
Expand All @@ -42,7 +44,7 @@ const ListPartners: React.FC<ListPartnersProps> = ({ partners }) => {
}
}}
partner={partner}
key={index}
key={partner.attributes.companyName}
clickable={
!!(
partner.attributes.companyName &&
Expand Down
14 changes: 1 addition & 13 deletions src/views/partners/Partner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,7 @@ const Partner = () => {
companyName: partnerID,
})
const navigate = useNavigate()
if (
error ||
!partner ||
!!(
partner.attributes.companyName &&
partner.attributes.companyLongDescription &&
partner.attributes.companyWebsite &&
partner.attributes.contactEmail &&
partner.attributes.contactFirstname &&
partner.attributes.contactLastname &&
partner.attributes.contactPhone
)
) {
if (error || (!partner && !isFetching && !isLoading)) {
navigate('/partners')
return null
}
Expand Down

0 comments on commit a96e1c0

Please sign in to comment.