Skip to content

Commit e02d66b

Browse files
committed
feat(local_ads_management): implement save as draft and publish functionalities
- Replace CreateLocalNativeAdSubmitted event handler with two new handlers - Add CreateLocalNativeAdSavedAsDraft event handler - Add CreateLocalNativeAdPublished event handler - Implement logic to save local native ad as a draft - Implement logic to publish local native ad - Update state management for both saving as draft and publishing actions
1 parent d23ac50 commit e02d66b

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

lib/local_ads_management/bloc/create_local_ads/create_local_native_ad_bloc.dart

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class CreateLocalNativeAdBloc
1919
on<CreateLocalNativeAdSubtitleChanged>(_onSubtitleChanged);
2020
on<CreateLocalNativeAdImageUrlChanged>(_onImageUrlChanged);
2121
on<CreateLocalNativeAdTargetUrlChanged>(_onTargetUrlChanged);
22-
on<CreateLocalNativeAdSubmitted>(_onSubmitted);
22+
on<CreateLocalNativeAdSavedAsDraft>(_onSavedAsDraft);
23+
on<CreateLocalNativeAdPublished>(_onPublished);
2324
}
2425

2526
final DataRepository<LocalAd> _localAdsRepository;
@@ -53,8 +54,54 @@ class CreateLocalNativeAdBloc
5354
emit(state.copyWith(targetUrl: event.targetUrl));
5455
}
5556

56-
Future<void> _onSubmitted(
57-
CreateLocalNativeAdSubmitted event,
57+
/// Handles saving the local native ad as a draft.
58+
Future<void> _onSavedAsDraft(
59+
CreateLocalNativeAdSavedAsDraft event,
60+
Emitter<CreateLocalNativeAdState> emit,
61+
) async {
62+
if (!state.isFormValid) return;
63+
64+
emit(state.copyWith(status: CreateLocalNativeAdStatus.submitting));
65+
try {
66+
final now = DateTime.now();
67+
final newLocalNativeAd = LocalNativeAd(
68+
id: _uuid.v4(),
69+
title: state.title,
70+
subtitle: state.subtitle,
71+
imageUrl: state.imageUrl,
72+
targetUrl: state.targetUrl,
73+
createdAt: now,
74+
updatedAt: now,
75+
status: ContentStatus.draft,
76+
);
77+
78+
await _localAdsRepository.create(item: newLocalNativeAd);
79+
emit(
80+
state.copyWith(
81+
status: CreateLocalNativeAdStatus.success,
82+
createdLocalNativeAd: newLocalNativeAd,
83+
),
84+
);
85+
} on HttpException catch (e) {
86+
emit(
87+
state.copyWith(
88+
status: CreateLocalNativeAdStatus.failure,
89+
exception: e,
90+
),
91+
);
92+
} catch (e) {
93+
emit(
94+
state.copyWith(
95+
status: CreateLocalNativeAdStatus.failure,
96+
exception: UnknownException('An unexpected error occurred: $e'),
97+
),
98+
);
99+
}
100+
}
101+
102+
/// Handles publishing the local native ad.
103+
Future<void> _onPublished(
104+
CreateLocalNativeAdPublished event,
58105
Emitter<CreateLocalNativeAdState> emit,
59106
) async {
60107
if (!state.isFormValid) return;

0 commit comments

Comments
 (0)