Skip to content

Commit 9d90006

Browse files
authored
Fixed sorting bug and renamed index to GrantList (#245)
* Fixed sorting bug and renamed index to GrantList * Removing unused imports * Fixing page sorting interaction bug
1 parent 1c0f330 commit 9d90006

File tree

7 files changed

+50
-36
lines changed

7 files changed

+50
-36
lines changed

frontend/src/external/bcanSatchel/actions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export const updateYearFilter = action(
6060
(yearFilter: number[] | []) => ({ yearFilter })
6161
);
6262

63+
export const updateSort = action(
64+
"updateSort",
65+
(sort: {header: keyof Grant, asc: boolean}) => ({ sort, })
66+
);
67+
6368
/**
6469
* Append a new grant to the current list of grants.
6570
*/

frontend/src/external/bcanSatchel/mutators.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
updateStartDateFilter, updateEndDateFilter,
99
updateSearchQuery,
1010
updateYearFilter,
11-
setNotifications
11+
setNotifications,
12+
updateSort
1213
} from './actions';
1314
import { getAppStore, persistToSessionStorage } from './store';
1415
import { setActiveUsers, setInactiveUsers } from './actions';
@@ -111,3 +112,8 @@ mutator(setNotifications, (actionMessage) => {
111112
const store = getAppStore();
112113
store.notifications = actionMessage.notifications;
113114
})
115+
116+
mutator(updateSort, (actionMessage) => {
117+
const store = getAppStore();
118+
store.sort = actionMessage.sort;
119+
})

frontend/src/external/bcanSatchel/store.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface AppState {
1717
yearFilter:number[] | [];
1818
activeUsers: User[] | [];
1919
inactiveUsers: User[] | [];
20+
sort: {header: keyof Grant, asc: boolean} | null;
2021
notifications: Notification[];
2122
}
2223

@@ -33,7 +34,8 @@ const initialState: AppState = {
3334
yearFilter: [],
3435
activeUsers: [],
3536
inactiveUsers: [],
36-
notifications: []
37+
notifications: [],
38+
sort: null,
3739
};
3840

