From e921eaf7bdd72b84506bb180cef38ffe9492f4d1 Mon Sep 17 00:00:00 2001 From: PGAN-MIS Date: Fri, 31 Mar 2023 10:27:45 +0800 Subject: [PATCH] refactor skills hobbies bloc and screen --- .../hobbies/hoobies_bloc.dart | 9 +- .../hobbies/hoobies_event.dart | 4 +- .../hobbies/hoobies_state.dart | 3 + .../skills_and_hobbies_screen.dart | 281 +++++++++++------- .../skills_hobbies/add_modal.dart | 2 +- lib/sevices/skillshobbies_services.dart | 21 +- lib/utils/urls.dart | 4 +- 7 files changed, 203 insertions(+), 121 deletions(-) diff --git a/lib/bloc/profile/other_information/hobbies/hoobies_bloc.dart b/lib/bloc/profile/other_information/hobbies/hoobies_bloc.dart index b612e61..fd8d923 100644 --- a/lib/bloc/profile/other_information/hobbies/hoobies_bloc.dart +++ b/lib/bloc/profile/other_information/hobbies/hoobies_bloc.dart @@ -31,6 +31,7 @@ class HoobiesBloc extends Bloc { on((event, emit) async { emit(HobbiesLoadingState()); try { + ////get all skills and hobbies if (allSkillsAndHobbies.isEmpty) { allSkillsAndHobbies = await SkillsHobbiesServices.instance.getAllSkillsHobbies(); @@ -99,7 +100,7 @@ class HoobiesBloc extends Bloc { ////DELETE on((event,emit)async{ emit(HobbiesLoadingState()); - try{ + // try{ bool success = await SkillsHobbiesServices.instance.delete(profileId: event.profileId, token: event.token, skillsHobbies: event.skillsHobbies); if(success){ skillsAndHobbies.removeWhere((element) => element.id == event.skillsHobbies[0].id); @@ -107,9 +108,9 @@ class HoobiesBloc extends Bloc { }else{ emit(HobbiesAndSkillsDeletedState(skillsHobbies: skillsAndHobbies, success: success)); } - }catch(e){ - emit (HobbiesErrorState(message: e.toString())); - } + // }catch(e){ + // emit (HobbiesErrorState(message: e.toString())); + // } }); } } diff --git a/lib/bloc/profile/other_information/hobbies/hoobies_event.dart b/lib/bloc/profile/other_information/hobbies/hoobies_event.dart index 5a718af..7a9abb5 100644 --- a/lib/bloc/profile/other_information/hobbies/hoobies_event.dart +++ b/lib/bloc/profile/other_information/hobbies/hoobies_event.dart @@ -15,8 +15,8 @@ class GetSkillsHobbies extends HobbiesEvent{ List get props => [profileId,token]; } class ShowHobbySkillAddForm extends HobbiesEvent{ - final List mySkillsAndHobbies; - const ShowHobbySkillAddForm({required this.mySkillsAndHobbies}); + + const ShowHobbySkillAddForm(); } class AddHobbyAndSkills extends HobbiesEvent{ diff --git a/lib/bloc/profile/other_information/hobbies/hoobies_state.dart b/lib/bloc/profile/other_information/hobbies/hoobies_state.dart index a2d39ac..858fd3a 100644 --- a/lib/bloc/profile/other_information/hobbies/hoobies_state.dart +++ b/lib/bloc/profile/other_information/hobbies/hoobies_state.dart @@ -36,9 +36,12 @@ class AddHobbySkillState extends HobbiesState{ class HobbiesLoadingState extends HobbiesState{ } + +//// show add modal for adding new skills and hobbies class ShowAddModalState extends HobbiesState{ } +//// hobbies and skills already added class HobbiesAndSkillsAddedState extends HobbiesState{ final List mySkillsAndHobbies; final Map status; diff --git a/lib/screens/profile/components/other_information/skills_and_hobbies_screen.dart b/lib/screens/profile/components/other_information/skills_and_hobbies_screen.dart index 7096b37..727b909 100644 --- a/lib/screens/profile/components/other_information/skills_and_hobbies_screen.dart +++ b/lib/screens/profile/components/other_information/skills_and_hobbies_screen.dart @@ -20,15 +20,23 @@ import '../../../../bloc/profile/other_information/hobbies/hoobies_bloc.dart'; import '../../../../theme-data.dart/btn-style.dart'; import '../../../../utils/alerts.dart'; -class SkillHobbiesScreen extends StatelessWidget { +class SkillHobbiesScreen extends StatefulWidget { const SkillHobbiesScreen({super.key}); + @override + State createState() => _SkillHobbiesScreenState(); +} + +class _SkillHobbiesScreenState extends State { @override Widget build(BuildContext context) { String token; int profileId; final bloc = BlocProvider.of(context); - List? mySkillsAndHobbies; + String output = ''; + String? deletedChip, deletedChipIndex; + final keySimpleChipsInput = GlobalKey(); + final FocusNode focusNode = FocusNode(); return Scaffold( appBar: AppBar( title: const Text(skillAndHobbiesTitle), @@ -37,13 +45,78 @@ class SkillHobbiesScreen extends StatelessWidget { actions: context.watch().state is AddHobbySkillState ? [ AddLeading(onPressed: () { - context.read().add(ShowAddModal()); + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: const Text("Add Skills and Hobbies"), + content: SimpleChipsInput( + separatorCharacter: ",", + createCharacter: ",", + focusNode: focusNode, + validateInput: true, + autoFocus: true, + formKey: keySimpleChipsInput, + onSubmitted: (p0) { + setState(() { + output = p0; + }); + }, + onChipDeleted: (p0, p1) { + setState(() { + deletedChip = p0; + deletedChipIndex = p1.toString(); + }); + }, + onSaved: ((p0) { + setState(() { + output = p0; + }); + }), + chipTextStyle: const TextStyle( + color: Colors.white, + fontSize: 16, + ), + deleteIcon: const Icon( + Icons.delete, + size: 14.0, + color: second, + ), + widgetContainerDecoration: BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.circular(16.0), + border: Border.all(color: Colors.blue[100]!), + ), + chipContainerDecoration: BoxDecoration( + color: second, + borderRadius: BorderRadius.circular(50), + ), + placeChipsSectionAbove: false, + ), + actions: [ + ElevatedButton( + onPressed: () { + keySimpleChipsInput.currentState!.save(); + Navigator.of(context, rootNavigator: true).pop('dialog'); + bloc.add( + GetAddedHobbiesSkills( + addedHobbiesSkills: output)); + + }, + style: mainBtnStyle( + primary, Colors.transparent, second), + child: const Text(submit), + ) + ], + ); + }); }) ] : [ AddLeading(onPressed: () { - context.read().add(ShowHobbySkillAddForm( - mySkillsAndHobbies: mySkillsAndHobbies!)); + bloc.add(const ShowHobbySkillAddForm( + )); + }) ]), body: ProgressHUD( @@ -51,6 +124,7 @@ class SkillHobbiesScreen extends StatelessWidget { backgroundColor: Colors.black87, indicatorWidget: const SpinKitFadingCircle(color: Colors.white), child: BlocBuilder( + buildWhen: (previous, current) => false, builder: (context, state) { if (state is UserLoggedIn) { token = state.userData!.user!.login!.token!; @@ -118,7 +192,7 @@ class SkillHobbiesScreen extends StatelessWidget { }, builder: (context, state) { if (state is HobbiesLoadedState) { - mySkillsAndHobbies = state.skillsAndHobbies; + if (state.skillsAndHobbies.isNotEmpty) { return Padding( padding: const EdgeInsets.all(12), @@ -136,15 +210,15 @@ class SkillHobbiesScreen extends StatelessWidget { return Wrap( children: [ Container( - padding: const EdgeInsets.only( - left: 6), - + padding: + const EdgeInsets.only(left: 6), child: Wrap( clipBehavior: Clip.antiAlias, alignment: WrapAlignment.center, crossAxisAlignment: WrapCrossAlignment.center, - runAlignment: WrapAlignment.center, + runAlignment: + WrapAlignment.center, children: [ Text( sh.name!, @@ -154,21 +228,23 @@ class SkillHobbiesScreen extends StatelessWidget { ), IconButton( onPressed: () { - confirmAlert( - context, - () => context.read().add(DeleteSkillHobbies( - profileId: - profileId, - skillsHobbies: [ - sh - ], - token: - token)), - "Delete", - "Confirm Delete"); - + confirmAlert( + context, + () => context + .read< + HoobiesBloc>() + .add(DeleteSkillHobbies( + profileId: + profileId, + skillsHobbies: [ + sh + ], + token: + token)), + "Delete", + "Confirm Delete"); }, - icon:const Icon( + icon: const Icon( Icons.delete, size: 16, color: second, @@ -191,9 +267,9 @@ class SkillHobbiesScreen extends StatelessWidget { token: token, ); } - if (state is ShowAddModalState) { - return AddModal(bloc: bloc); - } + // if (state is ShowAddModalState) { + // return AddModal(bloc: bloc); + // } return Container(); }, ); @@ -209,83 +285,80 @@ class SkillHobbiesScreen extends StatelessWidget { } } -class AddModal extends StatefulWidget { - final HoobiesBloc bloc; - const AddModal({super.key, required this.bloc}); +// class AddModal extends StatefulWidget { +// final HoobiesBloc bloc; +// const AddModal({super.key, required this.bloc}); - @override - State createState() => _AddModalState(); -} +// @override +// State createState() => _AddModalState(); +// } -String output = ''; -String? deletedChip, deletedChipIndex; -final keySimpleChipsInput = GlobalKey(); -final FocusNode focusNode = FocusNode(); -class _AddModalState extends State { - @override - Widget build(BuildContext context) { - return BlocBuilder( - builder: (context, state) { - return AlertDialog( - title: const Text("Add Skills and Hobbies"), - content: SimpleChipsInput( - separatorCharacter: ",", - createCharacter: ",", - focusNode: focusNode, - validateInput: true, - autoFocus: true, - formKey: keySimpleChipsInput, - onSubmitted: (p0) { - setState(() { - output = p0; - }); - }, - onChipDeleted: (p0, p1) { - setState(() { - deletedChip = p0; - deletedChipIndex = p1.toString(); - }); - }, - onSaved: ((p0) { - setState(() { - output = p0; - }); - }), - chipTextStyle: const TextStyle( - color: Colors.white, - fontSize: 16, - ), - deleteIcon: const Icon( - Icons.delete, - size: 14.0, - color: second, - ), - widgetContainerDecoration: BoxDecoration( - color: Colors.white, - borderRadius: BorderRadius.circular(16.0), - border: Border.all(color: Colors.blue[100]!), - ), - chipContainerDecoration: BoxDecoration( - color: second, - borderRadius: BorderRadius.circular(50), - ), - placeChipsSectionAbove: false, - ), - actions: [ - ElevatedButton( - onPressed: () { - keySimpleChipsInput.currentState!.save(); - context - .read() - .add(GetAddedHobbiesSkills(addedHobbiesSkills: output)); - }, - style: mainBtnStyle(primary, Colors.transparent, second), - child: const Text(submit), - ) - ], - ); - }, - ); - } -} + +// class _AddModalState extends State { +// @override +// Widget build(BuildContext context) { +// return BlocBuilder( +// builder: (context, state) { +// return AlertDialog( +// title: const Text("Add Skills and Hobbies"), +// content: SimpleChipsInput( +// separatorCharacter: ",", +// createCharacter: ",", +// focusNode: focusNode, +// validateInput: true, +// autoFocus: true, +// formKey: keySimpleChipsInput, +// onSubmitted: (p0) { +// setState(() { +// output = p0; +// }); +// }, +// onChipDeleted: (p0, p1) { +// setState(() { +// deletedChip = p0; +// deletedChipIndex = p1.toString(); +// }); +// }, +// onSaved: ((p0) { +// setState(() { +// output = p0; +// }); +// }), +// chipTextStyle: const TextStyle( +// color: Colors.white, +// fontSize: 16, +// ), +// deleteIcon: const Icon( +// Icons.delete, +// size: 14.0, +// color: second, +// ), +// widgetContainerDecoration: BoxDecoration( +// color: Colors.white, +// borderRadius: BorderRadius.circular(16.0), +// border: Border.all(color: Colors.blue[100]!), +// ), +// chipContainerDecoration: BoxDecoration( +// color: second, +// borderRadius: BorderRadius.circular(50), +// ), +// placeChipsSectionAbove: false, +// ), +// actions: [ +// ElevatedButton( +// onPressed: () { +// keySimpleChipsInput.currentState!.save(); +// context +// .read() +// .add(GetAddedHobbiesSkills(addedHobbiesSkills: output)); +// }, +// style: mainBtnStyle(primary, Colors.transparent, second), +// child: const Text(submit), +// ) +// ], +// ); +// }, +// ); +// } +// } diff --git a/lib/screens/profile/components/other_information/skills_hobbies/add_modal.dart b/lib/screens/profile/components/other_information/skills_hobbies/add_modal.dart index cbcdc47..8633278 100644 --- a/lib/screens/profile/components/other_information/skills_hobbies/add_modal.dart +++ b/lib/screens/profile/components/other_information/skills_hobbies/add_modal.dart @@ -36,7 +36,7 @@ class _AddHobbiesAndSkillsScreenState extends State { selectedListData: selectedList, onApplyButtonClick: (list) { // Navigator.pop(context, list); - context.read().add(AddHobbyAndSkills(profileId: widget.profileId, token: widget.token, skillsHobbies: selectedList)); + context.read().add(AddHobbyAndSkills(profileId: widget.profileId, token: widget.token, skillsHobbies: list!)); }, choiceChipLabel: (item) { diff --git a/lib/sevices/skillshobbies_services.dart b/lib/sevices/skillshobbies_services.dart index c09d67f..f998dcc 100644 --- a/lib/sevices/skillshobbies_services.dart +++ b/lib/sevices/skillshobbies_services.dart @@ -48,7 +48,11 @@ class SkillsHobbiesServices { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': authToken }; - Map body = {"skill_hobby": skillsHobbies}; + List> skillsHobbiesBody=[]; + for(var element in skillsHobbies){ + skillsHobbiesBody.add({"id":element.id,"name":element.name}); + } + Map body = {"skill_hobby": skillsHobbiesBody}; Map statusResponse = {}; try { http.Response response = await Request.instance @@ -76,21 +80,22 @@ class SkillsHobbiesServices { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': authToken }; - + Map params = {"force_mode": "true"}; Map body = { - "skill_hobby": [skillsHobbies] + "skill_hobby": [{"id":skillsHobbies[0].id, "name":skillsHobbies[0].name}] + }; - try { + // try { http.Response response = await Request.instance.deleteRequest( - path: path, headers: headers, body: body, param: {}); + path: path, headers: headers, body: body, param: params); if (response.statusCode == 200) { Map data = jsonDecode(response.body); success = data['success']; } - } catch (e) { - throw e.toString(); - } + // } catch (e) { + // throw e.toString(); + // } return success; } diff --git a/lib/utils/urls.dart b/lib/utils/urls.dart index ef2d994..38c24aa 100644 --- a/lib/utils/urls.dart +++ b/lib/utils/urls.dart @@ -5,9 +5,9 @@ class Url { String host() { // return '192.168.10.221:3003'; // return 'agusandelnorte.gov.ph'; - return "192.168.10.219:3000"; + // return "192.168.10.219:3000"; // return "devweb.agusandelnorte.gov.ph"; - // return 'devapi.agusandelnorte.gov.ph:3004'; + return 'devapi.agusandelnorte.gov.ph:3004'; } String authentication() {