Skip to content

Feature_create_source_within_content_management #16

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 6 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
141 changes: 141 additions & 0 deletions lib/content_management/bloc/create_source/create_source_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:ht_data_repository/ht_data_repository.dart';
import 'package:ht_shared/ht_shared.dart';

part 'create_source_event.dart';
part 'create_source_state.dart';

/// A BLoC to manage the state of creating a new source.
class CreateSourceBloc extends Bloc<CreateSourceEvent, CreateSourceState> {
/// {@macro create_source_bloc}
CreateSourceBloc({
required HtDataRepository<Source> sourcesRepository,
required HtDataRepository<Country> countriesRepository,
}) : _sourcesRepository = sourcesRepository,
_countriesRepository = countriesRepository,
super(const CreateSourceState()) {
on<CreateSourceDataLoaded>(_onDataLoaded);
on<CreateSourceNameChanged>(_onNameChanged);
on<CreateSourceDescriptionChanged>(_onDescriptionChanged);
on<CreateSourceUrlChanged>(_onUrlChanged);
on<CreateSourceTypeChanged>(_onSourceTypeChanged);
on<CreateSourceLanguageChanged>(_onLanguageChanged);
on<CreateSourceHeadquartersChanged>(_onHeadquartersChanged);
on<CreateSourceSubmitted>(_onSubmitted);
}

final HtDataRepository<Source> _sourcesRepository;
final HtDataRepository<Country> _countriesRepository;

Future<void> _onDataLoaded(
CreateSourceDataLoaded event,
Emitter<CreateSourceState> emit,
) async {
emit(state.copyWith(status: CreateSourceStatus.loading));
try {
final countriesResponse = await _countriesRepository.readAll();
final countries = (countriesResponse as PaginatedResponse<Country>).items;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The direct cast as PaginatedResponse<Country> is unsafe and could lead to a runtime crash if _countriesRepository.readAll() returns a different type. It's safer to perform a type check and handle unexpected responses gracefully.


emit(
state.copyWith(
status: CreateSourceStatus.initial,
countries: countries,
),
);
} on HtHttpException catch (e) {
emit(
state.copyWith(
status: CreateSourceStatus.failure,
errorMessage: e.message,
),
);
} catch (e) {
emit(
state.copyWith(
status: CreateSourceStatus.failure,
errorMessage: e.toString(),
),
);
}
}

void _onNameChanged(
CreateSourceNameChanged event,
Emitter<CreateSourceState> emit,
) {
emit(state.copyWith(name: event.name));
}

void _onDescriptionChanged(
CreateSourceDescriptionChanged event,
Emitter<CreateSourceState> emit,
) {
emit(state.copyWith(description: event.description));
}

void _onUrlChanged(
CreateSourceUrlChanged event,
Emitter<CreateSourceState> emit,
) {
emit(state.copyWith(url: event.url));
}

void _onSourceTypeChanged(
CreateSourceTypeChanged event,
Emitter<CreateSourceState> emit,
) {
emit(state.copyWith(sourceType: () => event.sourceType));
}

void _onLanguageChanged(
CreateSourceLanguageChanged event,
Emitter<CreateSourceState> emit,
) {
emit(state.copyWith(language: event.language));
}

void _onHeadquartersChanged(
CreateSourceHeadquartersChanged event,
Emitter<CreateSourceState> emit,
) {
emit(state.copyWith(headquarters: () => event.headquarters));
}

Future<void> _onSubmitted(
CreateSourceSubmitted event,
Emitter<CreateSourceState> emit,
) async {
if (!state.isFormValid) return;

emit(state.copyWith(status: CreateSourceStatus.submitting));
try {
final newSource = Source(
name: state.name,
description: state.description.isNotEmpty ? state.description : null,
url: state.url.isNotEmpty ? state.url : null,
sourceType: state.sourceType,
language: state.language.isNotEmpty ? state.language : null,
headquarters: state.headquarters,
);

await _sourcesRepository.create(item: newSource);
emit(state.copyWith(status: CreateSourceStatus.success));
} on HtHttpException catch (e) {
emit(
state.copyWith(
status: CreateSourceStatus.failure,
errorMessage: e.message,
),
);
} catch (e) {
emit(
state.copyWith(
status: CreateSourceStatus.failure,
errorMessage: e.toString(),
),
);
}
}
}
67 changes: 67 additions & 0 deletions lib/content_management/bloc/create_source/create_source_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
part of 'create_source_bloc.dart';

/// Base class for all events related to the [CreateSourceBloc].
sealed class CreateSourceEvent extends Equatable {
const CreateSourceEvent();

@override
List<Object?> get props => [];
}

