77 ContractCallPayloadBuilder ,
88 ContractFunction ,
99} from '@multiversx/sdk-core' ;
10- import { useForm } from 'react-hook-form' ;
10+ import { useForm , useWatch } from 'react-hook-form' ;
1111
1212import { zodResolver } from '@hookform/resolvers/zod' ;
1313import { Form } from '@/components/ui/form' ;
@@ -27,11 +27,15 @@ import {
2727} from '@/components/operations/constants' ;
2828import { OperationsInputField } from '@/components/operations/operations-input-field' ;
2929import { OperationsCheckboxGroup } from '@/components/operations/operations-checkbox-group' ;
30- import { OperationsSubmitButton } from '.. /operations-submit-button' ;
31- import { useContext } from 'react' ;
30+ import { OperationsSubmitButton } from '@/components/operations /operations-submit-button' ;
31+ import { useContext , useEffect , useState } from 'react' ;
3232import { OperationsStateDialogContext } from '@/components/operations/operations-status-dialog' ;
3333import { CommonOpertationContentProps } from '@/components/operations/operations-common-types' ;
34- import { OperationsRadioGroup } from '../operations-radio-group' ;
34+ import { OperationsRadioGroup } from '@/components/operations/operations-radio-group' ;
35+ import { useCreatorTokens } from '@/hooks/use-creator-tokens' ;
36+ import { OperationsSelectField } from '@/components/operations/operations-select-field' ;
37+ import { useAccount } from '@useelven/core' ;
38+ import { useTokenRolesByAccount } from '@/hooks/use-token-roles-by-account' ;
3539
3640const formSchema = z . object ( {
3741 tokenId : z . string ( ) . min ( 1 , 'The field is required' ) ,
@@ -57,20 +61,54 @@ export const ToggleSpecialRoles = ({
5761 close,
5862 tokenType,
5963} : CommonOpertationContentProps ) => {
64+ const { address } = useAccount ( ) ;
6065 const { setOpen : setTxStatusDialogOpen } = useContext (
6166 OperationsStateDialogContext
6267 ) ;
6368
69+ const [ disabledRoles , setDisabledRoles ] = useState < string [ ] > ( ) ;
70+
71+ const { tokens } = useCreatorTokens < { ticker : string } > ( { tokenType } ) ;
72+
6473 const form = useForm < z . infer < typeof formSchema > > ( {
6574 resolver : zodResolver ( formSchema ) ,
6675 defaultValues : {
6776 tokenId : '' ,
68- address : '' ,
77+ address,
6978 type : 'set' ,
70- roles : [ 'ESDTRoleNFTCreate' , 'ESDTRoleNFTBurn' ] ,
79+ roles : [ ] ,
7180 } ,
7281 } ) ;
7382
83+ const watchTokenId = useWatch ( { name : 'tokenId' , control : form . control } ) ;
84+ const watchAddress = useWatch ( { name : 'address' , control : form . control } ) ;
85+ const watchType = useWatch ( { name : 'type' , control : form . control } ) ;
86+
87+ const { roles } = useTokenRolesByAccount ( {
88+ tokenId : watchTokenId ,
89+ tokenType,
90+ address : watchAddress ,
91+ } ) ;
92+
93+ // Handle selected roles in form
94+ useEffect ( ( ) => {
95+ const roleNames = rolesMap [ tokenType ] . map ( ( role ) => role . name ) ;
96+
97+ if ( ! watchTokenId || ! watchAddress ) {
98+ setDisabledRoles ( roleNames ) ;
99+ return ;
100+ }
101+
102+ let disabledRoles : string [ ] = [ ] ;
103+ if ( watchType === 'set' ) {
104+ disabledRoles = roles ;
105+ } else {
106+ disabledRoles = roleNames . filter ( ( role ) => ! roles ?. includes ( role ) ) ;
107+ }
108+ setDisabledRoles ( disabledRoles ) ;
109+ // eslint-disable-next-line react-hooks/exhaustive-deps
110+ } , [ roles , watchType , watchTokenId , watchAddress ] ) ;
111+
74112 const onSubmit = ( {
75113 tokenId,
76114 address,
@@ -109,6 +147,17 @@ export const ToggleSpecialRoles = ({
109147 close ( ) ;
110148 } ;
111149
150+ const rolesDescription = ( ) => {
151+ if ( ! watchAddress || ! watchTokenId ) {
152+ return 'Please set tokenId and address, then you can choose roles!' ;
153+ }
154+ return `Disabled ones are ${
155+ form . getValues ( 'type' ) === 'set'
156+ ? "set, so you can't set them"
157+ : "not set, so you can't unset them"
158+ } .`;
159+ } ;
160+
112161 return (
113162 < >
114163 < DialogHeader className = "p-8 pb-0" >
@@ -137,23 +186,31 @@ export const ToggleSpecialRoles = ({
137186 label = "Operation type"
138187 description = "Please choose the type of the operation. Set or Unset."
139188 />
140- < OperationsInputField
189+ < OperationsSelectField
141190 name = "tokenId"
142191 label = "Token id"
143- placeholder = "Example: MyToken-23432"
144- description = "Please provide your token id"
192+ description = "Example: MyToken-23432"
193+ options = {
194+ tokens
195+ ? tokens ?. map ( ( token ) => ( {
196+ value : token . ticker ,
197+ label : token . ticker ,
198+ } ) )
199+ : [ ]
200+ }
145201 />
146202 < OperationsInputField
147203 name = "address"
148204 label = "Address"
149205 placeholder = "Example: erd1..."
150- description = "Please provide the address for which the roles will be assigned"
206+ description = "Please provide the address for which the roles will be assigned. By default your own. "
151207 />
152208 < OperationsCheckboxGroup
153209 items = { rolesMap [ tokenType ] }
210+ disabledItems = { disabledRoles }
154211 name = "roles"
155212 label = "Roles"
156- description = " Special roles available for basic ESDT tokens."
213+ description = { ` Special roles available for basic ESDT tokens. ${ rolesDescription ( ) } ` }
157214 />
158215 </ div >
159216 </ form >
0 commit comments