Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: create hook to handle subscription
- Loading branch information
Showing
4 changed files
with
146 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { gql } from '@apollo/client' | ||
|
||
export const FRAGMENT_FUNDING_TX_FOR_PROJECT_FUNDING = gql` | ||
fragment FundingTxForProjectFunding on FundingTx { | ||
id | ||
comment | ||
amount | ||
funder { | ||
id | ||
amountFunded | ||
timesFunded | ||
confirmedAt | ||
user { | ||
id | ||
username | ||
imageUrl | ||
externalAccounts { | ||
externalUsername | ||
public | ||
accountType | ||
} | ||
} | ||
} | ||
paidAt | ||
onChain | ||
media | ||
source | ||
method | ||
projectId | ||
sourceResource { | ||
... on Project { | ||
id | ||
name | ||
title | ||
image | ||
createdAt | ||
thumbnailImage | ||
} | ||
... on Entry { | ||
createdAt | ||
id | ||
image | ||
title | ||
} | ||
} | ||
} | ||
` | ||
|
||
export const PROJECT_FUNDING_SUBSCRIPTION = gql` | ||
${FRAGMENT_FUNDING_TX_FOR_PROJECT_FUNDING} | ||
subscription ActivityCreated($input: ActivityCreatedSubscriptionInput) { | ||
activityCreated(input: $input) { | ||
... on FundingTx { | ||
...FundingTxForProjectFunding | ||
} | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useSubscription } from '@apollo/client' | ||
import { useState } from 'react' | ||
|
||
import { ACTIVITY_CREATION_SUBSCRIPTION } from '../../graphql/subscriptions' | ||
import { | ||
ActivityCreatedSubscription, | ||
ActivityCreatedSubscriptionInput, | ||
ActivityResourceType, | ||
FundingTxForLandingPageFragment, | ||
} from '../../types' | ||
|
||
type UseFundSubscriptionProps = { | ||
projectId: number | ||
} | ||
|
||
export const useFundSubscription = ({ | ||
projectId, | ||
}: UseFundSubscriptionProps) => { | ||
const [skip, setSkip] = useState(true) | ||
const [fundingTxId, setFundingTxId] = useState<number | null>(null) | ||
const [funded, setFunded] = useState(false) | ||
|
||
const startListening = (id: number) => { | ||
setFundingTxId(id) | ||
setSkip(false) | ||
} | ||
|
||
const stopListening = () => { | ||
setFundingTxId(null) | ||
setFunded(false) | ||
setSkip(true) | ||
} | ||
|
||
const skipSubscription = skip || !projectId || !fundingTxId | ||
|
||
const { loading } = useSubscription< | ||
ActivityCreatedSubscription, | ||
ActivityCreatedSubscriptionInput | ||
>(ACTIVITY_CREATION_SUBSCRIPTION, { | ||
variables: { | ||
where: { | ||
projectIds: [projectId], | ||
resourceType: ActivityResourceType.FundingTx, | ||
}, | ||
}, | ||
skip: skipSubscription, | ||
onData(options) { | ||
console.log('checking value', options) | ||
const activityCreated = options.data.data | ||
?.activityCreated as FundingTxForLandingPageFragment | ||
if (activityCreated.id === fundingTxId) { | ||
setFunded(false) | ||
} | ||
}, | ||
}) | ||
console.log('checking loading', loading) | ||
return { startListening, stopListening, funded } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters