assign area barangay area type add response with bloc

feature/passo/PASSO-#1-Sync-data-from-device-to-postgre-and-vice-versa
PGAN-MIS 2023-09-20 09:35:34 +08:00
parent 6dd9216af7
commit 6a42e52d87
5 changed files with 165 additions and 70 deletions

View File

@ -1,8 +1,10 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:unit2/model/location/barangay.dart';
import 'package:unit2/model/rbac/assigned_role.dart';
import 'package:unit2/model/rbac/rbac.dart';
import 'package:unit2/sevices/roles/rbac_operations/assigned_area_services.dart';
import '../../../../model/location/barangay2.dart';
import '../../../../model/profile/assigned_area.dart';
import '../../../../model/profile/basic_information/primary-information.dart';
import '../../../../sevices/roles/rbac_operations/role_assignment_services.dart';
@ -41,7 +43,10 @@ class AssignAreaBloc extends Bloc<AssignAreaEvent, AssignAreaState> {
roles.add(role.role!);
}
emit(AssignedAreaLoadedState(
userAssignedAreas: userAssignedAreas, fullname: fullname!,roles: roles,userId: id));
userAssignedAreas: userAssignedAreas,
fullname: fullname!,
roles: roles,
userId: id));
} else {
id = 0;
emit(UserNotExistError());
@ -50,22 +55,70 @@ class AssignAreaBloc extends Bloc<AssignAreaEvent, AssignAreaState> {
emit(AssignAreaErorState(message: e.toString()));
}
});
on<AddAssignArea>((event,emit)async{
try{
on<AddAssignArea>((event, emit) async {
// try {
emit(AssignAreaLoadingState());
Map<dynamic,dynamic> response = await RbacAssignedAreaServices.instance.add(userId: event.userId, roleId: event.roleId, areaTypeId: event.areaTypeId, areaId: event.areaId);
if(response["success"]){
Map<dynamic, dynamic> response = await RbacAssignedAreaServices.instance
.add(
userId: event.userId,
roleId: event.roleId,
areaTypeId: event.areaTypeId,
areaId: event.areaId);
if (response["success"]) {
UserAssignedArea newAssignArea = userAssignedAreas.firstWhere(
(element) =>
element.assignedRole!.role!.id == event.roleId &&
element.assignedRole!.user!.id == event.userId);
userAssignedAreas.removeWhere((element) =>
element.assignedRole!.role!.id == event.roleId &&
element.assignedRole!.user!.id == event.userId);
////barangay
if (event.areaTypeId == 1) {
List<dynamic> newAreas = [];
for (var area in response['data']) {
for (var assignedArea in area['assigned_area']) {
var newArea = {};
newArea.addAll({"id": assignedArea['id']});
newArea.addAll({'isactive': assignedArea['isactive']});
Barangay2 newBarangay = Barangay2.fromJson(assignedArea['area']);
newArea.addAll({
'area': {
"brgycode": newBarangay.brgycode,
"brgydesc": newBarangay.brgydesc,
"citymuncode": newBarangay.citymuncode
}
});
newAreas.add(newArea);
}
}
newAssignArea.assignedArea = newAreas;
userAssignedAreas.add(newAssignArea);
//// purok
}
if (event.areaTypeId == 2) {
//// station
}
if (event.areaTypeId == 4) {
////agency
}
if (event.areaTypeId == 3) {}
emit(AssignAreaAddedState(response: response));
}else{
} else {
emit(AssignAreaAddedState(response: response));
}
}catch(e){
emit(AssignAreaErorState(message: e.toString()));
}
// } catch (e) {
// emit(AssignAreaErorState(message: e.toString()));
// }
});
on<LoadAssignedAreas>((event, emit) async {
emit(AssignedAreaLoadedState(
userAssignedAreas: userAssignedAreas, fullname: fullname!,roles: roles,userId: event.userId));
userAssignedAreas: userAssignedAreas,
fullname: fullname!,
roles: roles,
userId: event.userId));
});
on<DeleteAssignedArea>((event, emit) async {
emit(AssignAreaLoadingState());
@ -73,6 +126,11 @@ class AssignAreaBloc extends Bloc<AssignAreaEvent, AssignAreaState> {
bool success = await RbacAssignedAreaServices.instance
.deleteAssignedArea(areaId: event.areaId);
if (success) {
for (var area in userAssignedAreas) {
area.assignedArea.removeWhere((var a) {
return a['id'] == event.areaId;
});
}
emit(AssignedAreaDeletedState(success: success));
} else {
emit(AssignedAreaDeletedState(success: success));

View File

@ -41,6 +41,7 @@ class AddAssignArea extends AssignAreaEvent {
final int roleId;
final int areaTypeId;
final String areaId;
const AddAssignArea(
{required this.areaId,
required this.areaTypeId,

View File

@ -0,0 +1,41 @@
// To parse this JSON data, do
//
// final barangay = barangayFromJson(jsonString);
import 'package:meta/meta.dart';
import 'dart:convert';
import 'city.dart';
import 'provinces.dart';
Barangay2 barangayFromJson(String str) => Barangay2.fromJson(json.decode(str));
String barangayToJson(Barangay2 data) => json.encode(data.toJson());
class Barangay2 {
Barangay2({
required this.brgycode,
required this.brgydesc,
required this.citymuncode,
});
final String? brgycode;
final String? brgydesc;
final String? citymuncode;
factory Barangay2.fromJson(Map<String, dynamic> json) => Barangay2(
brgycode: json["brgycode"],
brgydesc: json["brgydesc"],
citymuncode: json['citymuncode']
);
Map<String, dynamic> toJson() => {
"brgycode": brgycode,
"brgydesc": brgydesc,
"citymuncode": citymuncode
};
}

View File

@ -6,7 +6,7 @@ import '../roles/pass_check/assign_role_area_type.dart';
class UserAssignedArea {
final AssignedRole? assignedRole;
final AssignRoleAreaType? assignedRoleAreaType;
final dynamic assignedArea;
dynamic assignedArea;
UserAssignedArea({
required this.assignedRole,

View File

@ -1,7 +1,5 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
@ -17,24 +15,15 @@ import 'package:unit2/model/location/barangay.dart';
import 'package:unit2/model/location/city.dart';
import 'package:unit2/model/location/provinces.dart';
import 'package:unit2/model/location/purok.dart';
import 'package:unit2/model/location/region.dart';
import 'package:unit2/model/login_data/user_info/assigned_area.dart';
import 'package:unit2/model/profile/assigned_area.dart';
import 'package:unit2/model/rbac/rbac.dart';
import 'package:unit2/model/rbac/rbac_station.dart';
import 'package:unit2/model/roles/pass_check/agency_area_type.dart';
import 'package:unit2/model/roles/pass_check/purok_assign_area.dart';
import 'package:unit2/model/roles/pass_check/station_assign_area.dart';
import 'package:unit2/model/utils/agency.dart';
import 'package:unit2/model/utils/category.dart';
import 'package:unit2/sevices/roles/rbac_operations/station_services.dart';
import 'package:unit2/test_data.dart';
import 'package:unit2/theme-data.dart/btn-style.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:unit2/theme-data.dart/form-style.dart';
import 'package:unit2/utils/alerts.dart';
import 'package:unit2/utils/formatters.dart';
import 'package:unit2/utils/global_context.dart';
import 'package:unit2/utils/location_utilities.dart';
import 'package:unit2/utils/profile_utilities.dart';
import 'package:unit2/widgets/Leadings/add_leading.dart';
@ -60,7 +49,6 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
bool cityAsyncCall = false;
bool barangayAsyncCall = false;
bool purokAsyncCall = false;
List<UserAssignedArea> userAssignedAreas = [];
final bloc = BlocProvider.of<AssignAreaBloc>(context);
int? areaTypeId;
int? roleId;
@ -76,9 +64,7 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
List<CityMunicipality> cities = [];
List<Barangay> barangays = [];
List<Purok> puroks = [];
Province? selectedProvince;
CityMunicipality? selectedMunicipality;
Barangay? selectedBarangay;
return Scaffold(
appBar: AppBar(
backgroundColor: primary,
@ -152,7 +138,7 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
bloc.add(CallErrorState(
message: e.toString()));
}
////===========
//// purok
} else if (e.name!.toLowerCase() ==
"purok president") {
@ -206,7 +192,7 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
"process server") {
try {
areaType = "station";
areaId = "4";
areaTypeId = 4;
setState(() {
agencyAsyncCall = true;
});
@ -376,10 +362,23 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
)),
////agency suggestion tap
onSuggestionTap:
(agency) {
(agency) async {
setState(() {
stationAsyncCall =
true;
});
stations = await RbacStationServices
.instance
.getStations(
agencyId: agency
.item!
.id
.toString());
agencyFocusNode
.unfocus();
setState(() {
stationAsyncCall =
false;
});
},
emptyWidget: const Text(
@ -415,7 +414,9 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
);
}).toList(),
onChanged:
(RbacStation? e) {},
(RbacStation? e) {
areaId = e!.code;
},
validator:
FormBuilderValidators
.required(
@ -458,8 +459,6 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
onChanged: (Province?
province) async {
setState(() {
selectedProvince =
province;
cityAsyncCall =
true;
});
@ -645,7 +644,6 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
(Province?
province) async {
setState(() {
selectedProvince =
province;
cityAsyncCall =
true;
@ -736,7 +734,8 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
setState(() {
barangayAsyncCall =
false;
purokAsyncCall = true;
purokAsyncCall =
true;
});
puroks = await LocationUtils
.instance
@ -744,8 +743,9 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
barangay:
barangays[0].code!);
setState((){
purokAsyncCall = false;
setState(() {
purokAsyncCall =
false;
});
},
@ -773,7 +773,6 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
),
////Barangay
Expanded(
child: SizedBox(
height: 60,
child:
@ -784,25 +783,25 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
child:
DropdownButtonFormField<
Barangay>(
isExpanded: true,
onChanged:
(Barangay?
baragay)async {
onChanged: (Barangay?
baragay) async {
areaId =
baragay!
.code;
setState((){
purokAsyncCall= true;
setState(() {
purokAsyncCall =
true;
});
puroks = await LocationUtils
.instance
.getPurok(
barangay:
barangays[0].code!);
setState((){
purokAsyncCall = false;
setState(() {
purokAsyncCall =
false;
});
},
decoration:
@ -815,7 +814,6 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
errorText:
"This field is required"),
items: barangays
.isEmpty
? []
@ -829,8 +827,6 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
child:
Text(barangay.description!));
}).toList(),
),
),
),
@ -851,8 +847,7 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
DropdownButtonFormField<
Purok>(
isExpanded: true,
onChanged:
(Purok?
onChanged: (Purok?
purok) {
areaId =
purok!.code;
@ -985,7 +980,7 @@ class _RbacAssignedAreaScreenState extends State<RbacAssignedAreaScreen> {
assignedAreas = {};
roles = state.roles;
userId = state.userId;
userAssignedAreas = state.userAssignedAreas;
if (state.userAssignedAreas.isNotEmpty) {
for (var roleMod in state.userAssignedAreas) {
assignedAreas.addAll({