Skip to content

Fix content management form #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class CreateHeadlineBloc
on<CreateHeadlineCountryChanged>(_onCountryChanged);
on<CreateHeadlineStatusChanged>(_onStatusChanged);
on<CreateHeadlineSubmitted>(_onSubmitted);
on<CreateHeadlineLoadMoreCountriesRequested>(_onLoadMoreCountriesRequested);
}

final DataRepository<Headline> _headlinesRepository;
Expand Down Expand Up @@ -76,6 +75,21 @@ class CreateHeadlineBloc
countriesHasMore: countriesResponse.hasMore,
),
);

// Start background fetching for all countries
while (state.countriesHasMore) {
final nextCountries = await _countriesRepository.readAll(
pagination: PaginationOptions(cursor: state.countriesCursor),
sort: [const SortOption('name', SortOrder.asc)],
);
emit(
state.copyWith(
countries: List.of(state.countries)..addAll(nextCountries.items),
countriesCursor: nextCountries.cursor,
countriesHasMore: nextCountries.hasMore,
),
);
}
} on HttpException catch (e) {
emit(state.copyWith(status: CreateHeadlineStatus.failure, exception: e));
} catch (e) {
Expand Down Expand Up @@ -190,47 +204,4 @@ class CreateHeadlineBloc
);
}
}

Future<void> _onLoadMoreCountriesRequested(
CreateHeadlineLoadMoreCountriesRequested event,
Emitter<CreateHeadlineState> emit,
) async {
if (!state.countriesHasMore || state.countriesIsLoadingMore) return;

emit(state.copyWith(countriesIsLoadingMore: true));

try {
final countriesResponse = await _countriesRepository.readAll(
pagination: state.countriesCursor != null
? PaginationOptions(cursor: state.countriesCursor)
: null,
sort: [const SortOption('name', SortOrder.asc)],
);

emit(
state.copyWith(
countries: List.of(state.countries)..addAll(countriesResponse.items),
countriesCursor: countriesResponse.cursor,
countriesHasMore: countriesResponse.hasMore,
countriesIsLoadingMore: false,
),
);
} on HttpException catch (e) {
emit(
state.copyWith(
status: CreateHeadlineStatus.failure,
exception: e,
countriesIsLoadingMore: false,
),
);
} catch (e) {
emit(
state.copyWith(
status: CreateHeadlineStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
countriesIsLoadingMore: false,
),
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,3 @@ final class CreateHeadlineStatusChanged extends CreateHeadlineEvent {
final class CreateHeadlineSubmitted extends CreateHeadlineEvent {
const CreateHeadlineSubmitted();
}

/// Event to request loading more countries.
final class CreateHeadlineLoadMoreCountriesRequested
extends CreateHeadlineEvent {
const CreateHeadlineLoadMoreCountriesRequested();
}
122 changes: 30 additions & 92 deletions lib/content_management/bloc/create_source/create_source_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
on<CreateSourceHeadquartersChanged>(_onHeadquartersChanged);
on<CreateSourceStatusChanged>(_onStatusChanged);
on<CreateSourceSubmitted>(_onSubmitted);
on<CreateSourceLoadMoreCountriesRequested>(
_onLoadMoreCountriesRequested,
);
on<CreateSourceLoadMoreLanguagesRequested>(
_onLoadMoreLanguagesRequested,
);
}

final DataRepository<Source> _sourcesRepository;
Expand Down Expand Up @@ -71,6 +65,36 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
languagesHasMore: languagesPaginated.hasMore,
),
);

// Start background fetching for all countries
while (state.countriesHasMore) {
final nextCountries = await _countriesRepository.readAll(
pagination: PaginationOptions(cursor: state.countriesCursor),
sort: [const SortOption('name', SortOrder.asc)],
);
emit(
state.copyWith(
countries: List.of(state.countries)..addAll(nextCountries.items),
countriesCursor: nextCountries.cursor,
countriesHasMore: nextCountries.hasMore,
),
);
}

// Start background fetching for all languages
while (state.languagesHasMore) {
final nextLanguages = await _languagesRepository.readAll(
pagination: PaginationOptions(cursor: state.languagesCursor),
sort: [const SortOption('name', SortOrder.asc)],
);
emit(
state.copyWith(
languages: List.of(state.languages)..addAll(nextLanguages.items),
languagesCursor: nextLanguages.cursor,
languagesHasMore: nextLanguages.hasMore,
),
);
}
} on HttpException catch (e) {
emit(state.copyWith(status: CreateSourceStatus.failure, exception: e));
} catch (e) {
Expand Down Expand Up @@ -177,90 +201,4 @@ class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
);
}
}

Future<void> _onLoadMoreCountriesRequested(
CreateSourceLoadMoreCountriesRequested event,
Emitter<CreateSourceState> emit,
) async {
if (!state.countriesHasMore || state.countriesIsLoadingMore) return;

emit(state.copyWith(countriesIsLoadingMore: true));

try {
final countriesResponse = await _countriesRepository.readAll(
pagination: state.countriesCursor != null
? PaginationOptions(cursor: state.countriesCursor)
: null,
sort: [const SortOption('name', SortOrder.asc)],
);

emit(
state.copyWith(
countries: List.of(state.countries)..addAll(countriesResponse.items),
countriesCursor: countriesResponse.cursor,
countriesHasMore: countriesResponse.hasMore,
countriesIsLoadingMore: false,
),
);
} on HttpException catch (e) {
emit(
state.copyWith(
status: CreateSourceStatus.failure,
exception: e,
countriesIsLoadingMore: false,
),
);
} catch (e) {
emit(
state.copyWith(
status: CreateSourceStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
countriesIsLoadingMore: false,
),
);
}
}

