Skip to content

Commit 8edfa8e

Browse files
committed
feat(category): add create category BLoC
- Manages category creation state - Handles form input changes - Submits new category to repository - Includes submission status and error states
1 parent 0b99b93 commit 8edfa8e

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import 'package:bloc/bloc.dart';
2+
import 'package:equatable/equatable.dart';
3+
import 'package:ht_data_repository/ht_data_repository.dart';
4+
import 'package:ht_shared/ht_shared.dart';
5+
6+
part 'create_category_event.dart';
7+
part 'create_category_state.dart';
8+
9+
/// A BLoC to manage the state of creating a new category.
10+
class CreateCategoryBloc
11+
extends Bloc<CreateCategoryEvent, CreateCategoryState> {
12+
/// {@macro create_category_bloc}
13+
CreateCategoryBloc({
14+
required HtDataRepository<Category> categoriesRepository,
15+
}) : _categoriesRepository = categoriesRepository,
16+
super(const CreateCategoryState()) {
17+
on<CreateCategoryNameChanged>(_onNameChanged);
18+
on<CreateCategoryDescriptionChanged>(_onDescriptionChanged);
19+
on<CreateCategoryIconUrlChanged>(_onIconUrlChanged);
20+
on<CreateCategorySubmitted>(_onSubmitted);
21+
}
22+
23+
final HtDataRepository<Category> _categoriesRepository;
24+
25+
void _onNameChanged(
26+
CreateCategoryNameChanged event,
27+
Emitter<CreateCategoryState> emit,
28+
) {
29+
emit(
30+
state.copyWith(
31+
name: event.name,
32+
status: CreateCategoryStatus.initial,
33+
),
34+
);
35+
}
36+
37+
void _onDescriptionChanged(
38+
CreateCategoryDescriptionChanged event,
39+
Emitter<CreateCategoryState> emit,
40+
) {
41+
emit(
42+
state.copyWith(
43+
description: event.description,
44+
status: CreateCategoryStatus.initial,
45+
),
46+
);
47+
}
48+
49+
void _onIconUrlChanged(
50+
CreateCategoryIconUrlChanged event,
51+
Emitter<CreateCategoryState> emit,
52+
) {
53+
emit(
54+
state.copyWith(
55+
iconUrl: event.iconUrl,
56+
status: CreateCategoryStatus.initial,
57+
),
58+
);
59+
}
60+
61+
Future<void> _onSubmitted(
62+
CreateCategorySubmitted event,
63+
Emitter<CreateCategoryState> emit,
64+
) async {
65+
if (!state.isFormValid) return;
66+
67+
emit(state.copyWith(status: CreateCategoryStatus.submitting));
68+
try {
69+
final newCategory = Category(
70+
name: state.name,
71+
description: state.description.isNotEmpty ? state.description : null,
72+
iconUrl: state.iconUrl.isNotEmpty ? state.iconUrl : null,
73+
);
74+
75+
await _categoriesRepository.create(item: newCategory);
76+
emit(state.copyWith(status: CreateCategoryStatus.success));
77+
} on HtHttpException catch (e) {
78+
emit(
79+
state.copyWith(
80+
status: CreateCategoryStatus.failure,
81+
errorMessage: e.message,
82+
),
83+
);
84+
} catch (e) {
85+
emit(
86+
state.copyWith(
87+
status: CreateCategoryStatus.failure,
88+
errorMessage: e.toString(),
89+
),
90+
);
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)