Skip to content

Commit 5e906da

Browse files
committed
feat(content-management): update LoadHeadlinesRequested to use kDefaultRowsPerPage and improve pagination handling
1 parent 2d7f111 commit 5e906da

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

lib/content_management/view/headlines_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'; // Corrected import
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/utils/date_formatter.dart';
1112
import 'package:ht_dashboard/shared/widgets/failure_state_widget.dart';
@@ -24,14 +25,12 @@ class HeadlinesPage extends StatefulWidget {
2425
}
2526

2627
class _HeadlinesPageState extends State<HeadlinesPage> {
27-
static const int _rowsPerPage = 10;
28-
2928
@override
3029
void initState() {
3130
super.initState();
3231
context.read<ContentManagementBloc>().add(
33-
const LoadHeadlinesRequested(limit: _rowsPerPage),
34-
);
32+
const LoadHeadlinesRequested(limit: kDefaultRowsPerPage),
33+
);
3534
}
3635

3736
@override
@@ -54,8 +53,8 @@ class _HeadlinesPageState extends State<HeadlinesPage> {
5453
return FailureStateWidget(
5554
message: state.errorMessage ?? l10n.unknownError,
5655
onRetry: () => context.read<ContentManagementBloc>().add(
57-
const LoadHeadlinesRequested(limit: _rowsPerPage),
58-
),
56+
const LoadHeadlinesRequested(limit: kDefaultRowsPerPage),
57+
),
5958
);
6059
}
6160

@@ -87,20 +86,21 @@ class _HeadlinesPageState extends State<HeadlinesPage> {
8786
source: _HeadlinesDataSource(
8887
context: context,
8988
headlines: state.headlines,
89+
hasMore: state.headlinesHasMore,
9090
l10n: l10n,
9191
),
92-
rowsPerPage: _rowsPerPage,
93-
availableRowsPerPage: const [_rowsPerPage],
92+
rowsPerPage: kDefaultRowsPerPage,
93+
availableRowsPerPage: const [kDefaultRowsPerPage],
9494
onPageChanged: (pageIndex) {
95-
final newOffset = pageIndex * _rowsPerPage;
95+
final newOffset = pageIndex * kDefaultRowsPerPage;
9696
if (newOffset >= state.headlines.length &&
9797
state.headlinesHasMore) {
9898
context.read<ContentManagementBloc>().add(
99-
LoadHeadlinesRequested(
100-
startAfterId: state.headlinesCursor,
101-
limit: _rowsPerPage,
102-
),
103-
);
99+
LoadHeadlinesRequested(
100+
startAfterId: state.headlinesCursor,
101+
limit: kDefaultRowsPerPage,
102+
),
103+
);
104104
}
105105
},
106106
empty: Center(child: Text(l10n.noHeadlinesFound)),
@@ -122,16 +122,22 @@ class _HeadlinesDataSource extends DataTableSource {
122122
_HeadlinesDataSource({
123123
required this.context,
124124
required this.headlines,
125+
required this.hasMore,
125126
required this.l10n,
126127
});
127128

128129
final BuildContext context;
129130
final List<Headline> headlines;
131+
final bool hasMore;
130132
final AppLocalizations l10n;
131133

132134
@override
133135
DataRow? getRow(int index) {
134136
if (index >= headlines.length) {
137+
// This can happen if hasMore is true and the user is on the last page.
138+
// The table will try to build one extra row that is out of bounds.
139+
// We return null to signify the end of the available data.
140+
// The onPageChanged callback will handle fetching more data.
135141
return null;
136142
}
137143
final headline = headlines[index];
@@ -164,8 +170,8 @@ class _HeadlinesDataSource extends DataTableSource {
164170
onPressed: () {
165171
// Dispatch delete event
166172
context.read<ContentManagementBloc>().add(
167-
DeleteHeadlineRequested(headline.id),
168-
);
173+
DeleteHeadlineRequested(headline.id),
174+
);
169175
},
170176
),
171177
],
@@ -176,10 +182,18 @@ class _HeadlinesDataSource extends DataTableSource {
176182
}
177183

178184
@override
179-
bool get isRowCountApproximate => false;
185+
bool get isRowCountApproximate => true;
180186

181187
@override
182-
int get rowCount => headlines.length;
188+
int get rowCount {
189+
// If we have more items to fetch, we add 1 to the current length.
190+
// This signals to PaginatedDataTable2 that there is at least one more page,
191+
// which enables the 'next page' button.
192+
if (hasMore) {
193+
return headlines.length + 1;
194+
}
195+
return headlines.length;
196+
}
183197

184198
@override
185199
int get selectedRowCount => 0;

0 commit comments

Comments
 (0)