Skip to content

Commit

Permalink
Merge pull request #38 from marvinmarnold/ma/fix-double-create-campaign
Browse files Browse the repository at this point in the history
fix: no double click to create campaign
  • Loading branch information
0xPetra authored Aug 17, 2023
2 parents b23d26d + e55f228 commit e66fea3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
34 changes: 21 additions & 13 deletions src/pages/CreateCampaign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,22 @@ const CreateCampaign = () => {
const [rewardReferrer, setRewardReferrer] = useState<string>('')
const [rewardReferee, setRewardReferee] = useState<string>('')
const [contractDecimals, setContractDecimals] = useState<number>(10)
const [args, setArgs] = useState<any[]>(['', '', '', 0, 0, 0, 0, '', 0])
const [args, setArgs] = useState<any[]>([])
const [minCampaignTokenBalance, setMinCampaignTokenBalance] = useState<string>('')
const [returnedData, setReturnedData] = useState<any>('')
const formWidth = useBreakpointValue({ base: '90%', md: '600px' })
const [isLoading, setIsLoading] = useState<boolean>(false)
const [isSubmitting, setIsSubmitting] = useState<boolean>(false)
const [isReadyToSubmit, setIsReadyToSubmit] = useState(false)

const { address } = useAccount()
const actionid = uuidv4()

const bigIntMaxReferalsperReferee = maxReferalsperReferee ? parseUnits(maxReferalsperReferee, contractDecimals) : 0
const bigIntRewardReferer = rewardReferrer ? parseUnits(rewardReferrer, contractDecimals) : 0
const bigIntRewardReferee = rewardReferee ? parseUnits(rewardReferee, contractDecimals) : 0
const bigIntMinCampaignTokenBalance = minCampaignTokenBalance ? parseUnits(minCampaignTokenBalance, contractDecimals) : 0

const chainId: number = network.chain?.id ?? 5

const { config, error, isError } = usePrepareContractWrite({
abi: CampaignFactory.abi,
enabled: isSubmitting,
enabled: isReadyToSubmit,
functionName: 'addCampaign',
// FIXME: Add depending on the chain
address: networks[chainId].factoryContract,
args,
})
Expand All @@ -60,6 +54,10 @@ const CreateCampaign = () => {
hash: data?.hash,
})
const worldId = networks[chainId].worldId
const bigIntMaxReferalsperReferee = maxReferalsperReferee ? parseUnits(maxReferalsperReferee, contractDecimals) : false
const bigIntRewardReferer = rewardReferrer ? parseUnits(rewardReferrer, contractDecimals) : false
const bigIntRewardReferee = rewardReferee ? parseUnits(rewardReferee, contractDecimals) : false
const bigIntMinCampaignTokenBalance = minCampaignTokenBalance ? parseUnits(minCampaignTokenBalance, contractDecimals) : false

useEffect(() => {
setArgs([
Expand All @@ -73,6 +71,14 @@ const CreateCampaign = () => {
randActionId.toString(),
randActionId,
])
if (campaignContractAddress === '') return
if (rewardTokenAddress === '') return
if (!bigIntMaxReferalsperReferee) return
if (!bigIntRewardReferer) return
if (!bigIntRewardReferee) return
if (!bigIntMinCampaignTokenBalance) return

setIsReadyToSubmit(true)
}, [
worldId,
campaignContractAddress,
Expand Down Expand Up @@ -103,8 +109,10 @@ const CreateCampaign = () => {
try {
console.log('Sending TX')
console.log(args)
setIsSubmitting(true)
await write?.()
if (!!write) {
await write?.()
setIsSubmitting(true)
}
} catch (error) {
toast({
title: 'Error',
Expand All @@ -113,7 +121,7 @@ const CreateCampaign = () => {
duration: 9000,
isClosable: true,
})
setIsLoading(false)
setIsSubmitting(false)
}
}

Expand Down Expand Up @@ -281,7 +289,7 @@ const CreateCampaign = () => {
fontFamily="Dm Sans"
color="white"
type="submit"
isLoading={isLoading}
isLoading={isSubmitting}
onClick={handleSubmit}
disabled={writeLoading || isContractLoading}>
{writeLoading || isContractLoading ? 'Loading...' : 'Create'}
Expand Down
6 changes: 5 additions & 1 deletion src/pages/retrieve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,20 @@ const CreateLink: NextPage = () => {
functionName: 'acceptReferral',
address: campaignAddy,
args,
onSettled(data, error) {
console.warn('Settled', { data, error })
},
})

const { data, error, isError, write } = useContractWrite(config)

const execute = () => {
if (!!write) {
write()
setIsSubmitting(true)
console.log('executed')
} else {
console.warn("Can't execute because useContractWrite has is not yet ready")
console.warn("Can't execute because useContractWrite is not yet ready")
console.log(args)
console.log('sendTx')
console.log(write)
Expand Down

0 comments on commit e66fea3

Please sign in to comment.