Skip to content

Commit 7b58044

Browse files
committed
refactor(content): migrate edit headline UI to use topic and country
Refactors the `EditHeadlinePage` to fully align with the current `Headline` data model and the `EditHeadlineBloc`. - Updates the `BlocProvider` to inject `topicsRepository` and `countriesRepository` instead of the outdated `categoriesRepository`. - Replaces the "Description" field with "Excerpt" and updates the corresponding controller and event. - Replaces the single `Category` dropdown with two separate dropdowns for `Topic` and `Country`, dispatching the correct events and correctly handling the selected values.
1 parent fd877b6 commit 7b58044

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

lib/content_management/view/edit_headline_page.dart

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class EditHeadlinePage extends StatelessWidget {
2525
create: (context) => EditHeadlineBloc(
2626
headlinesRepository: context.read<HtDataRepository<Headline>>(),
2727
sourcesRepository: context.read<HtDataRepository<Source>>(),
28-
categoriesRepository: context.read<HtDataRepository<Category>>(),
28+
topicsRepository: context.read<HtDataRepository<Topic>>(),
29+
countriesRepository: context.read<HtDataRepository<Country>>(),
2930
headlineId: headlineId,
3031
)..add(const EditHeadlineLoaded()),
3132
child: const _EditHeadlineView(),
@@ -43,7 +44,7 @@ class _EditHeadlineView extends StatefulWidget {
4344
class _EditHeadlineViewState extends State<_EditHeadlineView> {
4445
final _formKey = GlobalKey<FormState>();
4546
late final TextEditingController _titleController;
46-
late final TextEditingController _descriptionController;
47+
late final TextEditingController _excerptController;
4748
late final TextEditingController _urlController;
4849
late final TextEditingController _imageUrlController;
4950

@@ -52,15 +53,15 @@ class _EditHeadlineViewState extends State<_EditHeadlineView> {
5253
super.initState();
5354
final state = context.read<EditHeadlineBloc>().state;
5455
_titleController = TextEditingController(text: state.title);
55-
_descriptionController = TextEditingController(text: state.description);
56+
_excerptController = TextEditingController(text: state.excerpt);
5657
_urlController = TextEditingController(text: state.url);
5758
_imageUrlController = TextEditingController(text: state.imageUrl);
5859
}
5960

6061
@override
6162
void dispose() {
6263
_titleController.dispose();
63-
_descriptionController.dispose();
64+
_excerptController.dispose();
6465
_urlController.dispose();
6566
_imageUrlController.dispose();
6667
super.dispose();
@@ -130,7 +131,7 @@ class _EditHeadlineViewState extends State<_EditHeadlineView> {
130131
}
131132
if (state.initialHeadline != null) {
132133
_titleController.text = state.title;
133-
_descriptionController.text = state.description;
134+
_excerptController.text = state.excerpt;
134135
_urlController.text = state.url;
135136
_imageUrlController.text = state.imageUrl;
136137
}
@@ -167,14 +168,25 @@ class _EditHeadlineViewState extends State<_EditHeadlineView> {
167168
}
168169
}
169170

170-
Category? selectedCategory;
171-
if (state.category != null) {
171+
Topic? selectedTopic;
172+
if (state.topic != null) {
172173
try {
173-
selectedCategory = state.categories.firstWhere(
174-
(c) => c.id == state.category!.id,
174+
selectedTopic = state.topics.firstWhere(
175+
(t) => t.id == state.topic!.id,
175176
);
176177
} catch (_) {
177-
selectedCategory = null;
178+
selectedTopic = null;
179+
}
180+
}
181+
182+
Country? selectedCountry;
183+
if (state.eventCountry != null) {
184+
try {
185+
selectedCountry = state.countries.firstWhere(
186+
(c) => c.id == state.eventCountry!.id,
187+
);
188+
} catch (_) {
189+
selectedCountry = null;
178190
}
179191
}
180192

@@ -198,15 +210,15 @@ class _EditHeadlineViewState extends State<_EditHeadlineView> {
198210
),
199211
const SizedBox(height: AppSpacing.lg),
200212
TextFormField(
201-
controller: _descriptionController,
213+
controller: _excerptController,
202214
decoration: InputDecoration(
203-
labelText: l10n.description,
215+
labelText: l10n.excerpt,
204216
border: const OutlineInputBorder(),
205217
),
206218
maxLines: 3,
207219
onChanged: (value) => context
208220
.read<EditHeadlineBloc>()
209-
.add(EditHeadlineDescriptionChanged(value)),
221+
.add(EditHeadlineExcerptChanged(value)),
210222
),
211223
const SizedBox(height: AppSpacing.lg),
212224
TextFormField(
@@ -251,24 +263,44 @@ class _EditHeadlineViewState extends State<_EditHeadlineView> {
251263
.add(EditHeadlineSourceChanged(value)),
252264
),
253265
const SizedBox(height: AppSpacing.lg),
254-
DropdownButtonFormField<Category?>(
255-
value: selectedCategory,
266+
DropdownButtonFormField<Topic?>(
267+
value: selectedTopic,
268+
decoration: InputDecoration(
269+
labelText: l10n.topicName,
270+
border: const OutlineInputBorder(),
271+
),
272+
items: [
273+
DropdownMenuItem(value: null, child: Text(l10n.none)),
274+
...state.topics.map(
275+
(topic) => DropdownMenuItem(
276+
value: topic,
277+
child: Text(topic.name),
278+
),
279+
),
280+
],
281+
onChanged: (value) => context
282+
.read<EditHeadlineBloc>()
283+
.add(EditHeadlineTopicChanged(value)),
284+
),
285+
const SizedBox(height: AppSpacing.lg),
286+
DropdownButtonFormField<Country?>(
287+
value: selectedCountry,
256288
decoration: InputDecoration(
257-
labelText: l10n.categoryName,
289+
labelText: l10n.countryName,
258290
border: const OutlineInputBorder(),
259291
),
260292
items: [
261293
DropdownMenuItem(value: null, child: Text(l10n.none)),
262-
...state.categories.map(
263-
(category) => DropdownMenuItem(
264-
value: category,
265-
child: Text(category.name),
294+
...state.countries.map(
295+
(country) => DropdownMenuItem(
296+
value: country,
297+
child: Text(country.name),
266298
),
267299
),
268300
],
269301
onChanged: (value) => context
270302
.read<EditHeadlineBloc>()
271-
.add(EditHeadlineCategoryChanged(value)),
303+
.add(EditHeadlineCountryChanged(value)),
272304
),
273305
const SizedBox(height: AppSpacing.lg),
274306
DropdownButtonFormField<ContentStatus>(

0 commit comments

Comments
 (0)