Skip to content

Commit 513012a

Browse files
committed
refactor(content): migrate topics page to use topic model
Refactors the `topics_page.dart` file to fully align with the `Topic` data model and its associated BLoC events and state. - Replaces all `Category`-related variables, states, and events with their `Topic` equivalents. - Updates the `_TopicsDataSource` to work with `List<Topic>`. - Corrects the `DataCell` for the last updated date to handle the non-nullable `updatedAt` field from the `Topic` model, removing an unnecessary null check.
1 parent 887f3e3 commit 513012a

File tree

2 files changed

+37
-42
lines changed

2 files changed

+37
-42
lines changed

lib/content_management/view/content_management_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
22
import 'package:flutter_bloc/flutter_bloc.dart';
33
import 'package:go_router/go_router.dart';
44
import 'package:ht_dashboard/content_management/bloc/content_management_bloc.dart';
5-
import 'package:ht_dashboard/content_management/view/topic_page.dart';
5+
import 'package:ht_dashboard/content_management/view/topics_page.dart';
66
import 'package:ht_dashboard/content_management/view/headlines_page.dart';
77
import 'package:ht_dashboard/content_management/view/sources_page.dart';
88
import 'package:ht_dashboard/l10n/l10n.dart';

lib/content_management/view/topic_page.dart renamed to lib/content_management/view/topics_page.dart

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import 'package:ht_dashboard/shared/shared.dart';
1111
import 'package:ht_shared/ht_shared.dart';
1212
import 'package:intl/intl.dart';
1313

