store current user id
This commit is contained in:
@@ -88,14 +88,14 @@ class _ProductDetailPageState extends ConsumerState<ProductDetailPage> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// Auto-select warehouse user based on stored email from login
|
||||
/// Auto-select warehouse user based on stored user ID from login
|
||||
Future<void> _autoSelectWarehouseUser() async {
|
||||
try {
|
||||
// Get stored email from secure storage
|
||||
// Get stored current user ID from secure storage
|
||||
final secureStorage = SecureStorage();
|
||||
final storedEmail = await secureStorage.getEmail();
|
||||
|
||||
if (storedEmail == null || storedEmail.isEmpty) {
|
||||
final currentUserId = await secureStorage.getCurrentUserId();
|
||||
print('user $currentUserId');
|
||||
if (currentUserId == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -104,9 +104,9 @@ class _ProductDetailPageState extends ConsumerState<ProductDetailPage> {
|
||||
.where((user) => user.isWareHouseUser)
|
||||
.toList();
|
||||
|
||||
// Find user with matching email
|
||||
// Find user with matching ID
|
||||
final matchingUsers = warehouseUsers
|
||||
.where((user) => user.email.toLowerCase() == storedEmail.toLowerCase())
|
||||
.where((user) => user.id == currentUserId)
|
||||
.toList();
|
||||
|
||||
final matchingUser = matchingUsers.isNotEmpty ? matchingUsers.first : null;
|
||||
@@ -656,7 +656,9 @@ class _ProductDetailPageState extends ConsumerState<ProductDetailPage> {
|
||||
_buildUserDropdown(
|
||||
label: 'Nhân viên *',
|
||||
value: _selectedEmployee,
|
||||
users: ref.watch(usersListProvider),
|
||||
users: ref.watch(usersListProvider)
|
||||
.where((user) => user.roleId == 2)
|
||||
.toList(),
|
||||
onChanged: (user) {
|
||||
setState(() {
|
||||
_selectedEmployee = user;
|
||||
|
||||
@@ -8,6 +8,9 @@ import '../models/user_model.dart';
|
||||
abstract class UsersRemoteDataSource {
|
||||
/// Fetch all users from the API
|
||||
Future<List<UserModel>> getUsers();
|
||||
|
||||
/// Get current logged-in user
|
||||
Future<UserModel> getCurrentUser();
|
||||
}
|
||||
|
||||
/// Implementation of UsersRemoteDataSource using ApiClient
|
||||
@@ -54,4 +57,41 @@ class UsersRemoteDataSourceImpl implements UsersRemoteDataSource {
|
||||
throw ServerException('Failed to get users: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<UserModel> getCurrentUser() async {
|
||||
try {
|
||||
// Make API call to get current user
|
||||
final response = await apiClient.get(ApiEndpoints.getCurrentUser);
|
||||
|
||||
// Parse the API response using ApiResponse wrapper
|
||||
final apiResponse = ApiResponse.fromJson(
|
||||
response.data as Map<String, dynamic>,
|
||||
(json) => UserModel.fromJson(json as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
// Check if the API call was successful
|
||||
if (apiResponse.isSuccess && apiResponse.value != null) {
|
||||
return apiResponse.value!;
|
||||
} else {
|
||||
// Throw exception with error message from API
|
||||
throw ServerException(
|
||||
apiResponse.errors.isNotEmpty
|
||||
? apiResponse.errors.first
|
||||
: 'Failed to get current user',
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
// Re-throw ServerException as-is
|
||||
if (e is ServerException) {
|
||||
rethrow;
|
||||
}
|
||||
// Re-throw NetworkException as-is
|
||||
if (e is NetworkException) {
|
||||
rethrow;
|
||||
}
|
||||
// Wrap other exceptions in ServerException
|
||||
throw ServerException('Failed to get current user: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../../../core/di/providers.dart';
|
||||
import '../../../../core/router/app_router.dart';
|
||||
import '../../../../core/storage/secure_storage.dart';
|
||||
import '../widgets/warehouse_card.dart';
|
||||
import '../widgets/warehouse_drawer.dart';
|
||||
|
||||
@@ -28,12 +29,34 @@ class _WarehouseSelectionPageState
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Load warehouses when page is first created
|
||||
Future.microtask(() {
|
||||
Future.microtask(() async {
|
||||
ref.read(warehouseProvider.notifier).loadWarehouses();
|
||||
// Users are automatically loaded from local storage by UsersNotifier
|
||||
|
||||
// Get current user and store user ID
|
||||
await _getCurrentUserAndStoreId();
|
||||
});
|
||||
}
|
||||
|
||||
/// Get current user from API and store user ID in secure storage
|
||||
Future<void> _getCurrentUserAndStoreId() async {
|
||||
try {
|
||||
final secureStorage = SecureStorage();
|
||||
final usersDataSource = ref.read(usersRemoteDataSourceProvider);
|
||||
|
||||
// Call API to get current user
|
||||
final currentUser = await usersDataSource.getCurrentUser();
|
||||
|
||||
// Store the current user ID
|
||||
await secureStorage.saveCurrentUserId(currentUser.id);
|
||||
|
||||
debugPrint('Current user ID stored: ${currentUser.id}');
|
||||
} catch (e) {
|
||||
// Silently fail - this is not critical
|
||||
debugPrint('Error getting current user: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Watch warehouse state
|
||||
|
||||
Reference in New Issue
Block a user