Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Adding errorState in useFetchProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
henrypalacios committed Oct 29, 2021
1 parent 0d31d85 commit e88cf57
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/custom/api/gnosisProtocol/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ export type ProfileData = {

export async function getProfileData(chainId: ChainId, address: string): Promise<ProfileData | null> {
console.log(`[api:${API_NAME}] Get profile data for`, chainId, address)
return new Promise(function (_resolve, reject) {
setTimeout(() => {
return reject(new Error('setTimeout Error'))
}, 1500)
})
try {
if (chainId !== ChainId.MAINNET) {
console.info('Profile data is only available for mainnet')
Expand Down
15 changes: 11 additions & 4 deletions src/custom/hooks/useFetchProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import { ProfileData } from 'api/gnosisProtocol/api'
export default function useFetchProfile() {
const { account, chainId } = useActiveWeb3React()
const [profileData, setProfileData] = useState<ProfileData | null>(null)
const [error, setError] = useState('')

useEffect(() => {
async function fetchAndSetProfileData() {
if (chainId && account) {
if (!chainId || !account) {
return
}

try {
const profileData = await getProfileData(chainId, account)
setProfileData(profileData)
} else {
setProfileData(null)
} catch (e) {
setError('Error getting profileData')
}
}

setError('')
setProfileData(null)
fetchAndSetProfileData()
}, [account, chainId])

return profileData
return { profileData, error }
}
16 changes: 15 additions & 1 deletion src/custom/pages/Profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@ import useFetchProfile from 'hooks/useFetchProfile'
import { numberFormatter } from 'utils/format'
import useTimeAgo from 'hooks/useTimeAgo'
import { MouseoverTooltipContent } from 'components/Tooltip'
import NotificationBanner from '@src/custom/components/NotificationBanner'

export default function Profile() {
const referralLink = useReferralLink()
const { account } = useActiveWeb3React()
const profileData = useFetchProfile()
const { profileData, error } = useFetchProfile()
const lastUpdated = useTimeAgo(profileData?.lastUpdated)

const renderNotificationMessages = () => {
return (
<>
{error && (
<NotificationBanner isVisible level="error">
There was an error loading your profile data. Please try again.
</NotificationBanner>
)}
</>
)
}

return (
<Wrapper>
<GridWrap>
Expand All @@ -52,6 +65,7 @@ export default function Profile() {
</Txt>
)}
</CardHead>
{renderNotificationMessages()}
<ChildWrapper>
<Txt fs={16}>
<strong>Your referral url</strong>
Expand Down

0 comments on commit e88cf57

Please sign in to comment.