diff --git a/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditNftsPage/LaunchNftCollection.tsx b/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditNftsPage/LaunchNftCollection.tsx index d33e2c9b38..7b1c07e4f3 100644 --- a/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditNftsPage/LaunchNftCollection.tsx +++ b/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditNftsPage/LaunchNftCollection.tsx @@ -1,71 +1,10 @@ -import { Trans } from '@lingui/macro' -import { Button } from 'antd' import { NftRewardsPage } from 'components/Create/components' -import { useDispatch } from 'react-redux' -import { useAppSelector } from 'redux/hooks/useAppSelector' -import { editingV2ProjectActions } from 'redux/slices/editingV2Project' -import { pinNftCollectionMetadata, pinNftRewards } from 'utils/nftRewards' -import { useEditingFundingCycleConfig } from '../ReconfigureFundingCycleSettingsPage/hooks/useEditingFundingCycleConfig' -import { useReconfigureFundingCycle } from '../ReconfigureFundingCycleSettingsPage/hooks/useReconfigureFundingCycle' +import { UploadAndLaunchNftsButton } from './UploadAndLaunchNftsButton' export function LaunchNftCollection() { - const { - projectMetadata: { logoUri }, - } = useAppSelector(state => state.editingV2Project) - const dispatch = useDispatch() - - const editingFundingCycleConfig = useEditingFundingCycleConfig() - const { reconfigureLoading, reconfigureFundingCycle } = - useReconfigureFundingCycle({ - editingFundingCycleConfig, - memo: 'First NFT collection', - launchedNewNfts: true, - }) - - const onNftDeploy = async () => { - const newRewardTiers = - editingFundingCycleConfig.editingNftRewards?.rewardTiers - const collectionName = - editingFundingCycleConfig.editingNftRewards?.collectionMetadata.name ?? '' - const collectionDescription = - editingFundingCycleConfig.editingNftRewards?.collectionMetadata - .description ?? '' - const collectionLogoUri = logoUri ?? '' - const collectionInfoUri = - editingFundingCycleConfig.editingNftRewards?.collectionMetadata.uri ?? '' - - const [rewardTiersCIDs, nftCollectionMetadataUri] = await Promise.all([ - newRewardTiers ? pinNftRewards(newRewardTiers) : [], - pinNftCollectionMetadata({ - collectionName, - collectionDescription, - collectionLogoUri, - collectionInfoUri, - }), - ]) - console.info('rewardTiersCIDs: ', rewardTiersCIDs) - console.info('nftCollectionMetadataUri: ', nftCollectionMetadataUri) - dispatch(editingV2ProjectActions.setNftRewardsCIDs(rewardTiersCIDs)) - dispatch( - editingV2ProjectActions.setNftRewardsCollectionMetadataUri( - nftCollectionMetadataUri, - ), - ) - reconfigureFundingCycle() - } - return ( - Deploy NFT collection - - } + okButton={} /> ) } diff --git a/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditNftsPage/UploadAndLaunchNftsButton.tsx b/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditNftsPage/UploadAndLaunchNftsButton.tsx new file mode 100644 index 0000000000..3b7f0bab9d --- /dev/null +++ b/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/NewEditNftsPage/UploadAndLaunchNftsButton.tsx @@ -0,0 +1,95 @@ +import { Trans } from '@lingui/macro' +import { Button } from 'antd' +import { useState } from 'react' +import { useAppSelector } from 'redux/hooks/useAppSelector' +import { pinNftCollectionMetadata, pinNftRewards } from 'utils/nftRewards' +import { + EditingFundingCycleConfig, + useEditingFundingCycleConfig, +} from '../ReconfigureFundingCycleSettingsPage/hooks/useEditingFundingCycleConfig' +import { useReconfigureFundingCycle } from '../ReconfigureFundingCycleSettingsPage/hooks/useReconfigureFundingCycle' + +export function UploadAndLaunchNftsButton({ + className, +}: { + className?: string +}) { + const [ipfsUploading, setIpfsUploading] = useState(false) + + const { + projectMetadata: { logoUri }, + } = useAppSelector(state => state.editingV2Project) + + const editingFundingCycleConfig = useEditingFundingCycleConfig() + const { reconfigureLoading, reconfigureFundingCycle } = + useReconfigureFundingCycle({ + editingFundingCycleConfig, + memo: 'First NFT collection', + launchedNewNfts: true, + }) + + const { + editingPayoutGroupedSplits, + editingReservedTokensGroupedSplits, + editingFundingCycleMetadata, + editingFundingCycleData, + editingFundAccessConstraints, + editingNftRewards, + editingMustStartAtOrAfter, + } = editingFundingCycleConfig + + const uploadNftsToIpfs = async () => { + setIpfsUploading(true) + const newRewardTiers = + editingFundingCycleConfig.editingNftRewards?.rewardTiers + const collectionName = + editingFundingCycleConfig.editingNftRewards?.collectionMetadata.name ?? '' + const collectionDescription = + editingFundingCycleConfig.editingNftRewards?.collectionMetadata + .description ?? '' + const collectionLogoUri = logoUri ?? '' + const collectionInfoUri = + editingFundingCycleConfig.editingNftRewards?.collectionMetadata.uri ?? '' + + const [rewardTiersCIDs, nftCollectionMetadataUri] = await Promise.all([ + newRewardTiers ? pinNftRewards(newRewardTiers) : [], + pinNftCollectionMetadata({ + collectionName, + collectionDescription, + collectionLogoUri, + collectionInfoUri, + }), + ]) + const latestEditingData: EditingFundingCycleConfig = { + editingPayoutGroupedSplits, + editingReservedTokensGroupedSplits, + editingFundingCycleMetadata, + editingFundingCycleData, + editingFundAccessConstraints, + editingNftRewards: editingNftRewards + ? { + ...editingNftRewards, + collectionMetadata: { + ...editingNftRewards.collectionMetadata, + uri: nftCollectionMetadataUri, + }, + CIDs: rewardTiersCIDs, + } + : undefined, + editingMustStartAtOrAfter, + } + reconfigureFundingCycle(latestEditingData) + setIpfsUploading(false) + } + + return ( + + ) +} diff --git a/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/ReconfigureFundingCycleSettingsPage/hooks/useReconfigureFundingCycle.ts b/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/ReconfigureFundingCycleSettingsPage/hooks/useReconfigureFundingCycle.ts index 7ee6a2b5c6..72567cdec1 100644 --- a/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/ReconfigureFundingCycleSettingsPage/hooks/useReconfigureFundingCycle.ts +++ b/src/components/v2v3/V2V3Project/V2V3ProjectSettings/pages/ReconfigureFundingCycleSettingsPage/hooks/useReconfigureFundingCycle.ts @@ -57,10 +57,6 @@ export const useReconfigureFundingCycle = ({ const { nftRewards: { CIDs: nftRewardsCids }, } = useContext(NftRewardsContext) - console.info( - 'TODO REMOVE: editingFundingCycleConfig: ', - editingFundingCycleConfig, - ) const [reconfigureTxLoading, setReconfigureTxLoading] = useState(false) @@ -69,111 +65,109 @@ export const useReconfigureFundingCycle = ({ const reconfigureV2V3FundingCycleWithNftsTx = useReconfigureV2V3FundingCycleWithNftsTx() - const { - editingPayoutGroupedSplits, - editingReservedTokensGroupedSplits, - editingFundingCycleMetadata, - editingFundingCycleData, - editingFundAccessConstraints, - editingNftRewards, - editingMustStartAtOrAfter, - } = editingFundingCycleConfig - - const reconfigureFundingCycle = useCallback(async () => { - setReconfigureTxLoading(true) - if ( - !( - fundingCycle && - editingFundingCycleData && - editingFundingCycleMetadata && - editingFundAccessConstraints - ) - ) { - setReconfigureTxLoading(false) - throw new Error('Error deploying project.') - } - - // Projects with NFT rewards need useDataSourceForPay to be true for NFT rewards to work - const fundingCycleMetadata = nftRewardsCids?.length - ? { - ...editingFundingCycleMetadata, - ...NFT_FUNDING_CYCLE_METADATA_OVERRIDES, - } - : editingFundingCycleMetadata - - const weight = getWeightArgument({ - currentFundingCycleWeight: fundingCycle.weight, - newFundingCycleWeight: editingFundingCycleData.weight, - }) - - const reconfigureFundingCycleData: ReconfigureFundingCycleTxParams = { - fundingCycleData: { - ...editingFundingCycleData, - weight, - }, - fundingCycleMetadata, - fundAccessConstraints: editingFundAccessConstraints, - groupedSplits: [ + // If editingData given, will use that. Else, will use redux store + const reconfigureFundingCycle = useCallback( + async (forceLatestEditingData?: EditingFundingCycleConfig) => { + const { editingPayoutGroupedSplits, editingReservedTokensGroupedSplits, - ], - memo, - mustStartAtOrAfter: editingMustStartAtOrAfter, - } + editingFundingCycleMetadata, + editingFundingCycleData, + editingFundAccessConstraints, + editingNftRewards, + editingMustStartAtOrAfter, + } = forceLatestEditingData ?? editingFundingCycleConfig - const txOpts = { - async onConfirmed() { - if (projectId) { - await revalidateProject({ - pv: PV_V2, - projectId: String(projectId), - }) - } + setReconfigureTxLoading(true) + if ( + !( + fundingCycle && + editingFundingCycleData && + editingFundingCycleMetadata && + editingFundAccessConstraints + ) + ) { setReconfigureTxLoading(false) - if (onComplete) { - onComplete() - } else { - reloadWindow() - } - }, - } + throw new Error('Error deploying project.') + } + + // Projects with NFT rewards need useDataSourceForPay to be true for NFT rewards to work + const fundingCycleMetadata = nftRewardsCids?.length + ? { + ...editingFundingCycleMetadata, + ...NFT_FUNDING_CYCLE_METADATA_OVERRIDES, + } + : editingFundingCycleMetadata - let txSuccessful: boolean - if (launchedNewNfts && editingNftRewards?.rewardTiers) { - txSuccessful = await reconfigureV2V3FundingCycleWithNftsTx( - { - reconfigureData: reconfigureFundingCycleData, - tiered721DelegateData: editingNftRewards, + const weight = getWeightArgument({ + currentFundingCycleWeight: fundingCycle.weight, + newFundingCycleWeight: editingFundingCycleData.weight, + }) + + const reconfigureFundingCycleData: ReconfigureFundingCycleTxParams = { + fundingCycleData: { + ...editingFundingCycleData, + weight, + }, + fundingCycleMetadata, + fundAccessConstraints: editingFundAccessConstraints, + groupedSplits: [ + editingPayoutGroupedSplits, + editingReservedTokensGroupedSplits, + ], + memo, + mustStartAtOrAfter: editingMustStartAtOrAfter, + } + + const txOpts = { + async onConfirmed() { + if (projectId) { + await revalidateProject({ + pv: PV_V2, + projectId: String(projectId), + }) + } + setReconfigureTxLoading(false) + if (onComplete) { + onComplete() + } else { + reloadWindow() + } }, - txOpts, - ) - } else { - txSuccessful = await reconfigureV2V3FundingCycleTx( - reconfigureFundingCycleData, - txOpts, - ) - } + } + + let txSuccessful: boolean + if (launchedNewNfts && editingNftRewards?.rewardTiers) { + txSuccessful = await reconfigureV2V3FundingCycleWithNftsTx( + { + reconfigureData: reconfigureFundingCycleData, + tiered721DelegateData: editingNftRewards, + }, + txOpts, + ) + } else { + txSuccessful = await reconfigureV2V3FundingCycleTx( + reconfigureFundingCycleData, + txOpts, + ) + } - if (!txSuccessful) { - setReconfigureTxLoading(false) - } - }, [ - editingFundingCycleData, - editingFundingCycleMetadata, - editingFundAccessConstraints, - reconfigureV2V3FundingCycleTx, - reconfigureV2V3FundingCycleWithNftsTx, - launchedNewNfts, - editingNftRewards, - editingPayoutGroupedSplits, - editingReservedTokensGroupedSplits, - editingMustStartAtOrAfter, - nftRewardsCids, - fundingCycle, - memo, - onComplete, - projectId, - ]) + if (!txSuccessful) { + setReconfigureTxLoading(false) + } + }, + [ + editingFundingCycleConfig, + reconfigureV2V3FundingCycleTx, + reconfigureV2V3FundingCycleWithNftsTx, + launchedNewNfts, + nftRewardsCids, + fundingCycle, + memo, + onComplete, + projectId, + ], + ) return { reconfigureLoading: reconfigureTxLoading, reconfigureFundingCycle } }