/// Event to signal that the data for dropdowns should be loaded.
final class CreateSourceDataLoaded extends CreateSourceEvent {
const CreateSourceDataLoaded();
}

/// Event for when the source's name is changed.
final class CreateSourceNameChanged extends CreateSourceEvent {
const CreateSourceNameChanged(this.name);
final String name;
@override
List<Object> get props => [name];
}

/// Event for when the source's description is changed.
final class CreateSourceDescriptionChanged extends CreateSourceEvent {
const CreateSourceDescriptionChanged(this.description);
final String description;
@override
List<Object> get props => [description];
}

/// Event for when the source's URL is changed.
final class CreateSourceUrlChanged extends CreateSourceEvent {
const CreateSourceUrlChanged(this.url);
final String url;
@override
List<Object> get props => [url];
}

/// Event for when the source's type is changed.
final class CreateSourceTypeChanged extends CreateSourceEvent {
const CreateSourceTypeChanged(this.sourceType);
final SourceType? sourceType;
@override
List<Object?> get props => [sourceType];
}

/// Event for when the source's language is changed.
final class CreateSourceLanguageChanged extends CreateSourceEvent {
const CreateSourceLanguageChanged(this.language);
final String language;
@override
List<Object> get props => [language];
}

/// Event for when the source's headquarters is changed.
final class CreateSourceHeadquartersChanged extends CreateSourceEvent {
const CreateSourceHeadquartersChanged(this.headquarters);
final Country? headquarters;
@override
List<Object?> get props => [headquarters];
}

/// Event to signal that the form should be submitted.
final class CreateSourceSubmitted extends CreateSourceEvent {
const CreateSourceSubmitted();
}
85 changes: 85 additions & 0 deletions lib/content_management/bloc/create_source/create_source_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
part of 'create_source_bloc.dart';

/// Represents the status of the create source operation.
enum CreateSourceStatus {
/// Initial state, before any data is loaded.
initial,

/// Data is being loaded.
loading,

/// An operation completed successfully.
success,

/// An error occurred.
failure,

/// The form is being submitted.
submitting,
}

/// The state for the [CreateSourceBloc].
final class CreateSourceState extends Equatable {
/// {@macro create_source_state}
const CreateSourceState({
this.status = CreateSourceStatus.initial,
this.name = '',
this.description = '',
this.url = '',
this.sourceType,
this.language = '',
this.headquarters,
this.countries = const [],
this.errorMessage,
});

final CreateSourceStatus status;
final String name;
final String description;
final String url;
final SourceType? sourceType;
final String language;
final Country? headquarters;
final List<Country> countries;
final String? errorMessage;

/// Returns true if the form is valid and can be submitted.
bool get isFormValid => name.isNotEmpty;

CreateSourceState copyWith({
CreateSourceStatus? status,
String? name,
String? description,
String? url,
ValueGetter<SourceType?>? sourceType,
String? language,
ValueGetter<Country?>? headquarters,
List<Country>? countries,
String? errorMessage,
}) {
return CreateSourceState(
status: status ?? this.status,
name: name ?? this.name,
description: description ?? this.description,
url: url ?? this.url,
sourceType: sourceType != null ? sourceType() : this.sourceType,
language: language ?? this.language,
headquarters: headquarters != null ? headquarters() : this.headquarters,
countries: countries ?? this.countries,
errorMessage: errorMessage,
);
}
Comment on lines +49 to +71

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The errorMessage in copyWith defaults to null if not provided, potentially clearing existing error messages. Use the ValueGetter pattern to avoid this, similar to sourceType and headquarters.

  CreateSourceState copyWith({
    CreateSourceStatus? status,
    String? name,
    String? description,
    String? url,
    ValueGetter<SourceType?>? sourceType,
    String? language,
    ValueGetter<Country?>? headquarters,
    List<Country>? countries,
    ValueGetter<String?>? errorMessage,
  }) {
    return CreateSourceState(
      status: status ?? this.status,
      name: name ?? this.name,
      description: description ?? this.description,
      url: url ?? this.url,
      sourceType: sourceType != null ? sourceType() : this.sourceType,
      language: language ?? this.language,
      headquarters: headquarters != null ? headquarters() : this.headquarters,
      countries: countries ?? this.countries,
      errorMessage: errorMessage != null ? errorMessage() : this.errorMessage,
    );
  }


@override
List<Object?> get props => [
status,
name,
description,
url,
sourceType,
language,
headquarters,
countries,
errorMessage,
];
}
Loading
Loading