From 6f251973a5a8809a34d3ac66a10b52b3f2ef49d5 Mon Sep 17 00:00:00 2001 From: venbaittech Date: Sat, 30 Aug 2025 18:22:59 +0530 Subject: [PATCH] UI_TP_SEQ_PARL --- lib/Screens/group/group.dart | 2 + lib/Screens/group/groupDetails.dart | 2 + lib/Screens/organization/orgSetup.dart | 118 ++++++++++++++++++++++--- lib/Screens/policy/policy.dart | 1 + lib/Screens/policy/policyCriteria.dart | 66 +++++++------- lib/app.dart | 6 +- lib/services/apiService.dart | 67 ++++++++++++++ 7 files changed, 217 insertions(+), 45 deletions(-) diff --git a/lib/Screens/group/group.dart b/lib/Screens/group/group.dart index a309647..5d8a252 100644 --- a/lib/Screens/group/group.dart +++ b/lib/Screens/group/group.dart @@ -262,6 +262,8 @@ class _groupState extends State { )); if (response.statusCode == 200 || response.statusCode == 201) { + print("GRPDATA"); + await apiService.handleTokenRefresh(userId!); print("Group submitted successfully!"); print("Response: ${response.body}"); context.go('/group'); diff --git a/lib/Screens/group/groupDetails.dart b/lib/Screens/group/groupDetails.dart index 738b5cb..7358dae 100644 --- a/lib/Screens/group/groupDetails.dart +++ b/lib/Screens/group/groupDetails.dart @@ -285,6 +285,8 @@ class GroupDataState extends State { // _clearError(); _clearError(); await widget.fetchGetGroup(); + + await apiService.handleTokenRefresh(userId!); if (context.mounted) { Navigator.of(context).pop(); // Close modal only if mounted } diff --git a/lib/Screens/organization/orgSetup.dart b/lib/Screens/organization/orgSetup.dart index 97d7aa5..fa8b995 100644 --- a/lib/Screens/organization/orgSetup.dart +++ b/lib/Screens/organization/orgSetup.dart @@ -52,6 +52,7 @@ class _OrgSetUpState extends State { // List selectedServiceIds = []; List> selectedServiceIds = []; + List> originalSelectedServiceIds = []; @override void initState() { @@ -280,6 +281,16 @@ class _OrgSetUpState extends State { final map = Map.from(item); return {"service_id": map['service_id'].toString()}; }).toList(); + + originalSelectedServiceIds = + services.map>((item) { + // force cast or copy to a regular map + final map = Map.from(item); + return {"service_id": map['service_id'].toString()}; + }).toList(); + + print("SEA1 -$selectedServiceIds"); + print("SEA2 -$originalSelectedServiceIds"); }); orgId = await getOrgId(); @@ -389,6 +400,7 @@ class _OrgSetUpState extends State { print(orgList); await updateOrgDataWithNewValues(orgList); + await apiService.handleTokenRefresh(userId!); print("📨 Response Organizt Update:"); // return orgList; context.go('/OrganizationSettings'); @@ -439,18 +451,104 @@ class _OrgSetUpState extends State { void handleSubmit() { print("HandleSubmiy - $orgData"); + print("Raw Service IDs: ${orgData['services_ids']}"); + print("SEA2 -$originalSelectedServiceIds"); createOrgData(orgData); - setState(() { - if (!isValidData(orgData)) { - print("USERDETAILS : $orgData"); - print("Validation Failed: Required fields are missing."); - return; // Stop execution if validation fails - } else { - print("USERDETAILS : $orgData"); - createOrgData(orgData); - } - }); + // Raw (decode JSON string to list) + // final rawServiceList = jsonDecode(orgData['services_ids']); + // + // print("Raw Service IDs: ${orgData['services_ids']}"); + // print("Raw Service IDs2: $rawServiceList"); + // print("SEA2 -$originalSelectedServiceIds"); + // final rawServiceIds = + // rawServiceList.map((e) => e['service_id'].toString()).toList(); + // + // // Original service IDs + // final originalServiceIds = + // originalSelectedServiceIds + // .map((e) => e['service_id'].toString()) + // .toList(); + // + // final addedServices = + // rawServiceIds.where((id) => !originalServiceIds.contains(id)).toList(); + // final removedServices = + // originalServiceIds.where((id) => !rawServiceIds.contains(id)).toList(); + // + // setState(() { + // if (addedServices.isNotEmpty || removedServices.isNotEmpty) { + // print("UE1"); + // _showConfirmationDialog(context); + // } else { + // print("UE2"); + // createOrgData(orgData); + // } + // }); + + // setState(() { + // if (!isValidData(orgData)) { + // print("USERDETAILS : $orgData"); + // print("Validation Failed: Required fields are missing."); + // return; // Stop execution if validation fails + // } else { + // print("USERDETAILS : $orgData"); + // + // //createOrgData(orgData); + // } + // }); + } + + void _showConfirmationDialog( + BuildContext context, + // List added, + // List removed, + ) { + print("clickTR12 "); + String message = ''; + // if (added.isNotEmpty) { + // message += "Added: ${added.join(", ")}\n"; + // } + // if (removed.isNotEmpty) { + // message += "Removed: ${removed.join(", ")}\n"; + // } + + showDialog( + context: context, + builder: + (ctx) => AlertDialog( + title: Text("Service Changes "), + content: Text( + "App will logout when service changes,Do you want to continue?", + ), + actions: [ + TextButton( + child: Text("Cancel"), + onPressed: () => Navigator.of(ctx).pop(), + ), + TextButton( + child: Text("Continue"), + onPressed: () { + Navigator.of(ctx).pop(); + logout(ctx); + // ✅ Call your function here + createOrgData(orgData); + }, + ), + ], + ), + ); + } + + Future logout(BuildContext context) async { + // Clear localStorage + final prefs = await SharedPreferences.getInstance(); + await prefs.clear(); // Clears all keys + + // Optional: clear sessionStorage if used + // html.window.sessionStorage.clear(); + + // Navigate to login or home page + context.go('/'); } @override diff --git a/lib/Screens/policy/policy.dart b/lib/Screens/policy/policy.dart index 5b6ebc3..3cf3326 100644 --- a/lib/Screens/policy/policy.dart +++ b/lib/Screens/policy/policy.dart @@ -602,6 +602,7 @@ class _PolicyState extends State { if (response.statusCode == 200) { print("policyData submitted successfully!"); + await apiService.handleTokenRefresh(userId!); print("Response: ${response.body}"); return true; } else { diff --git a/lib/Screens/policy/policyCriteria.dart b/lib/Screens/policy/policyCriteria.dart index 2bbded4..5553b43 100644 --- a/lib/Screens/policy/policyCriteria.dart +++ b/lib/Screens/policy/policyCriteria.dart @@ -1448,8 +1448,8 @@ class PolicyCriteriaState extends State { SelectedParallelProcess[ServiceId!] == "3") { setState(() { - SelectedParallelProcess[ServiceId!] = - "0"; + // SelectedParallelProcess[ServiceId!] = + // "0"; }); } print( @@ -2373,15 +2373,17 @@ class PolicyCriteriaState extends State { "None")) ? "P" : ((FourthExceptionalApproverAction[ServiceId!] != - "None") && - ((SelectedParallelExceptionalProcess[ServiceId] == - "1" || - SelectedParallelExceptionalProcess[ServiceId] == - "2" || - SelectedParallelExceptionalProcess[ServiceId] == - "3" || - SelectedParallelExceptionalProcess[ServiceId] == - "4"))) + "None") + // && + // ((SelectedParallelExceptionalProcess[ServiceId] == + // "1" || + // SelectedParallelExceptionalProcess[ServiceId] == + // "2" || + // SelectedParallelExceptionalProcess[ServiceId] == + // "3" || + // SelectedParallelExceptionalProcess[ServiceId] == + // "4")) + ) ? 'S' : '', @@ -2504,14 +2506,13 @@ class PolicyCriteriaState extends State { // Find the country_code based on selected country_name FourthExceptionalApproverAction[ServiceId!] = newValue; - + // SelectedParallelExceptionalProcess[ServiceId!] = + // "4"; if (newValue == "None" && ((SelectedParallelExceptionalProcess[ServiceId!] == "4"))) { - setState(() { - SelectedParallelExceptionalProcess[ServiceId!] = - "4"; - }); + // SelectedParallelExceptionalProcess[ServiceId!] = + // "4"; } // print("selectedUserType - $selectedUserType"); @@ -3355,8 +3356,8 @@ class PolicyCriteriaState extends State { GestureDetector( onTap: () { setState(() { - SelectedParallelAmendProcess[ServiceId!] = - "4"; + // SelectedParallelAmendProcess[ServiceId!] = + // "4"; if ((FourthAmendApproverAction[ServiceId!] == "None")) { @@ -3415,15 +3416,17 @@ class PolicyCriteriaState extends State { "None")) ? "P" : ((FourthAmendApproverAction[ServiceId!] != - "None") && - ((SelectedParallelAmendProcess[ServiceId] == - "1" || - SelectedParallelAmendProcess[ServiceId] == - "2" || - SelectedParallelAmendProcess[ServiceId] == - "3" || - SelectedParallelAmendProcess[ServiceId] == - "4"))) + "None") + // && + // ((SelectedParallelAmendProcess[ServiceId] == + // "1" || + // SelectedParallelAmendProcess[ServiceId] == + // "2" || + // SelectedParallelAmendProcess[ServiceId] == + // "3" || + // SelectedParallelAmendProcess[ServiceId] == + // "4")) + ) ? 'S' : '', @@ -3533,14 +3536,13 @@ class PolicyCriteriaState extends State { // Find the country_code based on selected country_name FourthAmendApproverAction[ServiceId!] = newValue; - + // SelectedParallelAmendProcess[ServiceId!] = + // "4"; if (newValue == "None" && ((SelectedParallelAmendProcess[ServiceId!] == "4"))) { - setState(() { - SelectedParallelAmendProcess[ServiceId!] = - "4"; - }); + // SelectedParallelAmendProcess[ServiceId!] = + // "4"; } // print("selectedUserType - $selectedUserType"); diff --git a/lib/app.dart b/lib/app.dart index 9d8f22d..c010629 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -34,8 +34,8 @@ class _MyAppState extends State { @override void initState() { super.initState(); - SemanticsBinding.instance - .ensureSemantics(); // -only for testing uncomment, Otherwise Email Template wont allow to type + // SemanticsBinding.instance + // .ensureSemantics(); // -only for testing uncomment, Otherwise Email Template wont allow to type if (kIsWeb) { final uri = Uri.parse(html.window.location.href); print("URI - $uri"); @@ -62,7 +62,7 @@ class _MyAppState extends State { setState(() { _isAuthRedirect = true; // Show loading screen ONLY for MS login }); - print("🔍 Auth code detected at startup: $authCode"); + print(" Auth code detected at startup: $authCode"); // ✅ Immediately begin token handling // WidgetsBinding.instance.addPostFrameCallback((_) { // handleTokenUsingMS(authCode); diff --git a/lib/services/apiService.dart b/lib/services/apiService.dart index b8e8560..17eef42 100644 --- a/lib/services/apiService.dart +++ b/lib/services/apiService.dart @@ -13,6 +13,42 @@ import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; class ApiService { + Future storeUserDetails(String token) async { + try { + final parts = token.split('.'); + if (parts.length != 3) throw Exception('Invalid token format'); + + final payload = json.decode( + utf8.decode(base64Url.decode(base64Url.normalize(parts[1]))), + ); + + final userData = payload['data']; + + final prefs = await SharedPreferences.getInstance(); + await prefs.setString('auth_token', token); + await prefs.setString( + 'user_data', + jsonEncode(userData), + ); // Store full user data + + if (userData != null) { + final pref = await SharedPreferences.getInstance(); + await pref.setString('auth_token', token); + await pref.setString('user_data', jsonEncode(userData)); + + // userRole = userData['role']; + + print("userData - $userData"); + print("userData11 - ${userData['role']}"); + // print("userData12 - $userRole"); + } + + await getOrganizationData(); + } catch (e) { + print('Error decoding token: $e'); + } + } + Future getOrganizationData() async { try { print("ORG getUpdatedServices"); @@ -610,6 +646,37 @@ class ApiService { } } + Future handleTokenRefresh(String userId) async { + final String apiUrldata = + '$apiUrl/api/user/refreshUserToken?user_id=$userId'; + print("API URL: $userId"); + // final token = await getToken(); + + final token = await getToken(); + + final response = await http.get( + Uri.parse(apiUrldata), + headers: { + 'Authorization': 'Bearer $token', + 'Content-Type': 'application/json', + 'app-signature': 'ts-traveltool-2025-signature-123456', + }, + ); + + if (response.statusCode == 200) { + final data = jsonDecode(response.body); + print("data- $data"); + + final token = data['token']; // Assuming the token is in response + // final userId = data['user_id'].toString(); + + print("Token - $token"); + await storeUserDetails(token); + } else { + throw Exception('Failed to load plans'); + } + } + static Future viewPlan( BuildContext context, String planId, {