passo_mobile_app/lib/bloc/profile/education/education_bloc.dart

132 lines
4.7 KiB
Dart

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/profile/educational_background.dart';
import 'package:unit2/sevices/profile/education_services.dart';
part 'education_event.dart';
part 'education_state.dart';
class EducationBloc extends Bloc<EducationEvent, EducationState> {
List<EducationalBackground> educationalBackgrounds = [];
List<School> schools = [];
List<Course> programs = [];
List<Honor> honors = [];
EducationBloc() : super(EducationInitial()) {
on<GetEducationalBackground>((event, emit) async {
emit(EducationalBackgroundLoadingState());
try {
List<EducationalBackground> educations = await EducationService.instace
.getEducationalBackground(event.profileId, event.token);
educationalBackgrounds = educations;
emit(EducationalBackgroundLoadedState(
educationalBackground: educationalBackgrounds));
} catch (e) {
emit(EducationalBackgroundErrorState(message: e.toString()));
}
});
//// SHOW ADD FORM
on<ShowAddEducationForm>((event, emit) async {
emit(EducationalBackgroundLoadingState());
try {
if (schools.isEmpty) {
List<School> newSchools = await EducationService.instace.getSchools();
schools = newSchools;
}
if (programs.isEmpty) {
List<Course> newPrograms =
await EducationService.instace.getPrograms();
programs = newPrograms;
}
if (honors.isEmpty) {
List<Honor> newHonors = await EducationService.instace.getHonors();
honors = newHonors;
}
emit(AddEducationState(
schools: schools, programs: programs, honors: honors));
} catch (e) {
emit(EducationalBackgroundErrorState(message: e.toString()));
}
});
////Add
on<AddEducation>((event, emit) async {
Map<dynamic, dynamic> status = await EducationService.instace.add(
honors: event.honors,
educationalBackground: event.educationalBackground,
token: event.token,
profileId: event.profileId);
if (status['success']) {
EducationalBackground educationalBackground =
EducationalBackground.fromJson(status['data']);
educationalBackgrounds.add(educationalBackground);
emit(EducationAddedState(response: status));
} else {
emit(EducationAddedState(response: status));
}
});
////Update
on<UpdateEducation>((event, emit) async {
Map<dynamic, dynamic> status = await EducationService.instace.edit(
honors: event.honors,
educationalBackground: event.educationalBackground,
token: event.token,
profileId: event.profileId);
if (status['success']) {
EducationalBackground educationalBackground =
EducationalBackground.fromJson(status['data']);
educationalBackgrounds.add(educationalBackground);
emit(EditedEducationState(response: status));
} else {
emit(EditedEducationState(response: status));
}
});
////LOAD
on<LoadEducations>((event, emit) {
emit(EducationalBackgroundLoadedState(
educationalBackground: educationalBackgrounds));
});
//// SHOW EDIT FORM
on<ShowEditEducationForm>((event, emit) async {
try {
if (schools.isEmpty) {
List<School> newSchools = await EducationService.instace.getSchools();
schools = newSchools;
}
if (programs.isEmpty) {
List<Course> newPrograms =
await EducationService.instace.getPrograms();
programs = newPrograms;
}
if (honors.isEmpty) {
List<Honor> newHonors = await EducationService.instace.getHonors();
honors = newHonors;
}
emit(EditEducationState(
schools: schools,
programs: programs,
honors: honors,
educationalBackground: event.educationalBackground));
} catch (e) {
emit(EducationalBackgroundErrorState(message: e.toString()));
}
});
////delete
on<DeleteEducation>((event, emit) async {
try {
final bool success = await EducationService.instace.delete(
profileId: event.profileId,
token: event.token,
educationalBackground: event.educationalBackground);
if (success) {
educationalBackgrounds.removeWhere(
(element) => element.id == event.educationalBackground.id);
emit(EducationDeletedState(success: success));
} else {
emit(EducationDeletedState(success: success));
}
} catch (e) {
emit(EducationalBackgroundErrorState(message: e.toString()));
}
});
}
}