Skip to content

Commit 0017d1d

Browse files
committed
feat(content-management): enhance categories pagination with dynamic row count and loading state
1 parent 67e94ab commit 0017d1d

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

lib/content_management/view/categories_page.dart

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:ht_dashboard/content_management/bloc/content_management_bloc.dar
66
import 'package:ht_dashboard/l10n/app_localizations.dart';
77
import 'package:ht_dashboard/l10n/l10n.dart';
88
import 'package:ht_dashboard/router/routes.dart';
9+
import 'package:ht_dashboard/shared/constants/pagination_constants.dart';
910
import 'package:ht_dashboard/shared/constants/app_spacing.dart';
1011
import 'package:ht_dashboard/shared/widgets/failure_state_widget.dart';
1112
import 'package:ht_dashboard/shared/widgets/loading_state_widget.dart';
@@ -23,14 +24,12 @@ class CategoriesPage extends StatefulWidget {
2324
}
2425

2526
class _CategoriesPageState extends State<CategoriesPage> {
26-
static const int _rowsPerPage = 10;
27-
2827
@override
2928
void initState() {
3029
super.initState();
3130
context.read<ContentManagementBloc>().add(
32-
const LoadCategoriesRequested(limit: _rowsPerPage),
33-
);
31+
const LoadCategoriesRequested(limit: kDefaultRowsPerPage),
32+
);
3433
}
3534

3635
@override
@@ -53,8 +52,8 @@ class _CategoriesPageState extends State<CategoriesPage> {
5352
return FailureStateWidget(
5453
message: state.errorMessage ?? l10n.unknownError,
5554
onRetry: () => context.read<ContentManagementBloc>().add(
56-
const LoadCategoriesRequested(limit: _rowsPerPage),
57-
),
55+
const LoadCategoriesRequested(limit: kDefaultRowsPerPage),
56+
),
5857
);
5958
}
6059

@@ -82,20 +81,21 @@ class _CategoriesPageState extends State<CategoriesPage> {
8281
source: _CategoriesDataSource(
8382
context: context,
8483
categories: state.categories,
84+
hasMore: state.categoriesHasMore,
8585
l10n: l10n,
8686
),
87-
rowsPerPage: _rowsPerPage,
88-
availableRowsPerPage: const [_rowsPerPage],
87+
rowsPerPage: kDefaultRowsPerPage,
88+
availableRowsPerPage: const [kDefaultRowsPerPage],
8989
onPageChanged: (pageIndex) {
90-
final newOffset = pageIndex * _rowsPerPage;
90+
final newOffset = pageIndex * kDefaultRowsPerPage;
9191
if (newOffset >= state.categories.length &&
9292
state.categoriesHasMore) {
9393
context.read<ContentManagementBloc>().add(
94-
LoadCategoriesRequested(
95-
startAfterId: state.categoriesCursor,
96-
limit: _rowsPerPage,
97-
),
98-
);
94+
LoadCategoriesRequested(
95+
startAfterId: state.categoriesCursor,
96+
limit: kDefaultRowsPerPage,
97+
),
98+
);
9999
}
100100
},
101101
empty: Center(child: Text(l10n.noCategoriesFound)),
@@ -117,16 +117,22 @@ class _CategoriesDataSource extends DataTableSource {
117117
_CategoriesDataSource({
118118
required this.context,
119119
required this.categories,
120+
required this.hasMore,
120121
required this.l10n,
121122
});
122123

123124
final BuildContext context;
124125
final List<Category> categories;
126+
final bool hasMore;
125127
final AppLocalizations l10n;
126128

127129
@override
128130
DataRow? getRow(int index) {
129131
if (index >= categories.length) {
132+
// This can happen if hasMore is true and the user is on the last page.
133+
// The table will try to build one extra row that is out of bounds.
134+
// We return null to signify the end of the available data.
135+
// The onPageChanged callback will handle fetching more data.
130136
return null;
131137
}
132138
final category = categories[index];
@@ -152,8 +158,8 @@ class _CategoriesDataSource extends DataTableSource {
152158
onPressed: () {
153159
// Dispatch delete event
154160
context.read<ContentManagementBloc>().add(
155-
DeleteCategoryRequested(category.id),
156-
);
161+
DeleteCategoryRequested(category.id),
162+
);
157163
},
158164
),
159165
],
@@ -164,10 +170,18 @@ class _CategoriesDataSource extends DataTableSource {
164170
}
165171

166172
@override
167-
bool get isRowCountApproximate => false;
173+
bool get isRowCountApproximate => true;
168174

169175
@override
170-
int get rowCount => categories.length;
176+
int get rowCount {
177+
// If we have more items to fetch, we add 1 to the current length.
178+
// This signals to PaginatedDataTable2 that there is at least one more page,
179+
// which enables the 'next page' button.
180+
if (hasMore) {
181+
return categories.length + 1;
182+
}
183+
return categories.length;
184+
}
171185

172186
@override
173187
int get selectedRowCount => 0;

0 commit comments

Comments
 (0)