2023-03-02 05:28:33 +00:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
import 'package:equatable/equatable.dart';
|
2023-03-07 02:31:28 +00:00
|
|
|
import '../../../model/location/city.dart';
|
|
|
|
import '../../../model/location/country.dart';
|
|
|
|
import '../../../model/location/provinces.dart';
|
|
|
|
import '../../../model/location/region.dart';
|
|
|
|
import '../../../model/profile/eligibility.dart';
|
|
|
|
import '../../../model/utils/eligibility.dart';
|
|
|
|
import '../../../sevices/profile/eligibility_services.dart';
|
|
|
|
import '../../../utils/location_utilities.dart';
|
|
|
|
import '../../../utils/profile_utilities.dart';
|
2023-03-02 05:28:33 +00:00
|
|
|
part 'eligibility_event.dart';
|
|
|
|
part 'eligibility_state.dart';
|
|
|
|
|
|
|
|
class EligibilityBloc extends Bloc<EligibilityEvent, EligibilityState> {
|
|
|
|
EligibilityBloc() : super(EligibilityInitial()) {
|
|
|
|
List<Country>? globalCountries;
|
|
|
|
List<Region>? globalRegions;
|
2023-04-11 01:27:53 +00:00
|
|
|
List<Eligibility> globalEligibilities = [];
|
|
|
|
List<EligibityCert> eligibilities = [];
|
|
|
|
//// LOAD ELIGIBILTY
|
2023-03-02 05:28:33 +00:00
|
|
|
on<LoadEligibility>((event, emit) {
|
|
|
|
emit(EligibilityLoadingState());
|
2023-04-11 01:27:53 +00:00
|
|
|
if (eligibilities.isEmpty) {
|
|
|
|
GetEligibilities(profileId: event.profileId!, token: event.token!);
|
|
|
|
} else {
|
|
|
|
emit(EligibilityLoaded(eligibilities: eligibilities));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//// DELETE
|
|
|
|
on<DeleteEligibility>((event, emit) async {
|
|
|
|
try {
|
|
|
|
final bool success = await EligibilityService.instance.delete(
|
|
|
|
eligibilityId: event.eligibilityId,
|
|
|
|
profileId: int.parse(event.profileId),
|
|
|
|
token: event.token);
|
|
|
|
if (success) {
|
|
|
|
eligibilities.removeWhere(
|
|
|
|
((EligibityCert element) => element.id == event.eligibilityId));
|
|
|
|
emit(DeletedState(
|
|
|
|
success: success,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
emit(DeletedState(success: success));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
2023-03-02 05:28:33 +00:00
|
|
|
});
|
2023-04-11 01:27:53 +00:00
|
|
|
|
|
|
|
//// GET ELIGIBILITY
|
2023-03-02 05:28:33 +00:00
|
|
|
on<GetEligibilities>((event, emit) async {
|
|
|
|
try {
|
2023-04-11 01:27:53 +00:00
|
|
|
if (eligibilities.isNotEmpty) {
|
|
|
|
emit(EligibilityLoaded(eligibilities: eligibilities));
|
2023-03-02 05:28:33 +00:00
|
|
|
} else {
|
|
|
|
emit(EligibilityLoadingState());
|
|
|
|
eligibilities = await EligibilityService.instance
|
|
|
|
.getEligibilities(event.profileId, event.token);
|
2023-04-11 01:27:53 +00:00
|
|
|
emit(EligibilityLoaded(eligibilities: eligibilities));
|
2023-03-02 05:28:33 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
});
|
2023-04-11 01:27:53 +00:00
|
|
|
//// SHOW EDIT FORM
|
2023-03-02 05:28:33 +00:00
|
|
|
on<ShowEditEligibilityForm>((event, emit) async {
|
|
|
|
try {
|
|
|
|
if (globalCountries == null) {
|
|
|
|
List<Country> countries = await LocationUtils.instance.getCountries();
|
|
|
|
globalCountries = countries;
|
|
|
|
}
|
|
|
|
if (globalRegions == null) {
|
|
|
|
List<Region> regions = await LocationUtils.instance.getRegions();
|
|
|
|
globalRegions = regions;
|
|
|
|
}
|
2023-04-11 01:27:53 +00:00
|
|
|
if (globalEligibilities.isEmpty) {
|
2023-03-02 05:28:33 +00:00
|
|
|
List<Eligibility> eligibilities =
|
|
|
|
await ProfileUtilities.instance.getEligibilities();
|
|
|
|
globalEligibilities = eligibilities;
|
|
|
|
}
|
2023-04-11 01:27:53 +00:00
|
|
|
Eligibility currentEligibility = globalEligibilities.firstWhere(
|
2023-03-02 05:28:33 +00:00
|
|
|
(Eligibility eligibility) =>
|
|
|
|
event.eligibityCert.eligibility!.id == eligibility.id);
|
|
|
|
bool? isOverseas = event.eligibityCert.overseas;
|
|
|
|
Country currentCountry = globalCountries!.firstWhere(
|
|
|
|
(Country country) =>
|
|
|
|
event.eligibityCert.examAddress!.country!.code == country.code);
|
|
|
|
if (event.eligibityCert.examAddress?.cityMunicipality?.province
|
|
|
|
?.region !=
|
|
|
|
null) {
|
|
|
|
Region currrentRegion = globalRegions!.firstWhere((Region region) =>
|
|
|
|
event.eligibityCert.examAddress!.cityMunicipality!.province!
|
|
|
|
.region!.code ==
|
|
|
|
region.code);
|
|
|
|
List<Province> provinces = await LocationUtils.instance
|
|
|
|
.getProvinces(regionCode: currrentRegion.code.toString());
|
|
|
|
Province currentProvince = provinces.firstWhere((Province province) =>
|
|
|
|
event.eligibityCert.examAddress!.cityMunicipality!.province!
|
|
|
|
.code ==
|
|
|
|
province.code);
|
|
|
|
List<CityMunicipality> cities = await LocationUtils.instance
|
|
|
|
.getCities(code: currentProvince.code.toString());
|
|
|
|
CityMunicipality currentCity = cities.firstWhere(
|
|
|
|
(CityMunicipality cityMunicipality) =>
|
|
|
|
event.eligibityCert.examAddress!.cityMunicipality!.code ==
|
|
|
|
cityMunicipality.code);
|
|
|
|
|
|
|
|
emit(EditEligibilityState(
|
|
|
|
currentCity: currentCity,
|
|
|
|
selectedCountry: currentCountry,
|
|
|
|
currentProvince: currentProvince,
|
|
|
|
currentRegion: currrentRegion,
|
|
|
|
currentEligibility: currentEligibility,
|
|
|
|
provinces: provinces,
|
|
|
|
cities: cities,
|
|
|
|
isOverseas: isOverseas!,
|
|
|
|
eligibityCert: event.eligibityCert,
|
|
|
|
countries: globalCountries!,
|
|
|
|
regions: globalRegions!,
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities: globalEligibilities));
|
2023-03-02 05:28:33 +00:00
|
|
|
} else {
|
|
|
|
emit(EditEligibilityState(
|
|
|
|
selectedCountry: currentCountry,
|
|
|
|
currentCity: null,
|
|
|
|
currentProvince: null,
|
|
|
|
currentRegion: null,
|
|
|
|
provinces: null,
|
|
|
|
cities: null,
|
|
|
|
currentEligibility: currentEligibility,
|
|
|
|
isOverseas: isOverseas!,
|
|
|
|
eligibityCert: event.eligibityCert,
|
|
|
|
countries: globalCountries!,
|
|
|
|
regions: globalRegions!,
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities: globalEligibilities));
|
2023-03-02 05:28:33 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-11 01:27:53 +00:00
|
|
|
//// UPDATE
|
2023-03-02 05:28:33 +00:00
|
|
|
on<UpdateEligibility>((event, emit) async {
|
|
|
|
try {
|
|
|
|
Map<dynamic, dynamic> status = await EligibilityService.instance.update(
|
|
|
|
eligibityCert: event.eligibityCert,
|
|
|
|
token: event.token,
|
|
|
|
profileId: int.parse(event.profileId),
|
|
|
|
oldEligibility: event.oldEligibility);
|
|
|
|
if (status['success']) {
|
|
|
|
EligibityCert newEligibility = EligibityCert.fromJson(status['data']);
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities.removeWhere(
|
2023-03-02 05:28:33 +00:00
|
|
|
(EligibityCert element) => element.id == event.eligibityCert.id);
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities.add(newEligibility);
|
|
|
|
emit(EligibilityEditedState(response: status));
|
2023-03-02 05:28:33 +00:00
|
|
|
} else {
|
2023-04-11 01:27:53 +00:00
|
|
|
emit(EligibilityEditedState(response: status));
|
2023-03-02 05:28:33 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
});
|
2023-04-11 01:27:53 +00:00
|
|
|
//// SHOW ADD FORM
|
2023-03-02 05:28:33 +00:00
|
|
|
on<ShowAddEligibilityForm>((event, emit) async {
|
|
|
|
emit(EligibilityLoadingState());
|
|
|
|
if (globalRegions == null) {
|
|
|
|
List<Region> regions = await LocationUtils.instance.getRegions();
|
|
|
|
globalRegions = regions;
|
|
|
|
}
|
2023-04-11 01:27:53 +00:00
|
|
|
if (globalEligibilities.isEmpty) {
|
2023-03-02 05:28:33 +00:00
|
|
|
List<Eligibility> eligibilities =
|
|
|
|
await ProfileUtilities.instance.getEligibilities();
|
|
|
|
globalEligibilities = eligibilities;
|
|
|
|
}
|
|
|
|
if (globalCountries == null) {
|
|
|
|
List<Country> countries = await LocationUtils.instance.getCountries();
|
|
|
|
globalCountries = countries;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(AddEligibilityState(
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities: globalEligibilities,
|
2023-03-02 05:28:33 +00:00
|
|
|
regions: globalRegions!,
|
|
|
|
countries: globalCountries!));
|
|
|
|
});
|
2023-04-11 01:27:53 +00:00
|
|
|
|
|
|
|
//// ADD
|
2023-03-02 05:28:33 +00:00
|
|
|
on<AddEligibility>(
|
|
|
|
(event, emit) async {
|
|
|
|
try {
|
|
|
|
Map<dynamic, dynamic> status = await EligibilityService.instance.add(
|
|
|
|
eligibityCert: event.eligibityCert,
|
|
|
|
token: event.token,
|
|
|
|
profileId: int.parse(event.profileId));
|
|
|
|
if (status['success']) {
|
|
|
|
EligibityCert? eligibityCert =
|
|
|
|
EligibityCert.fromJson(status['data']);
|
2023-04-11 01:27:53 +00:00
|
|
|
eligibilities.add(eligibityCert);
|
|
|
|
emit(EligibilityAddedState(response: status));
|
2023-03-02 05:28:33 +00:00
|
|
|
} else {
|
2023-04-11 01:27:53 +00:00
|
|
|
emit(EligibilityAddedState(response: status));
|
2023-03-02 05:28:33 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
emit(EligibilityErrorState(message: e.toString()));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
on<CallErrorState>((event, emit) {
|
|
|
|
emit(const EligibilityErrorState(
|
|
|
|
message: "Something went wrong. Please try again"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|