Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add participants count to column in batch overview #118

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions packages/react-app/pages/admin/batches.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import BatchesListSkeleton from "../../components/skeletons/BatchesListSkeleton"

const serverPathBatches = "/batches";
const serverPathBatchGraduateBuilders = "/builders/batch-graduates";
const serverPathBatchParticipants = "/builders/batches";

const BatchColumnFilter = ({ column: { filterValue, setFilter } }) => {
const { baseColor } = useCustomColorModes();
Expand Down Expand Up @@ -63,6 +64,7 @@ export default function Batches({ serverUrl, userRole, mainnetProvider }) {
const [selectedBatch, setSelectedBatch] = useState(null);
const [amountBatches, setAmountBatches] = useState();
const [graduatesCount, setGraduatesCount] = useState({});
const [participantsCount, setParticipantsCount] = useState({});

const [isAddModalOpen, setIsAddModalOpen] = useState(false);

Expand Down Expand Up @@ -115,15 +117,38 @@ export default function Batches({ serverUrl, userRole, mainnetProvider }) {
setGraduatesCount(graduatesCounts);
}, [batches, serverUrl]);

const fetchParticipantsCount = useCallback(async () => {
const fetchedBatchParticipants = await axios.get(serverUrl + serverPathBatchParticipants);
const participantsCounts = {};

batches.forEach(
batch => {
const batchName = batch.batchName;
participantsCounts[batchName] = 0;

fetchedBatchParticipants.data.forEach(builder => {
if (builder.batch && builder.batch.number === String(batchName)) {
participantsCounts[batchName]++;
}
});
},
[batches],
);

setParticipantsCount(participantsCounts);
}, [batches, serverUrl]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, this nested loop is ugly haha

We are also making 3 requests to the server to render this page, which doesn't make sense.

Let's update the backend as needed so we don't need to do this. We could:

  • Extend the /batches endpoint so it comes back with the count calculations
    or
  • Create a new /batches/stats route that includes this data

So we'll only need to do 1 request. Also will be helpful here BuidlGuidl/buidlguidl.com#54 (comment)


useEffect(() => {
fetchBatches();
}, [fetchBatches]);

useEffect(() => {
if (batches.length > 0) {
fetchGraduatesCount();
Promise.all([fetchGraduatesCount(), fetchParticipantsCount()]).catch(error => {
console.error("Error fetching counts:", error);
});
}
}, [batches, fetchGraduatesCount]);
}, [batches, fetchGraduatesCount, fetchParticipantsCount]);

const BatchNameCellComponent = ({ row }) => (
<BatchNameCell batch={row.original.batchName} status={row.original.status} />
Expand All @@ -133,9 +158,14 @@ export default function Batches({ serverUrl, userRole, mainnetProvider }) {
};
const BatchLinksCellComponent = ({ value }) => <BatchLinksCell batch={value} />;
const BatchStatusCellComponent = ({ value }) => <BatchStatusCell status={value} />;
const BatchGraduatesCellComponent = ({ row }) => {
const count = graduatesCount[row.original.batchName] || 0;
return <Text>{count}</Text>;
const BatchBuildersCountCellComponent = ({ row }) => {
const gradCount = graduatesCount[row.original.batchName] || 0;
const partCount = participantsCount[row.original.batchName] || 0;
return (
<Text textAlign="center">
{gradCount} / {partCount} {" "}
</Text>
);
};

const BatchEditComponent = ({ row }) => (
Expand Down Expand Up @@ -183,10 +213,11 @@ export default function Batches({ serverUrl, userRole, mainnetProvider }) {
Cell: BatchCreatedCellComponent,
},
{
Header: "Graduates",
Header: "Graduates / Participants",
accessor: row => graduatesCount[row.batchName] || 0,
disableFilters: true,
Cell: BatchGraduatesCellComponent,
Cell: BatchBuildersCountCellComponent,
headerAlign: "center",
},
{
Header: "Links",
Expand Down Expand Up @@ -280,7 +311,12 @@ export default function Batches({ serverUrl, userRole, mainnetProvider }) {
{headerGroups.map((headerGroup, index) => (
<Tr {...headerGroup.getHeaderGroupProps()} key={index}>
{headerGroup.headers.map(column => (
<Th {...column.getHeaderProps(column.getSortByToggleProps())} key={column.id} whiteSpace="nowrap">
<Th
{...column.getHeaderProps(column.getSortByToggleProps())}
key={column.id}
whiteSpace="nowrap"
textAlign={column.headerAlign || "left"}
>
{column.render("Header")}
<chakra.span key={`span-${index}`} pl="4">
{column.isSorted ? (
Expand All @@ -302,7 +338,7 @@ export default function Batches({ serverUrl, userRole, mainnetProvider }) {
return (
<Tr {...row.getRowProps()} key={row.id}>
{row.cells.map(cell => (
<Td {...cell.getCellProps()} key={cell.column.id}>
<Td {...cell.getCellProps()} key={cell.column.id} textAlign={cell.column.align || "left"}>
{cell.render("Cell")}
</Td>
))}
Expand Down