Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOK-514: disabled if builder not operational #428

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { FC, useEffect, useState } from 'react'
import { useForm } from 'react-hook-form'
import { GoRocket } from 'react-icons/go'
import { z } from 'zod'
import { percentageToWei } from '../utils'
import { percentageToWei } from '@/app/collective-rewards/settings/utils'
import { useBuilderSettingsContext } from './context'
import { Popover } from '@/components/Popover'

const formSchema = z.object({
reward: z
Expand Down Expand Up @@ -42,6 +43,7 @@ export const BuilderRewardsSettingsForm: FC = () => {
const {
current: { refetch, isLoading: isCurrentRewardsLoading },
update: { isSuccess, setNewReward, isPending },
isBuilderOperational,
} = useBuilderSettingsContext()

const form = useForm<z.infer<typeof formSchema>>({
Expand Down Expand Up @@ -84,22 +86,41 @@ export const BuilderRewardsSettingsForm: FC = () => {
{isCurrentRewardsLoading ? (
<LoadingSpinner />
) : (
<FormInput placeholder="0 ... 100 %" inputMode="decimal" {...field} />
<FormInput
placeholder="0 ... 100 %"
inputMode="decimal"
{...field}
disabled={!isBuilderOperational}
/>
)}
</FormControl>
<FormMessage />
</FormItem>
)}
></FormField>
<div className="flex flex-row justify-start gap-4">
<Button
startIcon={<GoRocket />}
disabled={!isDirty || !isValid}
buttonProps={{ type: 'submit' }}
loading={isPending}
<Popover
content={
<div className="text-[12px] font-bold mb-1">
<p data-testid="adjustBackerRewardPctTooltip">
You need to be operational to adjust your backer reward %
</p>
</div>
}
size="small"
position="top"
trigger="hover"
disabled={isBuilderOperational}
>
Save changes
</Button>
<Button
startIcon={<GoRocket />}
disabled={!isDirty || !isValid || !isBuilderOperational}
buttonProps={{ type: 'submit' }}
loading={isPending}
>
Save changes
</Button>
</Popover>
<Button
variant="secondary"
onClick={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import {
import { createContext, FC, ReactNode, useContext } from 'react'
import { Address } from 'viem'
import { useAccount } from 'wagmi'
import { useBuilderContext } from '@/app/collective-rewards/user'
import { isBuilderOperational } from '@/app/collective-rewards/utils'

type BackerRewardsPercentageContext = {
update: SetBackerRewardsForBuilder
current: BackerRewardResponse
rewardPercentageToApply: RewardPercentageToApply
isBuilderOperational: boolean
}

const BuilderSettingsContext = createContext<BackerRewardsPercentageContext>(
Expand All @@ -22,15 +25,19 @@ const BuilderSettingsContext = createContext<BackerRewardsPercentageContext>(
export const useBuilderSettingsContext = () => useContext(BuilderSettingsContext)

export const BuilderSettingsProvider: FC<{ children: ReactNode }> = ({ children }) => {
const { getBuilderByAddress } = useBuilderContext()
const { address } = useAccount()
const current = useGetBackerRewardsForBuilder(address as Address)
const update = useSetBackerRewardsForBuilder()
const rewardPercentageToApply = useGetRewardPercentageToApply(address as Address)
const builder = getBuilderByAddress(address as Address)
const isOperational = builder ? isBuilderOperational(builder.stateFlags) : false

const contextValue: BackerRewardsPercentageContext = {
update,
current,
rewardPercentageToApply,
isBuilderOperational: isOperational,
}

return <BuilderSettingsContext.Provider value={contextValue}>{children}</BuilderSettingsContext.Provider>
Expand Down
2 changes: 2 additions & 0 deletions src/app/collective-rewards/settings/builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './BuilderRewardsSettingsMetrics'
export * from './BuilderRewardsSettingsForm'
export * from './BuilderSettings'
export * from './context'
export * from './hooks'
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,12 @@ export const ActionCell: FC<ActionCellProps> = ({ tableHeader: { className }, bu
[selections, builderAddress, isPreallocated],
)

const isBuilderOperational = useMemo(() => {
const isOperational = useMemo(() => {
if (!builder) {
console.log('Builder not found in selection') // TODO: handle this case better
return
}
return (
builder.stateFlags &&
builder.stateFlags.kycApproved &&
builder.stateFlags.communityApproved &&
!builder.stateFlags.paused
)
return isBuilderOperational(builder.stateFlags)
}, [builder])

const selectBuilder = () => {
Expand All @@ -228,7 +223,7 @@ export const ActionCell: FC<ActionCellProps> = ({ tableHeader: { className }, bu
<TableCell className={cn(className, 'border-solid align-center')}>
<Button
variant={isSelected ? 'white' : 'secondary'}
disabled={!isBuilderOperational || isPreallocated}
disabled={!isOperational || isPreallocated}
onClick={selectBuilder}
className="white text-center"
>
Expand Down
4 changes: 2 additions & 2 deletions src/app/collective-rewards/utils/getMostAdvancedProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ProposalState } from '@/shared/types'

const inactiveProposalsStates = [ProposalState.Canceled, ProposalState.Defeated, ProposalState.Expired]
const isActive = (state: ProposalState) => !inactiveProposalsStates.includes(state)
const isBuilderActive = (status?: BuilderStateFlags) =>
!status || status.activated || status.communityApproved
const isBuilderActive = (stateFlags?: BuilderStateFlags) =>
!stateFlags || stateFlags.activated || stateFlags.communityApproved

export const getMostAdvancedProposal = (
proposalsEvent: CreateBuilderProposalEventLog[],
Expand Down
Loading