Skip to content

Commit

Permalink
Show action button for revenue share correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
WRadoslaw committed Oct 13, 2023
1 parent 7e3446e commit 1c5137b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { useCallback } from 'react'

import { FullCreatorTokenFragment } from '@/api/queries/__generated__/fragments.generated'
import { Button } from '@/components/_buttons/Button'
import { atlasConfig } from '@/config'
import { useJoystream } from '@/providers/joystream'
import { useJoystreamStore } from '@/providers/joystream/joystream.store'
import { useSnackbar } from '@/providers/snackbars'
import { useTransaction } from '@/providers/transactions/transactions.hooks'
import { useUser } from '@/providers/user/user.hooks'

type CloseRevenueShareButtonProps = {
revenueShare: FullCreatorTokenFragment['revenueShares'][number]
}

export const CloseRevenueShareButton = ({ revenueShare }: CloseRevenueShareButtonProps) => {
const { joystream, proxyCallback } = useJoystream()
const { channelId, memberId } = useUser()
const handleTransaction = useTransaction()
const { displaySnackbar } = useSnackbar()
const { currentBlock } = useJoystreamStore()

const finalizeRevenueShare = useCallback(() => {
if (!joystream || !memberId || !channelId) {
return
}
handleTransaction({
txFactory: async (updateStatus) =>
(await joystream.extrinsics).finalizeRevenueSplit(memberId, channelId, proxyCallback(updateStatus)),
onTxSync: async (data) => {
displaySnackbar({
title: 'Revenue share is closed',
description: `Remaining unclaimed ${data.amount} ${atlasConfig.joystream.tokenTicker} was transfered back to your channel balance`,
iconType: 'info',
})
},
})
}, [channelId, displaySnackbar, handleTransaction, joystream, memberId, proxyCallback])

if (currentBlock < revenueShare.endsAt) {
return null
}

return <Button onClick={finalizeRevenueShare}>Close revenue share</Button>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CloseRevenueShareButton'
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type AuctionDatePickerProps = {
onChange: (value: AuctionDatePickerValue) => void
} & Omit<SelectProps<AuctionDatePickerValue>, 'onChange'>

// TODO shake animation on date picker is very glitchy, for now just disable it
export const AuctionDatePicker: FC<AuctionDatePickerProps> = ({
items,
value,
Expand Down
40 changes: 11 additions & 29 deletions packages/atlas/src/views/studio/CrtDashboard/CrtDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import { LimitedWidthContainer } from '@/components/LimitedWidthContainer'
import { Tabs } from '@/components/Tabs'
import { Text } from '@/components/Text'
import { Button } from '@/components/_buttons/Button'
import { CloseRevenueShareButton } from '@/components/_crt/CloseRevenueShareButton'
import { StartRevenueShare } from '@/components/_crt/StartRevenueShareModal/StartRevenueShareModal'
import { atlasConfig } from '@/config'
import { useJoystream } from '@/providers/joystream'
import { useSnackbar } from '@/providers/snackbars'
import { useTransaction } from '@/providers/transactions/transactions.hooks'
import { useUser } from '@/providers/user/user.hooks'
import { HeaderContainer, MainContainer, TabsContainer } from '@/views/studio/CrtDashboard/CrtDashboard.styles'
import { CrtDashboardMainTab } from '@/views/studio/CrtDashboard/tabs/CrtDashboardMainTab'
Expand All @@ -22,42 +19,24 @@ const TABS = ['Dashboard', 'Holders', 'Revenue share', 'Settings'] as const
export const CrtDashboard = () => {
const [currentTab, setCurrentTab] = useState<number>(0)
const [openRevenueShareModal, setOpenRevenueShareModal] = useState(false)
const { joystream, proxyCallback } = useJoystream()
const { channelId, memberId, activeChannel } = useUser()
const { activeChannel } = useUser()
const { data } = useGetFullCreatorTokenQuery({
variables: {
id: activeChannel?.creatorToken?.token.id ?? '',
},
})
const { displaySnackbar } = useSnackbar()
const handleTransaction = useTransaction()
const handleChangeTab = useCallback((idx: number) => {
setCurrentTab(idx)
}, [])

const mappedTabs = TABS.map((tab) => ({ name: tab }))

const finalizeRevenueShare = useCallback(() => {
if (!joystream || !memberId || !channelId) {
return
}
handleTransaction({
txFactory: async (updateStatus) =>
(await joystream.extrinsics).finalizeRevenueSplit(memberId, channelId, proxyCallback(updateStatus)),
onTxSync: async (data) => {
displaySnackbar({
title: 'Revenue share is closed',
description: `Remaining unclaimed ${data.amount} ${atlasConfig.joystream.tokenTicker} was transfered back to your channel balance`,
iconType: 'info',
})
},
})
}, [channelId, displaySnackbar, handleTransaction, joystream, memberId, proxyCallback])

if (!data?.creatorTokenById) {
return null
}

const activeRevenueShare = data.creatorTokenById.revenueShares.find((revenueShare) => !revenueShare.finalized)

return (
<LimitedWidthContainer>
<StartRevenueShare show={openRevenueShareModal} tokenId="1" onClose={() => setOpenRevenueShareModal(false)} />
Expand All @@ -83,10 +62,13 @@ export const CrtDashboard = () => {
)}
{currentTab === 2 && (
<>
<Button onClick={() => setOpenRevenueShareModal(true)} icon={<SvgActionRevenueShare />}>
Start revenue share
</Button>
<Button onClick={finalizeRevenueShare}>Close revenue share</Button>
{!activeRevenueShare ? (
<Button onClick={() => setOpenRevenueShareModal(true)} icon={<SvgActionRevenueShare />}>
Start revenue share
</Button>
) : (
<CloseRevenueShareButton revenueShare={activeRevenueShare} />
)}
</>
)}
</TabsContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const CrtRevenueTab = ({ token }: CrtRevenueTabProps) => {
})
const memberTokenAccount = data?.tokenAccounts[0]
const activeRevenueShare = token.revenueShares.find((revenueShare) => !revenueShare.finalized)
const finalizedRevenueShares = token.revenueShares.filter((revenueShare) => revenueShare.finalized)

return (
<LayoutGrid>
Expand Down Expand Up @@ -84,10 +85,10 @@ export const CrtRevenueTab = ({ token }: CrtRevenueTabProps) => {
</GridItem>
) : null}

{token.revenueShares.length ? (
{finalizedRevenueShares.length ? (
<GridItem colSpan={{ base: 12 }}>
<RevenueShareHistoryTable
data={token.revenueShares.map((revenueShare) => ({
data={finalizedRevenueShares.map((revenueShare) => ({
claimed: +(revenueShare.claimed ?? 0),
stakers: revenueShare.stakers,
totalParticipants: revenueShare.participantsNum,
Expand Down

0 comments on commit 1c5137b

Please sign in to comment.