3941
/**

frontend/src/main-page/grants/GrantPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "./styles/GrantPage.css";
2-
import GrantList from "./grant-list/index.tsx";
2+
import GrantList from "./grant-list/GrantList.tsx";
33

44
import AddGrantButton from "./new-grant/AddGrant.tsx";
55
import GrantSearch from "./filter-bar/GrantSearch.tsx";

frontend/src/main-page/grants/filter-bar/processGrantData.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
filterGrants,
88
yearFilterer,
99
statusFilter,
10-
searchFilter
10+
searchFilter,
1111
} from "./grantFilters";
1212
import { sortGrants } from "./grantSorter.ts";
1313
import { api } from "../../../api.ts";
@@ -29,25 +29,32 @@ export const fetchGrants = async () => {
2929
// contains callbacks for sorting and filtering grants
3030
// stores state for list of grants/filter
3131
export const ProcessGrantData = () => {
32-
const { allGrants, filterStatus, startDateFilter, endDateFilter, yearFilter, searchQuery } = getAppStore();
32+
const {
33+
allGrants,
34+
filterStatus,
35+
startDateFilter,
36+
endDateFilter,
37+
yearFilter,
38+
searchQuery,
39+
sort,
40+
} = getAppStore();
3341

3442
// fetch grants on mount if empty
3543
useEffect(() => {
3644
if (allGrants.length === 0) fetchGrants();
3745
}, [allGrants.length]);
3846

3947
// compute filtered grants dynamically — no useState needed
40-
const filteredGrants = filterGrants(allGrants, [
48+
const baseFiltered = filterGrants(allGrants, [
4149
statusFilter(filterStatus),
4250
dateRangeFilter(startDateFilter, endDateFilter),
4351
yearFilterer(yearFilter),
44-
searchFilter(searchQuery)
52+
searchFilter(searchQuery),
4553
]);
4654

47-
// sorting callback
48-
const onSort = (header: keyof Grant, asc: boolean) => {
49-
return sortGrants(filteredGrants, header, asc);
50-
};
55+
const filteredGrants = sort
56+
? sortGrants(baseFiltered, sort.header, sort.asc)
57+
: baseFiltered;
5158

52-
return { grants: filteredGrants, onSort };
59+
return { grants: filteredGrants };
5360
};

frontend/src/main-page/grants/grant-list/GrantLabels.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import React from "react";
21
import "../styles/GrantLabels.css";
32
import { useState } from "react";
43
import { Grant } from "../../../../../middle-layer/types/Grant";
4+
import { updateSort } from "../../../external/bcanSatchel/actions";
55

6-
const GrantLabels: React.FC<{
7-
onSort: (header: keyof Grant, asc: boolean) => void;
8-
}> = ({ onSort }) => {
6+
const GrantLabels = () => {
97
const [labels, setLabels] = useState({
108
header: "applicationDate",
119
asc: true,
1210
});
1311

1412
function buttonHandler(header: keyof Grant) {
1513
const isAsc = labels.header == header ? !labels.asc : true;
16-
onSort(header, isAsc);
14+
updateSort({header, asc: isAsc});
1715
setLabels({ header: header, asc: isAsc });
1816
}
1917

frontend/src/main-page/grants/grant-list/index.tsx renamed to frontend/src/main-page/grants/grant-list/GrantList.tsx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import "../styles/GrantList.css";
22
import { observer } from "mobx-react-lite";
33
import { useState, useEffect } from "react";
4-
import GrantItem from "./GrantItem.tsx";
5-
import GrantLabels from "./GrantLabels.tsx";
4+
import GrantItem from "./GrantItem";
5+
import GrantLabels from "./GrantLabels";
66
import { ButtonGroup, IconButton, Pagination } from "@chakra-ui/react";
77
import { HiChevronLeft, HiChevronRight } from "react-icons/hi";
8-
import { ProcessGrantData } from "../filter-bar/processGrantData.ts";
9-
import NewGrantModal from "../new-grant/NewGrantModal.tsx";
10-
import { Grant } from "../../../../../middle-layer/types/Grant.ts";
8+
import { ProcessGrantData } from "../filter-bar/processGrantData";
9+
import NewGrantModal from "../new-grant/NewGrantModal";
10+
import { Grant } from "../../../../../middle-layer/types/Grant";
11+
import { getAppStore } from '../../../external/bcanSatchel/store';
1112

1213
const ITEMS_PER_PAGE = 6;
1314

@@ -25,25 +26,20 @@ const GrantList: React.FC<GrantListProps> = observer(
2526
showOnlyMyGrants = false,
2627
currentUserEmail,
2728
}) => {
28-
const { grants, onSort } = ProcessGrantData();
29+
const { grants } = ProcessGrantData();
30+
const {filterStatus} = getAppStore();
2931
const [currentPage, setPage] = useState(1);
3032
const [showNewGrantModal, setShowNewGrantModal] = useState(false);
3133
// @ts-ignore
3234
const [wasGrantSubmitted, setWasGrantSubmitted] = useState(false);
33-
const [sortedGrants, setSortedGrants] = useState(grants);
34-
35-
// const handleSort = (header: keyof Grant, asc: boolean) => {
36-
// const sorted = onSort(header, asc);
37-
// setSortedGrants(sorted.length > 0 ? sorted : grants);
38-
// };
3935

4036
const displayedGrants = showOnlyMyGrants
41-
? sortedGrants.filter(
37+
? grants.filter(
4238
(grant: Grant) =>
4339
grant.bcan_poc?.POC_email?.toLowerCase() ===
4440
currentUserEmail?.toLowerCase()
4541
)
46-
: sortedGrants;
42+
: grants;
4743

4844
useEffect(() => {
4945
if (selectedGrantId !== undefined && grants.length > 0) {
@@ -57,11 +53,11 @@ const GrantList: React.FC<GrantListProps> = observer(
5753
}
5854
}
5955
}
60-
}, [selectedGrantId, grants, currentPage, sortedGrants]);
56+
}, [selectedGrantId, grants, currentPage]);
6157

6258
useEffect(() => {
63-
setSortedGrants(sortedGrants.length > 0 ? sortedGrants : grants);
64-
}, [grants]);
59+
setPage(1);
60+
},[filterStatus, showOnlyMyGrants]);
6561

6662
const count = displayedGrants.length;
6763
const startRange = (currentPage - 1) * ITEMS_PER_PAGE;
@@ -71,7 +67,7 @@ const GrantList: React.FC<GrantListProps> = observer(
7167
return (
7268
<div className="paginated-grant-list">
7369
<div className="bg-light-orange rounded-[1.2rem] pt-2">
74-
<GrantLabels onSort={onSort} />
70+
<GrantLabels/>
7571
<div className="grant-list p-4">
7672
{visibleItems.map((grant) => (
7773
<GrantItem key={grant.grantId}
@@ -82,7 +78,7 @@ const GrantList: React.FC<GrantListProps> = observer(
8278
<p className="text-center text-gray-500 py-6">
8379
{showOnlyMyGrants
8480
? "You currently have no grants assigned as BCAN POC."
85-
: "No grants found>"}
81+
: "No grants found :("}
8682
</p>
8783
)}
8884
</div>

0 commit comments

Comments
 (0)