-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e320fbe
feat(create_source): add events for managing source creation
fulleni ed63804
feat(create-source): add state for create source bloc
fulleni 0f8b2cf
feat(create-source): add create source bloc
fulleni 9ecddac
feat(content-management): add create source page
fulleni 28e4b48
feat(l10n): add source creation strings
fulleni bd68c4d
feat(router): integrate create source page
fulleni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
lib/content_management/bloc/create_source/create_source_bloc.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
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
67
lib/content_management/bloc/create_source/create_source_event.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
85
lib/content_management/bloc/create_source/create_source_state.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
|
||
@override | ||
List<Object?> get props => [ | ||
status, | ||
name, | ||
description, | ||
url, | ||
sourceType, | ||
language, | ||
headquarters, | ||
countries, | ||
errorMessage, | ||
]; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.