14-
/// {@template categories_page}
14+
/// {@template topics_page}
1515
/// A page for displaying and managing Topics in a tabular format.
1616
/// {@endtemplate}
1717
class TopicPage extends StatefulWidget {
18-
/// {@macro categories_page}
18+
/// {@macro topics_page}
1919
const TopicPage({super.key});
2020

2121
@override
@@ -27,7 +27,7 @@ class _TopicPageState extends State<TopicPage> {
2727
void initState() {
2828
super.initState();
2929
context.read<ContentManagementBloc>().add(
30-
const LoadCategoriesRequested(limit: kDefaultRowsPerPage),
30+
const LoadTopicsRequested(limit: kDefaultRowsPerPage),
3131
);
3232
}
3333

@@ -38,34 +38,34 @@ class _TopicPageState extends State<TopicPage> {
3838
padding: const EdgeInsets.all(AppSpacing.lg),
3939
child: BlocBuilder<ContentManagementBloc, ContentManagementState>(
4040
builder: (context, state) {
41-
if (state.categoriesStatus == ContentManagementStatus.loading &&
42-
state.categories.isEmpty) {
41+
if (state.topicsStatus == ContentManagementStatus.loading &&
42+
state.topics.isEmpty) {
4343
return LoadingStateWidget(
44-
icon: Icons.category,
45-
headline: l10n.loadingCategories,
44+
icon: Icons.topic,
45+
headline: l10n.loadingTopics,
4646
subheadline: l10n.pleaseWait,
4747
);
4848
}
4949

50-
if (state.categoriesStatus == ContentManagementStatus.failure) {
50+
if (state.topicsStatus == ContentManagementStatus.failure) {
5151
return FailureStateWidget(
5252
message: state.errorMessage ?? l10n.unknownError,
5353
onRetry: () => context.read<ContentManagementBloc>().add(
54-
const LoadCategoriesRequested(limit: kDefaultRowsPerPage),
54+
const LoadTopicsRequested(limit: kDefaultRowsPerPage),
5555
),
5656
);
5757
}
5858

59-
if (state.categories.isEmpty) {
59+
if (state.topics.isEmpty) {
6060
return Center(
61-
child: Text(l10n.noCategoriesFound),
61+
child: Text(l10n.noTopicsFound),
6262
);
6363
}
6464

6565
return PaginatedDataTable2(
6666
columns: [
6767
DataColumn2(
68-
label: Text(l10n.categoryName),
68+
label: Text(l10n.topicName),
6969
size: ColumnSize.L,
7070
),
7171
DataColumn2(
@@ -84,28 +84,27 @@ class _TopicPageState extends State<TopicPage> {
8484
],
8585
source: _TopicsDataSource(
8686
context: context,
87-
categories: state.categories,
88-
isLoading:
89-
state.categoriesStatus == ContentManagementStatus.loading,
90-
hasMore: state.categoriesHasMore,
87+
topics: state.topics,
88+
isLoading: state.topicsStatus == ContentManagementStatus.loading,
89+
hasMore: state.topicsHasMore,
9190
l10n: l10n,
9291
),
9392
rowsPerPage: kDefaultRowsPerPage,
9493
availableRowsPerPage: const [kDefaultRowsPerPage],
9594
onPageChanged: (pageIndex) {
9695
final newOffset = pageIndex * kDefaultRowsPerPage;
97-
if (newOffset >= state.categories.length &&
98-
state.categoriesHasMore &&
99-
state.categoriesStatus != ContentManagementStatus.loading) {
96+
if (newOffset >= state.topics.length &&
97+
state.topicsHasMore &&
98+
state.topicsStatus != ContentManagementStatus.loading) {
10099
context.read<ContentManagementBloc>().add(
101-
LoadCategoriesRequested(
102-
startAfterId: state.categoriesCursor,
100+
LoadTopicsRequested(
101+
startAfterId: state.topicsCursor,
103102
limit: kDefaultRowsPerPage,
104103
),
105104
);
106105
}
107106
},
108-
empty: Center(child: Text(l10n.noCategoriesFound)),
107+
empty: Center(child: Text(l10n.noTopicsFound)),
109108
showCheckboxColumn: false,
110109
showFirstLastButtons: true,
111110
fit: FlexFit.tight,
@@ -123,21 +122,21 @@ class _TopicPageState extends State<TopicPage> {
123122
class _TopicsDataSource extends DataTableSource {
124123
_TopicsDataSource({
125124
required this.context,
126-
required this.categories,
125+
required this.topics,
127126
required this.isLoading,
128127
required this.hasMore,
129128
required this.l10n,
130129
});
131130

132131
final BuildContext context;
133-
final List<Category> categories;
132+
final List<Topic> topics;
134133
final bool isLoading;
135134
final bool hasMore;
136135
final AppLocalizations l10n;
137136

138137
@override
139138
DataRow? getRow(int index) {
140-
if (index >= categories.length) {
139+
if (index >= topics.length) {
141140
// This can happen if hasMore is true and the user is on the last page.
142141
// If we are loading, show a spinner. Otherwise, we've reached the end.
143142
if (isLoading) {
@@ -150,25 +149,23 @@ class _TopicsDataSource extends DataTableSource {
150149
}
151150
return null;
152151
}
153-
final category = categories[index];
152+
final topic = topics[index];
154153
return DataRow2(
155154
onSelectChanged: (selected) {
156155
if (selected ?? false) {
157156
context.goNamed(
158-
Routes.editCategoryName,
159-
pathParameters: {'id': category.id},
157+
Routes.editTopicName,
158+
pathParameters: {'id': topic.id},
160159
);
161160
}
162161
},
163162
cells: [
164-
DataCell(Text(category.name)),
165-
DataCell(Text(category.status.l10n(context))),
163+
DataCell(Text(topic.name)),
164+
DataCell(Text(topic.status.l10n(context))),
166165
DataCell(
167166
Text(
168-
category.updatedAt != null
169-
// TODO(fulleni): Make date format configurable by admin.
170-
? DateFormat('dd-MM-yyyy').format(category.updatedAt!.toLocal())
171-
: l10n.notAvailable,
167+
// TODO(fulleni): Make date format configurable by admin.
168+
DateFormat('dd-MM-yyyy').format(topic.updatedAt.toLocal()),
172169
),
173170
),
174171
DataCell(
@@ -179,8 +176,8 @@ class _TopicsDataSource extends DataTableSource {
179176
onPressed: () {
180177
// Navigate to edit page
181178
context.goNamed(
182-
Routes.editCategoryName, // Assuming an edit route exists
183-
pathParameters: {'id': category.id},
179+
Routes.editTopicName, // Assuming an edit route exists
180+
pathParameters: {'id': topic.id},
184181
);
185182
},
186183
),
@@ -189,7 +186,7 @@ class _TopicsDataSource extends DataTableSource {
189186
onPressed: () {
190187
// Dispatch delete event
191188
context.read<ContentManagementBloc>().add(
192-
DeleteCategoryRequested(category.id),
189+
DeleteTopicRequested(topic.id),
193190
);
194191
},
195192
),
@@ -211,11 +208,9 @@ class _TopicsDataSource extends DataTableSource {
211208
if (hasMore) {
212209
// When loading, we show an extra row for the spinner.
213210
// Otherwise, we just indicate that there are more rows.
214-
return isLoading
215-
? categories.length + 1
216-
: categories.length + kDefaultRowsPerPage;
211+
return isLoading ? topics.length + 1 : topics.length + kDefaultRowsPerPage;
217212
}
218-
return categories.length;
213+
return topics.length;
219214
}
220215

221216
@override

0 commit comments

Comments
 (0)