Future<void> _onLoadMoreLanguagesRequested(
CreateSourceLoadMoreLanguagesRequested event,
Emitter<CreateSourceState> emit,
) async {
if (!state.languagesHasMore || state.languagesIsLoadingMore) return;

emit(state.copyWith(languagesIsLoadingMore: true));

try {
final languagesResponse = await _languagesRepository.readAll(
pagination: state.languagesCursor != null
? PaginationOptions(cursor: state.languagesCursor)
: null,
sort: [const SortOption('name', SortOrder.asc)],
);

emit(
state.copyWith(
languages: List.of(state.languages)..addAll(languagesResponse.items),
languagesCursor: languagesResponse.cursor,
languagesHasMore: languagesResponse.hasMore,
languagesIsLoadingMore: false,
),
);
} on HttpException catch (e) {
emit(
state.copyWith(
status: CreateSourceStatus.failure,
exception: e,
languagesIsLoadingMore: false,
),
);
} catch (e) {
emit(
state.copyWith(
status: CreateSourceStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
languagesIsLoadingMore: false,
),
);
}
}
}
10 changes: 0 additions & 10 deletions lib/content_management/bloc/create_source/create_source_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,3 @@ final class CreateSourceStatusChanged extends CreateSourceEvent {
final class CreateSourceSubmitted extends CreateSourceEvent {
const CreateSourceSubmitted();
}

/// Event to request loading more countries.
final class CreateSourceLoadMoreCountriesRequested extends CreateSourceEvent {
const CreateSourceLoadMoreCountriesRequested();
}

/// Event to request loading more languages.
final class CreateSourceLoadMoreLanguagesRequested extends CreateSourceEvent {
const CreateSourceLoadMoreLanguagesRequested();
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ final class CreateSourceState extends Equatable {
headquarters,
countries,
countriesHasMore,
countriesIsLoadingMore,
countriesCursor,
languages,
languagesHasMore,
languagesIsLoadingMore,
languagesCursor,
contentStatus,
exception,
countriesIsLoadingMore,
countriesCursor,
languages,
languagesHasMore,
languagesIsLoadingMore,
languagesCursor,
contentStatus,
exception,
createdSource,
];
}
61 changes: 15 additions & 46 deletions lib/content_management/bloc/edit_headline/edit_headline_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
on<EditHeadlineCountryChanged>(_onCountryChanged);
on<EditHeadlineStatusChanged>(_onStatusChanged);
on<EditHeadlineSubmitted>(_onSubmitted);
on<EditHeadlineLoadMoreCountriesRequested>(
_onLoadMoreCountriesRequested,
);
}

final DataRepository<Headline> _headlinesRepository;
Expand Down Expand Up @@ -89,6 +86,21 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
contentStatus: headline.status,
),
);

// Start background fetching for all countries
while (state.countriesHasMore) {
final nextCountries = await _countriesRepository.readAll(
pagination: PaginationOptions(cursor: state.countriesCursor),
sort: [const SortOption('name', SortOrder.asc)],
);
emit(
state.copyWith(
countries: List.of(state.countries)..addAll(nextCountries.items),
countriesCursor: nextCountries.cursor,
countriesHasMore: nextCountries.hasMore,
),
);
}
} on HttpException catch (e) {
emit(state.copyWith(status: EditHeadlineStatus.failure, exception: e));
} catch (e) {
Expand Down Expand Up @@ -240,47 +252,4 @@ class EditHeadlineBloc extends Bloc<EditHeadlineEvent, EditHeadlineState> {
);
}
}

Future<void> _onLoadMoreCountriesRequested(
EditHeadlineLoadMoreCountriesRequested event,
Emitter<EditHeadlineState> emit,
) async {
if (!state.countriesHasMore || state.countriesIsLoadingMore) return;

emit(state.copyWith(countriesIsLoadingMore: true));

try {
final countriesResponse = await _countriesRepository.readAll(
pagination: state.countriesCursor != null
? PaginationOptions(cursor: state.countriesCursor)
: null,
sort: [const SortOption('name', SortOrder.asc)],
);

emit(
state.copyWith(
countries: List.of(state.countries)..addAll(countriesResponse.items),
countriesCursor: countriesResponse.cursor,
countriesHasMore: countriesResponse.hasMore,
countriesIsLoadingMore: false,
),
);
} on HttpException catch (e) {
emit(
state.copyWith(
status: EditHeadlineStatus.failure,
exception: e,
countriesIsLoadingMore: false,
),
);
} catch (e) {
emit(
state.copyWith(
status: EditHeadlineStatus.failure,
exception: UnknownException('An unexpected error occurred: $e'),
countriesIsLoadingMore: false,
),
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,3 @@ final class EditHeadlineStatusChanged extends EditHeadlineEvent {
final class EditHeadlineSubmitted extends EditHeadlineEvent {
const EditHeadlineSubmitted();
}

/// Event to request loading more countries.
final class EditHeadlineLoadMoreCountriesRequested extends EditHeadlineEvent {
const EditHeadlineLoadMoreCountriesRequested();
}
Loading
Loading