Skip to content

Commit 6058e79

Browse files
committed
fix(content_management): correct debounce logic and complete handlers
Replaces the incorrect `debounce` transformer with the correct `restartable` transformer from `bloc_concurrency` in all form BLoCs. This fixes the runtime error and correctly implements a debounce for search fields. Also completes the implementation for the language search and load-more handlers in `CreateSourceBloc` and `EditSourceBloc`, which were previously left incomplete. This includes adding proper error handling.
1 parent 90db0c0 commit 6058e79

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

lib/content_management/bloc/create_headline/create_headline_bloc.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CreateHeadlineBloc
3737
on<CreateHeadlineSubmitted>(_onSubmitted);
3838
on<CreateHeadlineCountrySearchChanged>(
3939
_onCountrySearchChanged,
40-
transformer: debounce(_searchDebounceDuration),
40+
transformer: restartable(),
4141
);
4242
on<CreateHeadlineLoadMoreCountriesRequested>(
4343
_onLoadMoreCountriesRequested);
@@ -200,6 +200,7 @@ class CreateHeadlineBloc
200200
CreateHeadlineCountrySearchChanged event,
201201
Emitter<CreateHeadlineState> emit,
202202
) async {
203+
await Future<void>.delayed(_searchDebounceDuration);
203204
emit(state.copyWith(countrySearchTerm: event.searchTerm));
204205
try {
205206
final countriesResponse = await _countriesRepository.readAll(

lib/content_management/bloc/create_source/create_source_bloc.dart

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
3333
on<CreateSourceSubmitted>(_onSubmitted);
3434
on<CreateSourceCountrySearchChanged>(
3535
_onCountrySearchChanged,
36-
transformer: debounce(_searchDebounceDuration),
36+
transformer: restartable(),
3737
);
3838
on<CreateSourceLoadMoreCountriesRequested>(
3939
_onLoadMoreCountriesRequested,
4040
);
4141
on<CreateSourceLanguageSearchChanged>(
4242
_onLanguageSearchChanged,
43-
transformer: debounce(_searchDebounceDuration),
43+
transformer: restartable(),
4444
);
4545
on<CreateSourceLoadMoreLanguagesRequested>(
4646
_onLoadMoreLanguagesRequested,
@@ -189,6 +189,7 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
189189
CreateSourceCountrySearchChanged event,
190190
Emitter<CreateSourceState> emit,
191191
) async {
192+
await Future<void>.delayed(_searchDebounceDuration);
192193
emit(state.copyWith(countrySearchTerm: event.searchTerm));
193194
try {
194195
final countriesResponse = await _countriesRepository.readAll(
@@ -251,6 +252,7 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
251252
CreateSourceLanguageSearchChanged event,
252253
Emitter<CreateSourceState> emit,
253254
) async {
255+
await Future<void>.delayed(_searchDebounceDuration);
254256
emit(state.copyWith(languageSearchTerm: event.searchTerm));
255257
try {
256258
final languagesResponse = await _languagesRepository.readAll(
@@ -265,8 +267,15 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
265267
languagesHasMore: languagesResponse.hasMore,
266268
),
267269
);
270+
} on HttpException catch (e) {
271+
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
268272
} catch (e) {
269-
// Error handling omitted for brevity, but should be implemented
273+
emit(
274+
state.copyWith(
275+
status: CreateSourceStatus.failure,
276+
exception: UnknownException('An unexpected error occurred: $e'),
277+
),
278+
);
270279
}
271280
}
272281

@@ -275,6 +284,31 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
275284
Emitter<CreateSourceState> emit,
276285
) async {
277286
if (!state.languagesHasMore) return;
278-
// Implementation similar to _onLoadMoreCountriesRequested
287+
288+
try {
289+
final languagesResponse = await _languagesRepository.readAll(
290+
cursor: state.languagesCursor,
291+
filter: {'name': state.languageSearchTerm},
292+
sort: [const SortOption('name', SortOrder.asc)],
293+
) as PaginatedResponse<Language>;
294+
295+
emit(
296+
state.copyWith(
297+
languages: List.of(state.languages)
298+
..addAll(languagesResponse.items),
299+
languagesCursor: languagesResponse.cursor,
300+
languagesHasMore: languagesResponse.hasMore,
301+
),
302+
);
303+
} on HttpException catch (e) {
304+
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
305+
} catch (e) {
306+
emit(
307+
state.copyWith(
308+
status: CreateSourceStatus.failure,
309+
exception: UnknownException('An unexpected error occurred: $e'),
310+
),
311+
);
312+
}
279313
}
280314
}

lib/content_management/bloc/edit_headline/edit_headline_bloc.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
3737
on<EditHeadlineSubmitted>(_onSubmitted);
3838
on<EditHeadlineCountrySearchChanged>(
3939
_onCountrySearchChanged,
40-
transformer: debounce(_searchDebounceDuration),
40+
transformer: restartable(),
4141
);
4242
on<EditHeadlineLoadMoreCountriesRequested>(
4343
_onLoadMoreCountriesRequested,
@@ -250,6 +250,7 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
250250
EditHeadlineCountrySearchChanged event,
251251
Emitter<EditHeadlineState> emit,
252252
) async {
253+
await Future<void>.delayed(_searchDebounceDuration);
253254
emit(state.copyWith(countrySearchTerm: event.searchTerm));
254255
try {
255256
final countriesResponse = await _countriesRepository.readAll(

lib/content_management/bloc/edit_source/edit_source_bloc.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
3434
on<EditSourceSubmitted>(_onSubmitted);
3535
on<EditSourceCountrySearchChanged>(
3636
_onCountrySearchChanged,
37-
transformer: debounce(_searchDebounceDuration),
37+
transformer: restartable(),
3838
);
3939
on<EditSourceLoadMoreCountriesRequested>(
4040
_onLoadMoreCountriesRequested,
4141
);
4242
on<EditSourceLanguageSearchChanged>(
4343
_onLanguageSearchChanged,
44-
transformer: debounce(_searchDebounceDuration),
44+
transformer: restartable(),
4545
);
4646
on<EditSourceLoadMoreLanguagesRequested>(
4747
_onLoadMoreLanguagesRequested,
@@ -242,6 +242,7 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
242242
EditSourceCountrySearchChanged event,
243243
Emitter<EditSourceState> emit,
244244
) async {
245+
await Future<void>.delayed(_searchDebounceDuration);
245246
emit(state.copyWith(countrySearchTerm: event.searchTerm));
246247
try {
247248
final countriesResponse = await _countriesRepository.readAll(
@@ -304,6 +305,7 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
304305
EditSourceLanguageSearchChanged event,
305306
Emitter<EditSourceState> emit,
306307
) async {
308+
await Future<void>.delayed(_searchDebounceDuration);
307309
emit(state.copyWith(languageSearchTerm: event.searchTerm));
308310
try {
309311
final languagesResponse = await _languagesRepository.readAll(
@@ -318,8 +320,15 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
318320
languagesHasMore: languagesResponse.hasMore,
319321
),
320322
);
323+
} on HttpException catch (e) {
324+
emit(state.copyWith(status: EditSourceStatus.failure, exception: e));
321325
} catch (e) {
322-
// Proper error handling should be implemented here
326+
emit(
327+
state.copyWith(
328+
status: EditSourceStatus.failure,
329+
exception: UnknownException('An unexpected error occurred: $e'),
330+
),
331+
);
323332
}
324333
}
325334

@@ -343,8 +352,15 @@ class EditSourceBloc extends Bloc<EditSourceEvent, EditSourceState> {
343352
languagesHasMore: languagesResponse.hasMore,
344353
),
345354
);
355+
} on HttpException catch (e) {
356+
emit(state.copyWith(status: EditSourceStatus.failure, exception: e));
346357
} catch (e) {
347-
// Proper error handling should be implemented here
358+
emit(
359+
state.copyWith(
360+
status: EditSourceStatus.failure,
361+
exception: UnknownException('An unexpected error occurred: $e'),
362+
),
363+
);
348364
}
349365
}
350366
}

lib/shared/widgets/widgets.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export 'country_dropdown_form_field.dart';
2+
export 'language_dropdown_form_field.dart';
13
export 'searchable_dropdown_form_field.dart';

0 commit comments

Comments
 (0)