@@ -7,106 +7,109 @@ import { ButtonGroup, IconButton, Pagination } from "@chakra-ui/react";
77import { HiChevronLeft , HiChevronRight } from "react-icons/hi" ;
88import { ProcessGrantData } from "../filter-bar/processGrantData.ts" ;
99import NewGrantModal from "../new-grant/NewGrantModal.tsx" ;
10+ import { Grant } from "../../../../../middle-layer/types/Grant.ts" ;
1011
1112const ITEMS_PER_PAGE = 6 ;
1213
1314interface GrantListProps {
14- selectedGrantId ?: number ;
15- onClearSelectedGrant ?: ( ) => void ;
15+ selectedGrantId ?: number ;
16+ onClearSelectedGrant ?: ( ) => void ;
17+ showOnlyMyGrants ?: boolean ;
18+ currentUserEmail ?: string ;
1619}
1720
18- const GrantList : React . FC < GrantListProps > = observer (
19- ( { selectedGrantId, onClearSelectedGrant } ) => {
21+ const GrantList : React . FC < GrantListProps > = observer ( ( { selectedGrantId, onClearSelectedGrant, showOnlyMyGrants = false , currentUserEmail } ) => {
2022 const { grants, onSort } = ProcessGrantData ( ) ;
2123 const [ currentPage , setPage ] = useState ( 1 ) ;
2224 const [ showNewGrantModal , setShowNewGrantModal ] = useState ( false ) ;
2325
24- useEffect ( ( ) => {
25- if ( selectedGrantId !== undefined && grants . length > 0 ) {
26- const index = grants . findIndex (
27- ( grant ) => grant . grantId === Number ( selectedGrantId )
28- ) ;
29- if ( index !== - 1 ) {
30- const targetPage = Math . floor ( index / ITEMS_PER_PAGE ) + 1 ;
31- if ( targetPage !== currentPage ) {
32- setPage ( targetPage ) ;
33- }
34- }
35- }
36- } , [ selectedGrantId , grants , currentPage ] ) ;
26+ const displayedGrants = showOnlyMyGrants ? grants . filter (
27+ ( grant : Grant ) => grant . bcan_poc ?. POC_email ?. toLowerCase ( ) === currentUserEmail ?. toLowerCase ( )
28+ )
29+ : grants ;
3730
38- const count = grants . length ;
31+ useEffect ( ( ) => {
32+ if ( selectedGrantId !== undefined && grants . length > 0 ) {
33+ const index = grants . findIndex ( grant => grant . grantId === Number ( selectedGrantId ) ) ;
34+ if ( index !== - 1 ) {
35+ const targetPage = Math . floor ( index / ITEMS_PER_PAGE ) + 1 ;
36+ if ( targetPage !== currentPage ) {
37+ setPage ( targetPage ) ;
38+ }
39+ }
40+ }
41+ } , [ selectedGrantId , grants , currentPage ] ) ;
42+
43+ const count = displayedGrants . length ;
3944 const startRange = ( currentPage - 1 ) * ITEMS_PER_PAGE ;
4045 const endRange = startRange + ITEMS_PER_PAGE ;
41- const visibleItems = grants . slice ( startRange , endRange ) ;
46+ const visibleItems = displayedGrants . slice ( startRange , endRange ) ;
4247
4348 return (
44- < div className = "paginated-grant-list" >
45- < div className = "bg-light-orange rounded-[1.2rem] pt-2" >
46- < GrantLabels onSort = { onSort } />
47- < div className = "grant-list p-4" >
48- { visibleItems . map ( ( grant ) => (
49- < GrantItem
50- key = { grant . grantId }
51- grant = { grant }
52- defaultExpanded = { grant . grantId === Number ( selectedGrantId ) }
53- />
54- ) ) }
55- </ div >
49+ < div className = "paginated-grant-list" >
50+ < div className = "bg-light-orange rounded-[1.2rem] pt-2" >
51+ < GrantLabels onSort = { onSort } />
52+ < div className = "grant-list p-4" >
53+ { visibleItems . map ( ( grant ) => (
54+ < GrantItem key = { grant . grantId }
55+ grant = { grant }
56+ defaultExpanded = { grant . grantId === Number ( selectedGrantId ) } />
57+ ) ) }
58+ { visibleItems . length === 0 && (
59+ < p className = "text-center text-gray-500 py-6" >
60+ { showOnlyMyGrants
61+ ? "You currently have no grants assigned as BCAN POC."
62+ : "No grants found>" }
63+ </ p >
64+ ) }
65+ </ div >
66+ </ div >
67+ < Pagination . Root
68+ className = "pt-4"
69+ count = { count }
70+ pageSize = { ITEMS_PER_PAGE }
71+ page = { currentPage }
72+ onClick = { ( ) => {
73+ if ( onClearSelectedGrant ) { onClearSelectedGrant ( ) ; } } }
74+ onPageChange = { ( e ) => {
75+ setPage ( e . page ) ; } }
76+ >
77+ < ButtonGroup variant = "ghost" size = "md" >
78+ < Pagination . PrevTrigger asChild >
79+ < IconButton >
80+ < HiChevronLeft />
81+ </ IconButton >
82+ </ Pagination . PrevTrigger >
83+ < Pagination . Context >
84+ { ( { pages } ) =>
85+ pages . map ( ( page , index ) =>
86+ page . type === "page" ? (
87+ < IconButton
88+ key = { index }
89+ className = { currentPage === page . value ? "text-dark-blue underline" : "ghost" }
90+ onClick = { ( ) => setPage ( page . value ) }
91+ aria-label = { `Go to page ${ page . value } ` }
92+ >
93+ { page . value }
94+ </ IconButton >
95+ ) : (
96+ "..."
97+ )
98+ )
99+ }
100+ </ Pagination . Context >
101+ < Pagination . NextTrigger asChild >
102+ < IconButton >
103+ < HiChevronRight />
104+ </ IconButton >
105+ </ Pagination . NextTrigger >
106+ </ ButtonGroup >
107+ </ Pagination . Root >
108+ { showNewGrantModal && (
109+ < NewGrantModal onClose = { ( ) => setShowNewGrantModal ( false ) } />
110+ ) }
56111 </ div >
57- < Pagination . Root
58- className = "pt-4"
59- count = { count }
60- pageSize = { ITEMS_PER_PAGE }
61- page = { currentPage }
62- onClick = { ( ) => {
63- if ( onClearSelectedGrant ) {
64- onClearSelectedGrant ( ) ;
65- }
66- } }
67- onPageChange = { ( e ) => {
68- setPage ( e . page ) ;
69- } }
70- >
71- < ButtonGroup variant = "ghost" size = "md" >
72- < Pagination . PrevTrigger asChild >
73- < IconButton >
74- < HiChevronLeft />
75- </ IconButton >
76- </ Pagination . PrevTrigger >
77- < Pagination . Context >
78- { ( { pages } ) =>
79- pages . map ( ( page , index ) =>
80- page . type === "page" ? (
81- < IconButton
82- key = { index }
83- className = {
84- currentPage === page . value
85- ? "text-dark-blue underline"
86- : "ghost"
87- }
88- onClick = { ( ) => setPage ( page . value ) }
89- aria-label = { `Go to page ${ page . value } ` }
90- >
91- { page . value }
92- </ IconButton >
93- ) : (
94- "..."
95- )
96- )
97- }
98- </ Pagination . Context >
99- < Pagination . NextTrigger asChild >
100- < IconButton >
101- < HiChevronRight />
102- </ IconButton >
103- </ Pagination . NextTrigger >
104- </ ButtonGroup >
105- </ Pagination . Root >
106- { showNewGrantModal && (
107- < NewGrantModal onClose = { ( ) => setShowNewGrantModal ( false ) } />
108- ) }
109- </ div >
112+
110113 ) ;
111114 }
112115) ;
0 commit comments