@@ -24,7 +24,7 @@ import StarOutlineIcon from '@mui/icons-material/StarOutline';
24
24
import PieChartIcon from '@mui/icons-material/PieChart' ;
25
25
import MoreVertIcon from '@mui/icons-material/MoreVert' ;
26
26
import CloseIcon from '@mui/icons-material/Close' ;
27
- import { Sparklines , SparklinesLine , SparklinesReferenceLine } from 'react-sparklines' ;
27
+ import { Sparklines , SparklinesLine } from 'react-sparklines' ;
28
28
29
29
import {
30
30
Table ,
@@ -36,7 +36,7 @@ import {
36
36
Cell ,
37
37
} from '@table-library/react-table-library/table' ;
38
38
import { useTheme } from '@table-library/react-table-library/theme' ;
39
- import { DEFAULT_OPTIONS , getTheme } from '@table-library/react-table-library/themes/ material-ui' ;
39
+ import { DEFAULT_OPTIONS , getTheme } from '@table-library/react-table-library/material-ui' ;
40
40
import { usePagination } from '@table-library/react-table-library/pagination' ;
41
41
42
42
const queryClient = new QueryClient ( ) ;
@@ -291,6 +291,21 @@ const PRICE_CHANGE_PERCENTAGE = {
291
291
} ,
292
292
} ;
293
293
294
+ const CATEGORIES = {
295
+ cryptocurrencies : {
296
+ label : 'Cryptocurrencies' ,
297
+ } ,
298
+ 'decentralized-finance-defi' : {
299
+ label : 'DeFi' ,
300
+ } ,
301
+ 'non-fungible-tokens-nft' : {
302
+ label : 'NFT' ,
303
+ } ,
304
+ metaverse : {
305
+ label : 'Metaverse' ,
306
+ } ,
307
+ } ;
308
+
294
309
const CUSTOM_COLUMNS = {
295
310
ath : {
296
311
label : 'ATH' ,
@@ -326,6 +341,10 @@ const CUSTOM_COLUMNS = {
326
341
} ,
327
342
} ;
328
343
344
+ const DEFAULT_PAGE = 1 ;
345
+ const DEFAULT_SIZE = 10 ;
346
+ const DEFAULT_CATEGORY = 'cryptocurrencies' ;
347
+
329
348
const StyledSelect = styled ( Select ) `
330
349
& .MuiSelect-select {
331
350
padding-left: 0;
@@ -376,12 +395,18 @@ const ViewMarket = ({ marketData, item, isExpanded, onOpen, onClose }) => {
376
395
) ;
377
396
} ;
378
397
379
- const queryCurrencies = ( { page, size } ) => ( ) =>
380
- axios
398
+ const queryCurrencies = ( { page, size, category } ) => ( ) => {
399
+ let extra = '' ;
400
+ if ( category !== DEFAULT_CATEGORY ) {
401
+ extra = `category=${ category } &` ;
402
+ }
403
+
404
+ return axios
381
405
. get (
382
- `https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=${ size } &page=${ page } &sparkline=true&price_change_percentage=1h%2C24h%2C7d%2C14d%2C30d%2C200d%2C1y` ,
406
+ `https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&${ extra } order=market_cap_desc&per_page=${ size } &page=${ page } &sparkline=true&price_change_percentage=1h%2C24h%2C7d%2C14d%2C30d%2C200d%2C1y` ,
383
407
)
384
408
. then ( ( result ) => result . data ) ;
409
+ } ;
385
410
386
411
const queryMarkets = ( id ) => ( ) =>
387
412
axios
@@ -402,8 +427,6 @@ const Demo = () => {
402
427
const [ expandedMarketIds , setExpandedMarketIds ] = React . useState ( [ ] ) ;
403
428
404
429
const handleExpand = ( incomingId ) => {
405
- console . log ( expandedMarketIds , incomingId , isDropdownOpen ) ;
406
-
407
430
const id = incomingId ?? isDropdownOpen . id ;
408
431
409
432
if ( expandedMarketIds . includes ( id ) ) {
@@ -413,14 +436,6 @@ const Demo = () => {
413
436
}
414
437
} ;
415
438
416
- // search
417
-
418
- const [ search , setSearch ] = React . useState ( '' ) ;
419
-
420
- const handleSearch = ( event ) => {
421
- setSearch ( event . target . value ) ;
422
- } ;
423
-
424
439
// percentages
425
440
426
441
const [ percentageUnitOne , setPercentageUnitOne ] = React . useState (
@@ -457,14 +472,19 @@ const Demo = () => {
457
472
error : null ,
458
473
} ) ;
459
474
460
- const fetchCurrencies = async ( { page, size } , fetchState ) => {
475
+ const fetchCurrencies = async ( { page, size, category } , fetchState ) => {
461
476
setFetchState ( fetchState ) ;
462
- setCurrencies ( await queryClient . fetchQuery ( 'currencies' , queryCurrencies ( { page, size } ) ) ) ;
477
+ setCurrencies (
478
+ await queryClient . fetchQuery ( 'currencies' , queryCurrencies ( { page, size, category } ) ) ,
479
+ ) ;
463
480
setFetchState ( { isLoading : false , isOverlayLoading : false , error : null } ) ;
464
481
} ;
465
482
466
483
React . useEffect ( ( ) => {
467
- fetchCurrencies ( { page : 1 , size : 10 } , { isLoading : true } ) ;
484
+ fetchCurrencies (
485
+ { page : DEFAULT_PAGE , size : DEFAULT_SIZE , category : activeCategory } ,
486
+ { isLoading : true } ,
487
+ ) ;
468
488
} , [ ] ) ;
469
489
470
490
const marketsData = useQueries (
@@ -476,14 +496,27 @@ const Demo = () => {
476
496
477
497
const data = { nodes : isWatchlistActive ? favorites : currencies } ;
478
498
499
+ // category
500
+
501
+ const [ activeCategory , setActiveCategory ] = React . useState ( DEFAULT_CATEGORY ) ;
502
+
503
+ React . useEffect ( ( ) => {
504
+ pagination . fns . onSetPage ( DEFAULT_PAGE ) ;
505
+ pagination . fns . onSetSize ( 50 ) ; // TODO
506
+ fetchCurrencies (
507
+ { page : DEFAULT_PAGE , size : 50 , category : activeCategory } ,
508
+ { overlayLoading : true } ,
509
+ ) ;
510
+ } , [ activeCategory ] ) ;
511
+
479
512
// pagination
480
513
481
514
const pagination = usePagination (
482
515
data ,
483
516
{
484
517
state : {
485
- page : 0 ,
486
- size : 10 ,
518
+ page : DEFAULT_PAGE ,
519
+ size : DEFAULT_SIZE ,
487
520
} ,
488
521
onChange : onPaginationChange ,
489
522
} ,
@@ -493,7 +526,10 @@ const Demo = () => {
493
526
) ;
494
527
495
528
function onPaginationChange ( action , state ) {
496
- fetchCurrencies ( { page : state . page + 1 , size : state . size } , { isOverlayLoading : true } ) ;
529
+ fetchCurrencies (
530
+ { page : state . page + 1 , size : state . size , category : activeCategory } ,
531
+ { isOverlayLoading : true } ,
532
+ ) ;
497
533
}
498
534
499
535
// watchlist handler
@@ -507,8 +543,8 @@ const Demo = () => {
507
543
} ;
508
544
509
545
const handleWatchList = ( ) => {
510
- pagination . fns . onSetPage ( 1 ) ;
511
- pagination . fns . onSetSize ( 10 ) ;
546
+ pagination . fns . onSetPage ( DEFAULT_PAGE ) ;
547
+ pagination . fns . onSetSize ( DEFAULT_SIZE ) ;
512
548
setWatchlistActive ( ! isWatchlistActive ) ;
513
549
} ;
514
550
@@ -528,21 +564,15 @@ const Demo = () => {
528
564
Portfolio
529
565
</ Button >
530
566
< Divider orientation = "vertical" flexItem />
531
- < Button color = "primary" size = "small" onClick = { ( ) => { } } >
532
- Cryptocurrencies
533
- </ Button >
534
- < Button color = "inherit" size = "small" onClick = { ( ) => { } } >
535
- Categories
536
- </ Button >
537
- < Button color = "inherit" size = "small" onClick = { ( ) => { } } >
538
- DeFi
539
- </ Button >
540
- < Button color = "inherit" size = "small" onClick = { ( ) => { } } >
541
- NFT
542
- </ Button >
543
- < Button color = "inherit" size = "small" onClick = { ( ) => { } } >
544
- Metaverse
545
- </ Button >
567
+ { Object . keys ( CATEGORIES ) . map ( ( key ) => (
568
+ < Button
569
+ color = { key === activeCategory ? 'primary' : 'inherit' }
570
+ size = "small"
571
+ onClick = { ( ) => setActiveCategory ( key ) }
572
+ >
573
+ { CATEGORIES [ key ] . label }
574
+ </ Button >
575
+ ) ) }
546
576
</ Stack >
547
577
548
578
< Stack direction = "row" spacing = { 1 } m = { 1 } >
@@ -716,7 +746,6 @@ const Demo = () => {
716
746
< Cell >
717
747
< Sparklines data = { item . sparkline_in_7d . price } height = { 40 } >
718
748
< SparklinesLine color = "#1976d2" />
719
- < SparklinesReferenceLine type = "mean" />
720
749
</ Sparklines >
721
750
</ Cell >
722
751
{ customColumnsActive . map ( ( column ) => (
@@ -756,10 +785,12 @@ const Demo = () => {
756
785
{ fetchState . isOverlayLoading && < OverlayLoading /> }
757
786
758
787
< TablePagination
759
- count = { isWatchlistActive ? data . nodes . length : 9688 } // API does not offer this number
788
+ count = {
789
+ isWatchlistActive ? data . nodes . length : 9688 // TODO API does not offer this number
790
+ }
760
791
page = { pagination . state . page }
761
792
rowsPerPage = { pagination . state . size }
762
- rowsPerPageOptions = { [ 10 , 25 , 100 ] }
793
+ rowsPerPageOptions = { activeCategory === DEFAULT_CATEGORY ? [ 10 , 25 , 50 , 100 ] : [ 50 ] } // TODO
763
794
onRowsPerPageChange = { ( event ) => pagination . fns . onSetSize ( parseInt ( event . target . value , 10 ) ) }
764
795
onPageChange = { ( event , page ) => pagination . fns . onSetPage ( page ) }
765
796
/>
0 commit comments