@@ -67,16 +67,55 @@ export function RoundInCart(
67
67
] ) ;
68
68
69
69
const estimate = matchingEstimatesToText ( matchingEstimates ) ;
70
+ const [ gitcoinEnabled , setGitcoinEnabled ] = React . useState ( false ) ;
71
+ const [ gitcoinAmount , setGitcoinAmount ] = React . useState ( "" ) ;
72
+ const [ gitcoinDonationType , setGitcoinDonationType ] = React . useState <
73
+ "percentage" | "amount"
74
+ > ( "percentage" ) ;
70
75
71
- const totalDonationInUSD =
76
+ const baseDonationInUSD =
72
77
props . roundCart . reduce ( ( acc , proj ) => acc + Number ( proj . amount ) , 0 ) *
73
78
props . payoutTokenPrice ;
74
79
80
+ // Helper function to convert between percentage and amount
81
+ const convertAmount = (
82
+ value : string ,
83
+ fromType : "percentage" | "amount" ,
84
+ toType : "percentage" | "amount"
85
+ ) => {
86
+ const numValue = Number ( value ) || 0 ;
87
+ if ( fromType === toType ) return value ;
88
+ if ( fromType === "percentage" && toType === "amount" ) {
89
+ return (
90
+ ( baseDonationInUSD * numValue ) /
91
+ ( 100 * props . payoutTokenPrice )
92
+ ) . toFixed ( 6 ) ;
93
+ } else {
94
+ return (
95
+ ( numValue * props . payoutTokenPrice * 100 ) /
96
+ baseDonationInUSD
97
+ ) . toFixed ( 2 ) ;
98
+ }
99
+ } ;
100
+
101
+ // Update the donation calculation
102
+ const gitcoinDonationInUSD = gitcoinEnabled
103
+ ? gitcoinDonationType === "percentage"
104
+ ? ( baseDonationInUSD * ( Number ( gitcoinAmount ) || 0 ) ) / 100
105
+ : ( Number ( gitcoinAmount ) || 0 ) * props . payoutTokenPrice
106
+ : 0 ;
107
+
75
108
const showMatchingEstimate =
76
109
matchingEstimateError === undefined &&
77
110
matchingEstimates !== undefined &&
78
111
round ?. chainId !== 43114 ; // Avalanche
79
112
113
+ React . useEffect ( ( ) => {
114
+ if ( ! gitcoinEnabled ) {
115
+ setGitcoinAmount ( "" ) ;
116
+ }
117
+ } , [ gitcoinEnabled ] ) ;
118
+
80
119
return (
81
120
< div className = "my-4" >
82
121
{ /* Round In Cart */ }
@@ -128,6 +167,61 @@ export function RoundInCart(
128
167
</ div >
129
168
) ;
130
169
} ) }
170
+
171
+ { /* Gitcoin Donation Section */ }
172
+ < div className = "mt-4 p-4 border-t border-gray-200" >
173
+ < div className = "flex items-center justify-end space-x-2" >
174
+ < input
175
+ type = "checkbox"
176
+ id = "gitcoinDonation"
177
+ className = "rounded border-gray-300"
178
+ checked = { gitcoinEnabled }
179
+ onChange = { ( e ) => setGitcoinEnabled ( e . target . checked ) }
180
+ />
181
+ < label htmlFor = "gitcoinDonation" className = "text-sm font-medium" >
182
+ Support Gitcoin with an additional donation
183
+ </ label >
184
+ </ div >
185
+
186
+ { gitcoinEnabled && (
187
+ < div className = "mt-2 flex items-center justify-end space-x-2" >
188
+ < input
189
+ type = "number"
190
+ min = "0"
191
+ max = { gitcoinDonationType === "percentage" ? 1000 : undefined }
192
+ value = { gitcoinAmount }
193
+ onChange = { ( e ) => {
194
+ setGitcoinAmount ( e . target . value ) ;
195
+ } }
196
+ className = "w-20 px-2 py-1 border rounded text-right"
197
+ placeholder = "0"
198
+ step = "any"
199
+ />
200
+ < select
201
+ value = { gitcoinDonationType }
202
+ onChange = { ( e ) => {
203
+ const newType = e . target . value as "percentage" | "amount" ;
204
+ const newAmount = convertAmount (
205
+ gitcoinAmount ,
206
+ gitcoinDonationType ,
207
+ newType
208
+ ) ;
209
+ setGitcoinDonationType ( newType ) ;
210
+ setGitcoinAmount ( newAmount ) ;
211
+ } }
212
+ className = "px-2 py-1 border rounded text-sm w-40"
213
+ >
214
+ < option value = "percentage" > % of donation</ option >
215
+ < option value = "amount" >
216
+ { props . selectedPayoutToken . code }
217
+ </ option >
218
+ </ select >
219
+ < span className = "text-sm text-gray-500" >
220
+ (${ gitcoinDonationInUSD . toFixed ( 2 ) } )
221
+ </ span >
222
+ </ div >
223
+ ) }
224
+ </ div >
131
225
</ div >
132
226
</ div >
133
227
{ /* Total Donations */ }
@@ -167,9 +261,15 @@ export function RoundInCart(
167
261
< div className = "font-semibold" >
168
262
< p >
169
263
< span className = "mr-2" > Total donation</ span > $
170
- { isNaN ( totalDonationInUSD )
264
+ { isNaN ( baseDonationInUSD )
171
265
? "0.0"
172
- : totalDonationInUSD . toFixed ( 2 ) }
266
+ : baseDonationInUSD . toFixed ( 2 ) }
267
+ { gitcoinEnabled && gitcoinAmount !== "" && (
268
+ < span className = "text-sm text-gray-500 ml-2" >
269
+ (plus an additional ${ gitcoinDonationInUSD . toFixed ( 2 ) } { " " }
270
+ Gitcoin donation)
271
+ </ span >
272
+ ) }
173
273
</ p >
174
274
</ div >
175
275
</ div >
0 commit comments