1
1
import { useState , useCallback , useEffect } from 'react' ;
2
2
import { DateTime } from 'luxon' ;
3
- import { Address } from 'viem' ;
3
+ import { Address , Hash } from 'viem' ;
4
4
import {
5
- ClientDealsManager ,
6
5
DealRecord ,
7
6
DealStatus ,
8
7
} from '../../../../src/index.js' ; // @windingtree /sdk
8
+ import { ClientDealsManager } from '../../../../src/client/dealsManager.js' ;
9
9
import { RequestQuery , OfferOptions } from '../../../shared/index.js' ;
10
- import { centerEllipsis , formatBalance , parseWalletError } from '../utils.js' ;
11
- import { useWallet } from '../providers/WalletProvider/WalletProviderContext.js' ;
10
+ import {
11
+ centerEllipsis ,
12
+ formatBalance ,
13
+ parseWalletError ,
14
+ } from '../../../react-libs/src/utils/index.js' ;
15
+ import { useWallet } from '../../../react-libs/src/providers/WalletProvider/WalletProviderContext.js' ;
12
16
13
17
export type DealsRegistryRecord = Required <
14
18
DealRecord < RequestQuery , OfferOptions >
@@ -219,13 +223,31 @@ export const Cancel = ({ deal, manager, onClose }: CancelProps) => {
219
223
* Created deals table
220
224
*/
221
225
export const Deals = ( { deals, manager } : DealsProps ) => {
226
+ const { walletClient } = useWallet ( ) ;
222
227
const [ dealStates , setDealStates ] = useState < Record < string , DealStatus > > ( { } ) ;
223
228
const [ transferDeal , setTransferDeal ] = useState <
224
229
DealsRegistryRecord | undefined
225
230
> ( ) ;
226
231
const [ cancelDeal , setCancelDeal ] = useState <
227
232
DealsRegistryRecord | undefined
228
233
> ( ) ;
234
+ const [ userSign , setUserSign ] = useState < Hash | undefined > ( ) ;
235
+ const [ error , setError ] = useState < string | undefined > ( ) ;
236
+
237
+ const handleCheckInOut = useCallback ( async ( deal : DealsRegistryRecord ) => {
238
+ try {
239
+ if ( ! manager || ! walletClient ) {
240
+ throw new Error ( 'Wallet not connected yet' ) ;
241
+ }
242
+ setUserSign ( await manager . checkInOutSignature (
243
+ deal . offer . id ,
244
+ walletClient ,
245
+ ) ) ;
246
+ } catch ( error ) {
247
+ console . log ( error ) ;
248
+ setError ( ( error as Error ) . message || 'Unknown check in signature error' ) ;
249
+ }
250
+ } , [ manager , walletClient ] ) ;
229
251
230
252
useEffect ( ( ) => {
231
253
if ( deals && deals . length > 0 ) {
@@ -271,8 +293,8 @@ export const Deals = ({ deals, manager }: DealsProps) => {
271
293
{ DealStatus [ dealStates [ d . offer . id ] ] }
272
294
</ td >
273
295
< td >
274
- < div style = { { display : 'flex' , flexDirection : 'column' } } >
275
- < div style = { { marginBottom : 5 } } >
296
+ < div style = { { display : 'flex' , flexDirection : 'column' , gap : 5 } } >
297
+ < div >
276
298
< button
277
299
onClick = { ( ) => setCancelDeal ( d ) }
278
300
disabled = {
@@ -302,13 +324,22 @@ export const Deals = ({ deals, manager }: DealsProps) => {
302
324
Transfer
303
325
</ button >
304
326
</ div >
327
+ { d . status === DealStatus . Claimed &&
328
+ < div >
329
+ < button
330
+ onClick = { ( ) => handleCheckInOut ( d ) }
331
+ >
332
+ Check In
333
+ </ button >
334
+ </ div >
335
+ }
305
336
</ div >
306
337
</ td >
307
338
</ tr >
308
339
) ) }
309
340
</ tbody >
310
341
</ table >
311
- < div style = { { marginTop : 20 } } >
342
+ < div >
312
343
< TransferForm
313
344
deal = { transferDeal }
314
345
manager = { manager }
@@ -319,7 +350,17 @@ export const Deals = ({ deals, manager }: DealsProps) => {
319
350
manager = { manager }
320
351
onClose = { ( ) => setCancelDeal ( undefined ) }
321
352
/>
353
+ { userSign &&
354
+ < div style = { { marginTop : 20 } } >
355
+ < h2 > Provide this signature to the reception manager:</ h2 >
356
+ < input style = { { width : '100%' } } onFocus = { ( event ) => {
357
+ event . target . select ( ) ;
358
+ } } value = { userSign } onChange = { ( ) => { } } />
359
+ </ div >
360
+ }
322
361
</ div >
362
+
363
+ { error && < div style = { { marginTop : 20 } } > 🚨 { error } </ div > }
323
364
</ div >
324
365
) ;
325
366
} ;
0 commit comments