@@ -16,24 +16,22 @@ import {
1616 Tooltip ,
1717 Typography ,
1818} from '@mui/material'
19- import { DavinciSDK } from '@vocdoni/davinci-sdk'
19+ import { PlainCensus } from '@vocdoni/davinci-sdk'
2020import { Wallet , JsonRpcProvider } from 'ethers'
2121import { useEffect , useState } from 'react'
2222
2323interface CensusCreationScreenProps {
2424 onBack : ( ) => void
25- onNext : ( censusId : string ) => void
25+ onNext : ( census : PlainCensus ) => void
2626}
2727
2828export default function CensusCreationScreen ( { onBack, onNext } : CensusCreationScreenProps ) {
2929 const [ addresses , setAddresses ] = useState < string [ ] > ( [ ] )
3030 const { walletMap, setWalletMap } = useWallets ( )
3131 const [ newAddress , setNewAddress ] = useState ( '' )
3232 const [ error , setError ] = useState < string | null > ( null )
33- const [ isLoading , setIsLoading ] = useState ( false )
3433 const [ censusCreated , setCensusCreated ] = useState ( false )
35- const [ progress , setProgress ] = useState ( 0 )
36- const [ censusId , setCensusId ] = useState < string | null > ( null )
34+ const [ census , setCensus ] = useState < PlainCensus | null > ( null )
3735
3836 useEffect ( ( ) => {
3937 // Generate initial 10 random wallets on component mount
@@ -82,86 +80,28 @@ export default function CensusCreationScreen({ onBack, onNext }: CensusCreationS
8280 }
8381 }
8482
85- const handleCreateCensus = async ( ) => {
83+ const handleCreateCensus = ( ) => {
8684 if ( addresses . length === 0 ) {
8785 setError ( 'Add at least one address to create a census' )
8886 return
8987 }
9088
9189 try {
92- setIsLoading ( true )
9390 setError ( null )
94- setProgress ( 0 )
9591
96- // Initialize DavinciSDK with first available wallet
97- const firstWallet = Object . values ( walletMap ) [ 0 ]
98- if ( ! firstWallet ) {
99- throw new Error ( 'No wallets available for SDK initialization' )
100- }
92+ // Create a PlainCensus object with all addresses
93+ const newCensus = new PlainCensus ( )
94+ newCensus . add ( addresses )
10195
102- // Check if wallet already has a provider (e.g., MetaMask)
103- // If not, connect it to the RPC provider from env
104- const wallet = firstWallet as Wallet
105- let signerWithProvider = wallet
106- if ( ! wallet . provider ) {
107- if ( ! import . meta. env . RPC_URL ) {
108- throw new Error ( 'RPC_URL environment variable is required' )
109- }
110- const provider = new JsonRpcProvider ( import . meta. env . RPC_URL )
111- signerWithProvider = wallet . connect ( provider )
112- }
113-
114- const sdk = new DavinciSDK ( {
115- signer : signerWithProvider ,
116- environment : 'dev' ,
117- sequencerUrl : import . meta. env . SEQUENCER_API_URL ,
118- censusUrl : import . meta. env . CENSUS_API_URL ,
119- chain : 'sepolia' ,
120- useSequencerAddresses : true
121- } )
122- await sdk . init ( )
123-
124- // Create census
125- setProgress ( 20 )
126- const newCensusId = await sdk . api . census . createCensus ( )
127- setCensusId ( newCensusId )
128-
129- // Add voters in batches
130- const batchSize = Math . ceil ( addresses . length / 4 ) // Split into 4 parts for progress
131- for ( let i = 0 ; i < addresses . length ; i += batchSize ) {
132- const batch = addresses . slice ( i , i + batchSize )
133-
134- // Add participants using addresses
135- await sdk . api . census . addParticipants (
136- newCensusId ,
137- batch . map ( ( address ) => ( {
138- key : address ,
139- weight : '1' , // All voters have equal weight
140- } ) )
141- )
142- setProgress ( 40 + Math . floor ( ( i / addresses . length ) * 40 ) )
143- }
144-
145- // Publish the census and store root & size locally
146- setProgress ( 90 )
147- const publishResult = await sdk . api . census . publishCensus ( newCensusId )
148- const censusRoot = publishResult . root
149- const censusSize = await sdk . api . census . getCensusSize ( censusRoot )
150-
151- // Store census details locally for the next screen
152- localStorage . setItem ( 'censusDetails' , JSON . stringify ( {
153- censusId : newCensusId ,
154- censusRoot,
155- censusSize
156- } ) )
157-
158- setProgress ( 100 )
96+ // Store census object for the next screen
97+ setCensus ( newCensus )
15998 setCensusCreated ( true )
99+
100+ console . log ( 'Census object created with' , addresses . length , 'addresses' )
101+ console . log ( 'Census will be automatically published when creating the process' )
160102 } catch ( err ) {
161103 setError ( err instanceof Error ? err . message : 'Failed to create census' )
162104 console . error ( 'Error creating census:' , err )
163- } finally {
164- setIsLoading ( false )
165105 }
166106 }
167107
@@ -179,13 +119,13 @@ export default function CensusCreationScreen({ onBack, onNext }: CensusCreationS
179119 < Card sx = { { mb : 4 } } >
180120 < CardContent >
181121 < Box sx = { { mb : 3 , display : 'flex' , gap : 2 , justifyContent : 'center' } } >
182- < Button variant = 'contained' onClick = { handleAddRandomWallet } startIcon = { < AddIcon /> } disabled = { isLoading } >
122+ < Button variant = 'contained' onClick = { handleAddRandomWallet } startIcon = { < AddIcon /> } disabled = { censusCreated } >
183123 Add Random Wallet
184124 </ Button >
185125 < Button
186126 variant = 'contained'
187127 onClick = { handleCreateCensus }
188- disabled = { addresses . length === 0 || isLoading || censusCreated }
128+ disabled = { addresses . length === 0 || censusCreated }
189129 >
190130 Create Census
191131 </ Button >
@@ -197,18 +137,17 @@ export default function CensusCreationScreen({ onBack, onNext }: CensusCreationS
197137 </ Alert >
198138 ) }
199139
200- { isLoading && (
201- < Box sx = { { mb : 2 , textAlign : 'center' } } >
202- < CircularProgress variant = 'determinate' value = { progress } sx = { { mb : 1 } } />
203- < Typography variant = 'body2' color = 'text.secondary' >
204- Creating census... { progress } %
205- </ Typography >
206- </ Box >
207- ) }
208-
209140 { censusCreated && (
210- < Alert severity = 'success' sx = { { mb : 2 } } >
211- Census created successfully!
141+ < Alert severity = 'success' sx = { { mb : 3 , py : 2 } } >
142+ < Typography variant = 'h6' gutterBottom >
143+ ✓ Census Created Successfully!
144+ </ Typography >
145+ < Typography variant = 'body2' gutterBottom >
146+ Census ready with { addresses . length } addresses. It will be automatically published when you create the process.
147+ </ Typography >
148+ < Typography variant = 'body1' sx = { { mt : 2 , fontWeight : 'bold' } } >
149+ 👉 Click "Next" below to continue to the next step
150+ </ Typography >
212151 </ Alert >
213152 ) }
214153
@@ -217,11 +156,11 @@ export default function CensusCreationScreen({ onBack, onNext }: CensusCreationS
217156 < ListItem
218157 key = { address }
219158 secondaryAction = {
220- ! censusCreated && (
159+ ! censusCreated ? (
221160 < IconButton edge = 'end' aria-label = 'delete' onClick = { ( ) => handleRemoveAddress ( address ) } >
222161 < DeleteIcon />
223162 </ IconButton >
224- )
163+ ) : null
225164 }
226165 >
227166 < ListItemText
@@ -261,11 +200,37 @@ export default function CensusCreationScreen({ onBack, onNext }: CensusCreationS
261200 </ Card >
262201
263202 < Box sx = { { display : 'flex' , gap : 2 , justifyContent : 'center' } } >
264- < Button variant = 'outlined' onClick = { onBack } disabled = { isLoading } >
203+ < Button variant = 'outlined' onClick = { onBack } disabled = { censusCreated } >
265204 Back
266205 </ Button >
267- < Button variant = 'contained' onClick = { ( ) => censusId && onNext ( censusId ) } disabled = { ! censusCreated || ! censusId } >
268- Next
206+ < Button
207+ variant = 'contained'
208+ size = { censusCreated ? 'large' : 'medium' }
209+ onClick = { ( ) => {
210+ if ( census ) {
211+ onNext ( census )
212+ }
213+ } }
214+ disabled = { ! censusCreated || ! census }
215+ sx = { censusCreated ? {
216+ fontSize : '1.1rem' ,
217+ py : 1.5 ,
218+ px : 4 ,
219+ animation : 'pulse 2s infinite' ,
220+ '@keyframes pulse' : {
221+ '0%' : {
222+ boxShadow : '0 0 0 0 rgba(43, 108, 176, 0.7)' ,
223+ } ,
224+ '70%' : {
225+ boxShadow : '0 0 0 10px rgba(43, 108, 176, 0)' ,
226+ } ,
227+ '100%' : {
228+ boxShadow : '0 0 0 0 rgba(43, 108, 176, 0)' ,
229+ } ,
230+ } ,
231+ } : { } }
232+ >
233+ { censusCreated ? '→ Next: Create Election' : 'Next' }
269234 </ Button >
270235 </ Box >
271236 </ Box >
0 commit comments