You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

// import 'dart:async';
import 'package:bytedesk_kefu/blocs/feedback_bloc/bloc.dart';
import 'package:bytedesk_kefu/model/helpCategory.dart';
// import 'package:bytedesk_kefu/model/jsonResult.dart';
import 'package:bytedesk_kefu/repositories/feedback_repository.dart';
import 'package:bloc/bloc.dart';
class FeedbackBloc extends Bloc<FeedbackEvent, FeedbackState> {
//
final FeedbackRepository feedbackRepository = new FeedbackRepository();
FeedbackBloc() : super(new UnFeedbackState()) {
on<GetFeedbackCategoryEvent>(_mapGetFeedbackCategoryToState);
on<SubmitFeedbackEvent>(_mapSubmitFeedbackToState);
on<UploadImageEvent>(_mapUploadImageToState);
}
// @override
// void mapEventToState(
// FeedbackEvent event,
// ) async {
// if (event is GetFeedbackCategoryEvent) {
// yield* _mapGetFeedbackCategoryToState(event);
// } else if (event is SubmitFeedbackEvent) {
// yield* _mapSubmitFeedbackToState(event);
// } else if (event is UploadImageEvent) {
// yield* _mapUploadImageToState(event);
// }
// }
void _mapGetFeedbackCategoryToState(
GetFeedbackCategoryEvent event, Emitter<FeedbackState> emit) async {
emit(FeedbackLoading());
try {
final List<HelpCategory> categoryList =
await feedbackRepository.getHelpFeedbackCategories(event.uid);
emit(FeedbackCategoryState(categoryList));
} catch (error) {
print(error);
emit(FeedbackLoadError());
}
}
void _mapSubmitFeedbackToState(
SubmitFeedbackEvent event, Emitter<FeedbackState> emit) async {
emit(FeedbackSubmiting());
try {
await feedbackRepository.submitFeedback(event.content, event.imageUrls);
emit(FeedbackSubmitSuccess());
} catch (error) {
print(error);
emit(FeedbackSubmitError());
}
}
void _mapUploadImageToState(UploadImageEvent event, Emitter<FeedbackState> emit) async {
emit(ImageUploading());
try {
final String url = await feedbackRepository.upload(event.filePath);
emit(UploadImageSuccess(url));
} catch (error) {
print(error);
emit(UpLoadImageError());
}
}
}