Skip to content

Commit

Permalink
add fetch all button
Browse files Browse the repository at this point in the history
  • Loading branch information
bigabig committed Oct 18, 2024
1 parent 0e9f112 commit ea9e47c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
19 changes: 8 additions & 11 deletions frontend/src/components/tableSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Draft, PayloadAction, createSelector } from "@reduxjs/toolkit";
import {
MRT_ColumnSizingState,
MRT_DensityState,
MRT_PaginationState,
MRT_RowSelectionState,
MRT_SortingState,
MRT_VisibilityState,
Expand All @@ -11,24 +10,21 @@ import {
export interface TableState {
searchQuery?: string;
rowSelectionModel: MRT_RowSelectionState;
paginationModel: MRT_PaginationState;
sortingModel: MRT_SortingState;
columnVisibilityModel: MRT_VisibilityState;
columnSizingModel: MRT_ColumnSizingState;
gridDensityModel: MRT_DensityState;
fetchSize: number;
}

export const initialTableState: TableState = {
// project state:
searchQuery: "",
rowSelectionModel: {},
paginationModel: {
pageIndex: 0,
pageSize: 10,
},
sortingModel: [],
columnVisibilityModel: {},
columnSizingModel: {},
fetchSize: 20,
// app state:
gridDensityModel: "comfortable",
};
Expand All @@ -37,6 +33,7 @@ export const tableReducer = {
// query
onSearchQueryChange: (state: Draft<TableState>, action: PayloadAction<string | undefined>) => {
state.searchQuery = action.payload;
state.fetchSize = initialTableState.fetchSize;
},
// selection
onRowSelectionChange: (state: Draft<TableState>, action: PayloadAction<MRT_RowSelectionState>) => {
Expand All @@ -45,10 +42,6 @@ export const tableReducer = {
onClearRowSelection: (state: Draft<TableState>) => {
state.rowSelectionModel = {};
},
// pagination
onPaginationChange: (state: Draft<TableState>, action: PayloadAction<MRT_PaginationState>) => {
state.paginationModel = action.payload;
},
// sorting
onSortChange: (state: Draft<TableState>, action: PayloadAction<MRT_SortingState>) => {
state.sortingModel = action.payload;
Expand All @@ -65,16 +58,20 @@ export const tableReducer = {
onGridDensityChange: (state: Draft<TableState>, action: PayloadAction<MRT_DensityState>) => {
state.gridDensityModel = action.payload;
},
// fetch sizse
onFetchSizeChange: (state: Draft<TableState>, action: PayloadAction<number>) => {
state.fetchSize = action.payload;
},
};

// reset table state
export const resetProjectTableState = (state: Draft<TableState>) => {
state.searchQuery = initialTableState.searchQuery;
state.rowSelectionModel = initialTableState.rowSelectionModel;
state.paginationModel = initialTableState.paginationModel;
state.sortingModel = initialTableState.sortingModel;
state.columnVisibilityModel = initialTableState.columnVisibilityModel;
state.columnSizingModel = initialTableState.columnSizingModel;
state.fetchSize = initialTableState.fetchSize;
};

// selectors
Expand Down
16 changes: 14 additions & 2 deletions frontend/src/views/search/DocumentSearch/SearchDocumentTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Card, Toolbar, Typography } from "@mui/material";
import { Box, Button, Card, Divider, Stack, Toolbar, Typography } from "@mui/material";
import { useInfiniteQuery } from "@tanstack/react-query";
import parse from "html-react-parser";
import {
Expand Down Expand Up @@ -42,7 +42,6 @@ import { SearchActions } from "./searchSlice.ts";
// this has to match Search.tsx!
const filterStateSelector = (state: RootState) => state.searchFilter;
const filterName = "root";
const fetchSize = 20;

const flatMapData = (page: PaginatedElasticSearchDocumentHits) => page.hits;

Expand Down Expand Up @@ -152,6 +151,7 @@ function SearchDocumentTable({ projectId }: DocumentTableProps) {
}, [tableInfo, user]);

// search
const fetchSize = useAppSelector((state) => state.search.fetchSize);
const filter = useAppSelector((state) => state.searchFilter.filter[filterName]);
const { data, fetchNextPage, isError, isFetching, isLoading } = useInfiniteQuery<PaginatedElasticSearchDocumentHits>({
queryKey: [
Expand All @@ -160,6 +160,7 @@ function SearchDocumentTable({ projectId }: DocumentTableProps) {
searchQuery, // refetch when searchQuery changes
filter, // refetch when columnFilters changes
sortingModel, // refetch when sorting changes
fetchSize,
],
queryFn: ({ pageParam }) =>
SearchService.searchSdocs({
Expand Down Expand Up @@ -335,6 +336,17 @@ function SearchDocumentTable({ projectId }: DocumentTableProps) {
style={{ flexGrow: 1 }}
onScroll={(event) => fetchMoreOnScroll(event.target as HTMLDivElement)}
/>
<Box sx={{ p: 1 }}>
<Divider />
<Stack direction="row" alignItems="top" pt={0.5}>
<Typography variant="body2" color="textSecondary" pt={0.5} mr={1}>
Fetched {totalFetched} of {totalResults} documents
</Typography>
<Button size="small" onClick={() => dispatch(SearchActions.onFetchSizeChange(totalResults))}>
Fetch All
</Button>
</Stack>
</Box>
</Card>
</>
);
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/views/search/DocumentSearch/searchSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ export const searchSlice = createSlice({
};
}
}, {});
});
})
.addMatcher(
(action) => action.type.startsWith("searchFilter"),
(state) => {
console.log("SearchFilterActions dispatched! Resetting fetchSize.");
state.fetchSize = initialTableState.fetchSize;
},
);
},
});

Expand Down

0 comments on commit ea9e47c

Please sign in to comment.