fixed permissions loading issue on employee screen
This commit is contained in:
parent
45bc492683
commit
7e75431feb
@ -13,6 +13,7 @@ class PermissionController extends GetxController {
|
||||
var employeeInfo = Rxn<EmployeeInfo>();
|
||||
var projectsInfo = <ProjectInfo>[].obs;
|
||||
Timer? _refreshTimer;
|
||||
var isLoading = true.obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
@ -26,7 +27,8 @@ class PermissionController extends GetxController {
|
||||
await loadData(token!);
|
||||
_startAutoRefresh();
|
||||
} else {
|
||||
logSafe("Token is null or empty. Skipping API load and auto-refresh.", level: LogLevel.warning);
|
||||
logSafe("Token is null or empty. Skipping API load and auto-refresh.",
|
||||
level: LogLevel.warning);
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,19 +39,24 @@ class PermissionController extends GetxController {
|
||||
logSafe("Auth token retrieved: $token", level: LogLevel.debug);
|
||||
return token;
|
||||
} catch (e, stacktrace) {
|
||||
logSafe("Error retrieving auth token", level: LogLevel.error, error: e, stackTrace: stacktrace);
|
||||
logSafe("Error retrieving auth token",
|
||||
level: LogLevel.error, error: e, stackTrace: stacktrace);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadData(String token) async {
|
||||
try {
|
||||
isLoading.value = true;
|
||||
final userData = await PermissionService.fetchAllUserData(token);
|
||||
_updateState(userData);
|
||||
await _storeData();
|
||||
logSafe("Data loaded and state updated successfully.");
|
||||
} catch (e, stacktrace) {
|
||||
logSafe("Error loading data from API", level: LogLevel.error, error: e, stackTrace: stacktrace);
|
||||
logSafe("Error loading data from API",
|
||||
level: LogLevel.error, error: e, stackTrace: stacktrace);
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,7 +67,8 @@ class PermissionController extends GetxController {
|
||||
projectsInfo.assignAll(userData['projects']);
|
||||
logSafe("State updated with user data.");
|
||||
} catch (e, stacktrace) {
|
||||
logSafe("Error updating state", level: LogLevel.error, error: e, stackTrace: stacktrace);
|
||||
logSafe("Error updating state",
|
||||
level: LogLevel.error, error: e, stackTrace: stacktrace);
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,7 +97,8 @@ class PermissionController extends GetxController {
|
||||
|
||||
logSafe("User data successfully stored in SharedPreferences.");
|
||||
} catch (e, stacktrace) {
|
||||
logSafe("Error storing data", level: LogLevel.error, error: e, stackTrace: stacktrace);
|
||||
logSafe("Error storing data",
|
||||
level: LogLevel.error, error: e, stackTrace: stacktrace);
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,20 +109,23 @@ class PermissionController extends GetxController {
|
||||
if (token?.isNotEmpty ?? false) {
|
||||
await loadData(token!);
|
||||
} else {
|
||||
logSafe("Token missing during auto-refresh. Skipping.", level: LogLevel.warning);
|
||||
logSafe("Token missing during auto-refresh. Skipping.",
|
||||
level: LogLevel.warning);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bool hasPermission(String permissionId) {
|
||||
final hasPerm = permissions.any((p) => p.id == permissionId);
|
||||
logSafe("Checking permission $permissionId: $hasPerm", level: LogLevel.debug);
|
||||
logSafe("Checking permission $permissionId: $hasPerm",
|
||||
level: LogLevel.debug);
|
||||
return hasPerm;
|
||||
}
|
||||
|
||||
bool isUserAssignedToProject(String projectId) {
|
||||
final assigned = projectsInfo.any((project) => project.id == projectId);
|
||||
logSafe("Checking project assignment for $projectId: $assigned", level: LogLevel.debug);
|
||||
logSafe("Checking project assignment for $projectId: $assigned",
|
||||
level: LogLevel.debug);
|
||||
return assigned;
|
||||
}
|
||||
|
||||
|
@ -248,7 +248,16 @@ class _EmployeesScreenState extends State<EmployeesScreen> with UIMixin {
|
||||
}
|
||||
|
||||
Widget _buildFloatingActionButton() {
|
||||
if (!permissionController.hasPermission(Permissions.manageEmployees)) {
|
||||
return Obx(() {
|
||||
// Show nothing while permissions are loading
|
||||
if (permissionController.isLoading.value) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
// Show FAB only if user has Manage Employees permission
|
||||
final hasPermission =
|
||||
permissionController.hasPermission(Permissions.manageEmployees);
|
||||
if (!hasPermission) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
@ -262,7 +271,10 @@ class _EmployeesScreenState extends State<EmployeesScreen> with UIMixin {
|
||||
borderRadius: BorderRadius.circular(28),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Colors.black26, blurRadius: 6, offset: Offset(0, 3))
|
||||
color: Colors.black26,
|
||||
blurRadius: 6,
|
||||
offset: Offset(0, 3),
|
||||
)
|
||||
],
|
||||
),
|
||||
child: const Row(
|
||||
@ -275,6 +287,7 @@ class _EmployeesScreenState extends State<EmployeesScreen> with UIMixin {
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildSearchAndActionRow() {
|
||||
@ -371,7 +384,9 @@ class _EmployeesScreenState extends State<EmployeesScreen> with UIMixin {
|
||||
}
|
||||
|
||||
Widget _buildPopupMenu() {
|
||||
if (!permissionController.hasPermission(Permissions.viewAllEmployees)) {
|
||||
return Obx(() {
|
||||
if (permissionController.isLoading.value ||
|
||||
!permissionController.hasPermission(Permissions.viewAllEmployees)) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
@ -425,6 +440,7 @@ class _EmployeesScreenState extends State<EmployeesScreen> with UIMixin {
|
||||
),
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildEmployeeList() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user