Skip to content

Commit 917e8cf

Browse files
ocshawnShawn O'Connor
andauthored
(VPODC-240): fix pagnation of SelectCohort, add story to test longer pagnation (#74)
* VPODC-240: fix pagnation of SelectCohort, add story to test longer pagnation * fix lint error --------- Co-authored-by: Shawn O'Connor <shawnoconnor@uchicago.edu>
1 parent bc258d1 commit 917e8cf

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/lib/AnalysisApps/SharedUtils/SelectCohort/SelectCohort.stories.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ const TestData = {
4545
}
4646
]
4747
};
48+
const TestDataLong = {
49+
cohort_definitions_and_stats: Array(77).fill({}).map((_, i)=>(
50+
{
51+
cohort_definition_id: i,
52+
cohort_name: `test name ${i}`,
53+
size: i * 1000,
54+
}
55+
))
56+
};
4857

4958
const TestSourcesData = {
5059
sources: [
@@ -97,6 +106,22 @@ export const GenericSelectCohortMockedSuccess: Story = {
97106
render: () => <SelectCohortWithHooks />, // see https://storybook.js.org/docs/writing-stories
98107
};
99108

109+
export const GenericSelectCohortMockedSuccessLong: Story = {
110+
parameters: {
111+
msw: {
112+
handlers: [
113+
http.get(CohortsEndpoint + '/:sourceId/by-team-project?team-project=:selectedTeamProject', async () => {
114+
return HttpResponse.json(TestDataLong);
115+
}),
116+
http.get(SourcesEndpoint, async () => {
117+
return HttpResponse.json(TestSourcesData);
118+
}),
119+
],
120+
},
121+
},
122+
render: () => <SelectCohortWithHooks />, // see https://storybook.js.org/docs/writing-stories
123+
};
124+
100125
export const GenericSelectCohortMockedError: Story = {
101126
parameters: {
102127
msw: {

src/lib/AnalysisApps/SharedUtils/SelectCohort/Utils/CohortDefinitions.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const CohortDefinitions: React.FC<CohortDefinitionsProps> = ({
3131
CohortsEndpoint + '/' + sourceId + `/by-team-project?team-project=${selectedTeamProject}`,
3232
(...args) => fetch(...args).then((res) => res.json()),
3333
);
34-
let displayedCohorts: cohort[] = useFilter(data?.['cohort_definitions_and_stats'], searchTerm, 'cohort_name');
34+
const displayedCohorts: cohort[] = useFilter(data?.['cohort_definitions_and_stats'], searchTerm, 'cohort_name');
3535

3636
if (error)
3737
return <React.Fragment>Error getting data for table</React.Fragment>;
@@ -43,17 +43,17 @@ const CohortDefinitions: React.FC<CohortDefinitionsProps> = ({
4343
</div>
4444
);
4545

46-
if (data) {
47-
displayedCohorts = displayedCohorts.slice(
46+
if (displayedCohorts) {
47+
const pageOfDisplayedCohorts = displayedCohorts.slice(
4848
(page - 1) * rowsPerPage,
4949
page * rowsPerPage,
5050
);
51-
const totalPagesForPagination = Math.ceil(data.length / rowsPerPage);
51+
const totalPagesForPagination = Math.ceil(displayedCohorts.length / rowsPerPage);
5252

5353
return (
5454
<React.Fragment>
5555
<div className="w-full min-h-[200px] py-5">
56-
{displayedCohorts?.length > 0 ? (
56+
{pageOfDisplayedCohorts?.length > 0 ? (
5757
<Table className="shadow">
5858
<Table.Thead className="bg-vadc-slate_blue font-light">
5959
<Table.Tr>
@@ -63,7 +63,7 @@ const CohortDefinitions: React.FC<CohortDefinitionsProps> = ({
6363
</Table.Tr>
6464
</Table.Thead>
6565
<Table.Tbody>
66-
{displayedCohorts.map((cohort, i) => (
66+
{pageOfDisplayedCohorts.map((cohort, i) => (
6767
<Table.Tr
6868
key={i}
6969
className={i % 2 ? 'bg-vadc-alternate_row' : ''}
@@ -111,7 +111,10 @@ const CohortDefinitions: React.FC<CohortDefinitionsProps> = ({
111111
<Select
112112
className="pt-5 pl-3 w-32"
113113
value={rowsPerPage.toString()}
114-
onChange={(value) => setRowsPerPage(Number(value))}
114+
onChange={(value) => {
115+
setRowsPerPage(Number(value));
116+
setPage(1);
117+
}}
115118
size="sm"
116119
aria-label="pagination select"
117120
data={[

0 commit comments

Comments
 (0)