Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

[PAY-1628] Navigate to track after purchase #3904

Merged
merged 6 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions packages/common/src/store/purchase-content/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { getUSDCUserBank } from 'store/buy-usdc/utils'
import { getTrack } from 'store/cache/tracks/selectors'
import { getUser } from 'store/cache/users/selectors'
import { getContext } from 'store/effects'
import { setVisibility } from 'store/ui/modals/slice'
import { BN_USDC_CENT_WEI, ceilingBNUSDCToNearestCent } from 'utils/wallet'

import { pollPremiumTrack } from '../premium-content/sagas'
Expand Down Expand Up @@ -221,13 +220,6 @@ function* doStartPurchaseContentFlow({
// finish
yield* put(purchaseConfirmed())

yield* put(
setVisibility({
modal: 'PremiumContentPurchase',
visible: false
})
)

yield* call(
track,
make({ eventName: Name.PURCHASE_CONTENT_SUCCESS, contentId, contentType })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import {
combineStatuses,
premiumContentSelectors,
purchaseContentActions,
statusIsNotFinalized,
useGetTrackById,
useUSDCBalance
} from '@audius/common'
import { IconCart, Modal, ModalContentPages, ModalHeader } from '@audius/stems'
import { IconCart, Modal, ModalContent, ModalHeader } from '@audius/stems'
import { useDispatch, useSelector } from 'react-redux'

import { useModalState } from 'common/hooks/useModalState'
import { Icon } from 'components/Icon'
import { Text } from 'components/typography'

import styles from './PremiumContentPurchaseModal.module.css'
import { LoadingPage } from './components/LoadingPage'
import { PurchaseDetailsPage } from './components/PurchaseDetailsPage'

const { getPurchaseContentId } = premiumContentSelectors
Expand All @@ -25,11 +23,6 @@ const messages = {
completePurchase: 'Complete Purchase'
}

enum PurchaseSteps {
LOADING = 0,
DETAILS = 1
}

const usePremiumContentPurchaseModalState = () => {
const trackId = useSelector(getPurchaseContentId)
const dispatch = useDispatch()
Expand All @@ -47,15 +40,11 @@ const usePremiumContentPurchaseModalState = () => {
dispatch(purchaseContentActions.cleanup())
}, [setIsOpen, dispatch])

const currentStep = statusIsNotFinalized(status)
? PurchaseSteps.LOADING
: PurchaseSteps.DETAILS

return { isOpen, handleClose, currentStep, track, balance, status }
return { isOpen, handleClose, track, balance, status }
}

export const PremiumContentPurchaseModal = () => {
const { balance, isOpen, handleClose, currentStep, track } =
const { balance, isOpen, handleClose, track } =
usePremiumContentPurchaseModalState()

return (
Expand All @@ -77,16 +66,15 @@ export const PremiumContentPurchaseModal = () => {
{messages.completePurchase}
</Text>
</ModalHeader>
{track ? (
<ModalContentPages currentPage={currentStep}>
<LoadingPage />
<ModalContent>
{track ? (
<PurchaseDetailsPage
track={track}
currentBalance={balance}
onViewTrackClicked={handleClose}
/>
</ModalContentPages>
) : null}
) : null}
</ModalContent>
</Modal>
)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react'
import { useCallback, useEffect } from 'react'

import {
BNUSDC,
Expand Down Expand Up @@ -28,6 +28,7 @@ import LoadingSpinner from 'components/loading-spinner/LoadingSpinner'
import { LockedTrackDetailsTile } from 'components/track/LockedTrackDetailsTile'
import { TwitterShareButton } from 'components/twitter-share-button/TwitterShareButton'
import { Text } from 'components/typography'
import { pushUniqueRoute } from 'utils/route'

import { FormatPrice } from './FormatPrice'
import { PayToUnlockInfo } from './PayToUnlockInfo'
Expand All @@ -48,6 +49,18 @@ const messages = {
viewTrack: 'View Track'
}

const useNavigateOnSuccess = (
track: UserTrackMetadata,
stage: PurchaseContentStage
) => {
const dispatch = useDispatch()
useEffect(() => {
if (stage === PurchaseContentStage.FINISH) {
dispatch(pushUniqueRoute(track.permalink))
}
}, [stage, track, dispatch])
}

const ContentPurchaseError = () => {
return (
<Text className={styles.errorContainer} color='--accent-red'>
Expand Down Expand Up @@ -85,6 +98,8 @@ export const PurchaseDetailsPage = ({
)
}, [isUnlocking, dispatch, track.track_id])

useNavigateOnSuccess(track, stage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit picky and no need to change, but I'm curious why you decided to split the hook out rather than using useEffect inline?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have an undocumented preference for components to not have too much hook logic at the top and this seemed like a nice way to clearly indicate what the hook is doing and keep the component itself simple.


if (!isPremiumContentUSDCPurchaseGated(track.premium_conditions)) {
console.error(
`Loaded Purchase modal with a non-USDC-gated track: ${track.track_id}`
Expand Down
11 changes: 10 additions & 1 deletion packages/web/src/components/track/PremiumTrackSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,17 @@ const LockedPremiumTrackSection = ({

const handlePurchase = useCallback(() => {
dispatch(setPurchaseContentId({ id: trackId }))
if (lockedContentModalVisibility) {
setLockedContentModalVisibility(false)
}
setPurchaseModalVisibility(true)
}, [trackId, setPurchaseModalVisibility, dispatch])
}, [
trackId,
lockedContentModalVisibility,
setPurchaseModalVisibility,
setLockedContentModalVisibility,
dispatch
])

const handleSendTip = useCallback(() => {
if (account) {
Expand Down