Skip to content

Commit

Permalink
Merge pull request #569 from ensdomains/fix/extend-warning-logic
Browse files Browse the repository at this point in the history
fix: extend warning not showing when user is owner but not manager
  • Loading branch information
LeonmanRolls authored Aug 22, 2023
2 parents b235a69 + f085680 commit 38f320a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/components/ProfileSnippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import styled, { css } from 'styled-components'
import { Button, NametagSVG, Tag, Typography, mq } from '@ensdomains/thorin'

import FastForwardSVG from '@app/assets/FastForward.svg'
import { useAbilities } from '@app/hooks/abilities/useAbilities'
import useBeautifiedName from '@app/hooks/useBeautifiedName'
import { useRouterWithHistory } from '@app/hooks/useRouterWithHistory'
import { shouldShowExtendWarning } from '@app/utils/abilities/shouldShowExtendWarning'

import { useTransactionFlow } from '../transaction-flow/TransactionFlowProvider'
import { NameAvatar } from './AvatarWithZorb'
Expand Down Expand Up @@ -159,21 +161,21 @@ export const ProfileSnippet = ({
getTextRecord,
button,
network,
canEdit,
isPrimary,
children,
}: {
name: string
getTextRecord?: (key: string) => { value: string } | undefined
button?: 'viewProfile' | 'extend' | 'register'
canEdit?: boolean
isPrimary?: boolean
network: number
children?: React.ReactNode
}) => {
const router = useRouterWithHistory()
const { t } = useTranslation('common')

const abilities = useAbilities(name)

const { prepareDataInput } = useTransactionFlow()
const showExtendNamesInput = prepareDataInput('ExtendNames')

Expand All @@ -194,7 +196,10 @@ export const ProfileSnippet = ({
prefix={<FastForwardSVG />}
data-testid="extend-button"
onClick={() => {
showExtendNamesInput(`extend-names-${name}`, { names: [name], isSelf: canEdit })
showExtendNamesInput(`extend-names-${name}`, {
names: [name],
isSelf: shouldShowExtendWarning(abilities.data),
})
}}
>
{t('action.extend', { ns: 'common' })}
Expand All @@ -221,7 +226,7 @@ export const ProfileSnippet = ({
</Button>
)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [button, name, canEdit])
}, [button, name, abilities.data])

return (
<Container $banner={banner} data-testid="profile-snippet">
Expand All @@ -231,7 +236,7 @@ export const ProfileSnippet = ({
label={name}
name={name}
network={network}
noCache={canEdit}
noCache={abilities.data?.canEdit}
/>
<ButtonStack>
{ActionButton && <DetailButtonWrapper>{ActionButton}</DetailButtonWrapper>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Button, FastForwardSVG, mq } from '@ensdomains/thorin'

import { useAbilities } from '@app/hooks/abilities/useAbilities'
import { useTransactionFlow } from '@app/transaction-flow/TransactionFlowProvider'
import { shouldShowExtendWarning } from '@app/utils/abilities/shouldShowExtendWarning'

const FastForwardIcon = styled.svg(
({ theme }) => css`
Expand Down Expand Up @@ -43,7 +44,7 @@ export const ExtendButton = ({ name }: { name: string }) => {
onClick={() => {
showExtendNamesInput(`extend-names-${name}`, {
names: [name],
isSelf: abilities.data?.canEdit,
isSelf: shouldShowExtendWarning(abilities.data),
})
}}
prefix={<FastForwardIcon as={FastForwardSVG} />}
Expand Down
1 change: 0 additions & 1 deletion src/components/pages/profile/[name]/tabs/ProfileTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ const ProfileTab = ({ nameDetails, name }: Props) => {
network={chainId}
getTextRecord={getTextRecord}
button={snippetButton}
canEdit={abilities.data?.canEdit}
isPrimary={name === primaryData?.name}
>
{nameDetails.isNonASCII && (
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/abilities/useAbilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type SendAbilities = {
canSendError?: string
}

type Abilities = ExtendAbilities &
export type Abilities = ExtendAbilities &
DeleteAbilities &
EditAbilities &
ReclaimAbilities &
Expand Down
17 changes: 17 additions & 0 deletions src/utils/abilities/shouldShowExtendWarning.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { shouldShowExtendWarning } from "./shouldShowExtendWarning"

it('should return true when can edit', () => {
expect(shouldShowExtendWarning({ canEdit: true } as any)).toBe(true)
})

it('should return true when can send', () => {
expect(shouldShowExtendWarning({ canSend: true } as any)).toBe(true)
})

it('should return false when no abilities', () => {
expect(shouldShowExtendWarning(undefined)).toBe(false)
})

it('should return false when no edit or send abilities', () => {
expect(shouldShowExtendWarning({ canEdit: false, canSend: false } as any)).toBe(false)
})
6 changes: 6 additions & 0 deletions src/utils/abilities/shouldShowExtendWarning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Abilities } from '@app/hooks/abilities/useAbilities'

export const shouldShowExtendWarning = (abilities: Abilities | undefined) => {
if (!abilities) return false
return abilities?.canEdit || abilities.canSend
}

0 comments on commit 38f320a

Please sign in to comment.