Skip to content

Commit

Permalink
Fix fetch new domain after successful buy by dot
Browse files Browse the repository at this point in the history
  • Loading branch information
samchuk-vlad committed Sep 20, 2023
1 parent da9a0b5 commit ab8ff68
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
5 changes: 2 additions & 3 deletions src/components/domains/BuyDomainButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ export const BuyByDotTxButton = ({
close,
price,
}: BuyByDotTxButtonProps) => {
const { recipient, purchaser, setIsFetchNewDomains, setProcessingDomains } =
useManageDomainContext()
const { recipient, purchaser, setDomainToFetch, setProcessingDomains } = useManageDomainContext()
const sellerConfig = useSelectSellerConfig()
const dispatch = useAppDispatch()
const myAddress = useMyAddress()
Expand Down Expand Up @@ -115,7 +114,7 @@ export const BuyByDotTxButton = ({
}

const onSuccess = async () => {
setIsFetchNewDomains(true)
setDomainToFetch(domainName)
setProcessingDomains({ [domainName]: true })

close()
Expand Down
2 changes: 1 addition & 1 deletion src/components/domains/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const DomainMarketSection = ({ promoCode }: DomainServerProps) => {
const myAddress = useMyAddress()
const { domains } = useMyDomains()
const { api, isApiReady, subsocial } = useSubstrate()
useFetchNewDomains(domain?.id)
useFetchNewDomains()

const onSearchDomain = async (domain?: string) => {
if (!domain) return
Expand Down
14 changes: 7 additions & 7 deletions src/components/domains/manage/ManageDomainProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type ManageDomainProviderState = {
setRecipient: (recipient: string) => void
purchaser: string
setPurchaser: (recipient: string) => void
isFetchNewDomains: boolean
setIsFetchNewDomains: (isFetchNewDomains: boolean) => void
domainToFetch?: string
setDomainToFetch: (domainToFetch?: string) => void
processingDomains: Record<string, boolean>
setProcessingDomains: (processingDomains: Record<string, boolean>) => void
}
Expand All @@ -36,15 +36,15 @@ export const ManageDomainProvider: React.FC<{ promoCode?: string }> = ({ childre
const [domainSellerKind, setVariant] = useState<DomainSellerKind>('SUB')
const [recipient, setRecipient] = useState<string>(myAddress || '')
const [purchaser, setPurchaser] = useState<string>(myAddress || '')
const [isFetchNewDomains, setIsFetchNewDomains] = useState(false)
const [domainToFetch, setDomainToFetch] = useState<string>()
const [processingDomains, setProcessingDomains] = useState<Record<string, boolean>>({})

useEffect(() => {
if(!myAddress) return
if (!myAddress) return

setRecipient(myAddress)
setPurchaser(myAddress)
}, [ myAddress])
}, [myAddress])

const router = useRouter()
const [activePromoCode, setActivePromoCode] = useState(promoCode)
Expand Down Expand Up @@ -78,8 +78,8 @@ export const ManageDomainProvider: React.FC<{ promoCode?: string }> = ({ childre
clearPromoCode,
purchaser,
setPurchaser,
isFetchNewDomains,
setIsFetchNewDomains,
domainToFetch,
setDomainToFetch,
processingDomains,
setProcessingDomains: (newData: Record<string, boolean>) =>
setProcessingDomains({ ...processingDomains, ...newData }),
Expand Down
22 changes: 11 additions & 11 deletions src/components/domains/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,38 +132,38 @@ const waitMessage = controlledMessage({
icon: <LoadingOutlined />,
})

export const useFetchNewDomains = (domainName?: string) => {
export const useFetchNewDomains = () => {
const myAddress = useMyAddress()
const { setIsFetchNewDomains, isFetchNewDomains, openManageModal, setProcessingDomains } =
const { setDomainToFetch, domainToFetch, openManageModal, setProcessingDomains } =
useManageDomainContext()
const upsertDomains = useCreateUpsertDomains()
const removePendingOrder = useCreateRemovePendingOrders()

useSubsocialEffect(
({ substrate }) => {
setIsFetchNewDomains(false)
if (!myAddress || !isFetchNewDomains || !domainName) return
if (!myAddress || !domainToFetch || !domainToFetch) return

let unsub: any

const subscription = async () => {
const api = await (await substrate.api).isReady
waitMessage.open()

unsub = await api.query.domains.registeredDomains(domainName, data => {
unsub = await api.query.domains.registeredDomains(domainToFetch, data => {
const domain = data.unwrapOr(undefined)

if (domain) {
const domainEntity = {
id: domainName,
id: domainToFetch,
...(domain as unknown as DomainStruct),
}

upsertDomains({ domain: domainEntity, address: myAddress, domainName })
removePendingOrder({ domainName })
setProcessingDomains({ [domainName]: false })
setDomainToFetch(undefined)
upsertDomains({ domain: domainEntity, address: myAddress, domainName: domainToFetch })
removePendingOrder({ domainName: domainToFetch })
setProcessingDomains({ [domainToFetch]: false })
waitMessage.close()
openManageModal('success', domainName)
openManageModal('success', domainToFetch)
}
})
}
Expand All @@ -172,7 +172,7 @@ export const useFetchNewDomains = (domainName?: string) => {

return () => unsub && unsub()
},
[isFetchNewDomains, myAddress],
[domainToFetch, myAddress],
)
}

Expand Down

0 comments on commit ab8ff68

Please sign in to comment.