Refactor API client logging and enhance user data handling in AccountSettingsDialog
This commit is contained in:
@@ -146,19 +146,10 @@ class ApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, dynamic>> putRaw(String path, {dynamic data}) async {
|
Future<Map<String, dynamic>> putRaw(String path, {dynamic data}) async {
|
||||||
print('🌐 PUT request to: $path');
|
|
||||||
print('📦 Data: $data');
|
|
||||||
try {
|
try {
|
||||||
final response = await _dio.put(path, data: data);
|
final response = await _dio.put(path, data: data);
|
||||||
print('📨 Response status: ${response.statusCode}');
|
|
||||||
print('📨 Response data: ${response.data}');
|
|
||||||
return response.data;
|
return response.data;
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
print('❌ PUT request failed: ${e.message}');
|
|
||||||
if (e.response != null) {
|
|
||||||
print('❌ Response status: ${e.response?.statusCode}');
|
|
||||||
print('❌ Response data: ${e.response?.data}');
|
|
||||||
}
|
|
||||||
throw _handleError(e);
|
throw _handleError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,18 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
|
|||||||
_currentPasswordController = TextEditingController();
|
_currentPasswordController = TextEditingController();
|
||||||
_newPasswordController = TextEditingController();
|
_newPasswordController = TextEditingController();
|
||||||
_confirmPasswordController = TextEditingController();
|
_confirmPasswordController = TextEditingController();
|
||||||
|
|
||||||
|
// Get initial user data
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (mounted) {
|
||||||
|
final authState = context.read<AuthBloc>().state;
|
||||||
|
if (authState is AuthAuthenticated) {
|
||||||
|
_currentUser = authState.user;
|
||||||
|
_displayNameController.text = _currentUser?.displayName ?? '';
|
||||||
|
_avatarUrl = _currentUser?.avatarUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -102,25 +114,19 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _updateProfile() async {
|
Future<void> _updateProfile() async {
|
||||||
print(
|
|
||||||
'🔄 _updateProfile called with displayName: "${_displayNameController.text}"',
|
|
||||||
);
|
|
||||||
if (_currentUser == null) {
|
if (_currentUser == null) {
|
||||||
print('❌ _currentUser is null');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setState(() => _isLoading = true);
|
setState(() => _isLoading = true);
|
||||||
try {
|
try {
|
||||||
final apiClient = GetIt.I<ApiClient>();
|
final apiClient = GetIt.I<ApiClient>();
|
||||||
print('📡 Making API call to updateUserProfile');
|
|
||||||
await apiClient.updateUserProfile(
|
await apiClient.updateUserProfile(
|
||||||
displayName: _displayNameController.text.isEmpty
|
displayName: _displayNameController.text.isEmpty
|
||||||
? null
|
? null
|
||||||
: _displayNameController.text,
|
: _displayNameController.text,
|
||||||
avatarUrl: _avatarUrl,
|
avatarUrl: _avatarUrl,
|
||||||
);
|
);
|
||||||
print('✅ API call successful');
|
|
||||||
|
|
||||||
final updatedUser = _currentUser!.copyWith(
|
final updatedUser = _currentUser!.copyWith(
|
||||||
displayName: _displayNameController.text.isEmpty
|
displayName: _displayNameController.text.isEmpty
|
||||||
@@ -131,20 +137,16 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
|
|||||||
|
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
// Update auth state
|
// Update auth state
|
||||||
print('🔄 Updating auth state');
|
|
||||||
context.read<AuthBloc>().add(UpdateUserProfile(updatedUser));
|
context.read<AuthBloc>().add(UpdateUserProfile(updatedUser));
|
||||||
|
|
||||||
// Show success message
|
// Show success message
|
||||||
print('📢 Showing success snackbar');
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
const SnackBar(content: Text('Profile updated successfully')),
|
const SnackBar(content: Text('Profile updated successfully')),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('❌ API call failed: $e');
|
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
// Show error message
|
// Show error message
|
||||||
print('📢 Showing error snackbar');
|
|
||||||
ScaffoldMessenger.of(
|
ScaffoldMessenger.of(
|
||||||
context,
|
context,
|
||||||
).showSnackBar(SnackBar(content: Text('Failed to update profile: $e')));
|
).showSnackBar(SnackBar(content: Text('Failed to update profile: $e')));
|
||||||
@@ -347,10 +349,14 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocBuilder<AuthBloc, AuthState>(
|
return BlocBuilder<AuthBloc, AuthState>(
|
||||||
builder: (context, authState) {
|
builder: (context, authState) {
|
||||||
// Update user data when auth state changes
|
// Always ensure we have the current user
|
||||||
if (authState is AuthAuthenticated && _currentUser != authState.user) {
|
if (authState is AuthAuthenticated) {
|
||||||
_currentUser = authState.user;
|
_currentUser = authState.user;
|
||||||
|
// Only update controller if it's empty (first time) or if user changed
|
||||||
|
if (_displayNameController.text.isEmpty ||
|
||||||
|
_currentUser?.displayName != _displayNameController.text) {
|
||||||
_displayNameController.text = _currentUser?.displayName ?? '';
|
_displayNameController.text = _currentUser?.displayName ?? '';
|
||||||
|
}
|
||||||
_avatarUrl = _currentUser?.avatarUrl;
|
_avatarUrl = _currentUser?.avatarUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -582,12 +588,7 @@ class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
|
|||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
width: 144,
|
width: 144,
|
||||||
child: ModernGlassButton(
|
child: ModernGlassButton(
|
||||||
onPressed: _isLoading
|
onPressed: _isLoading ? () {} : _updateProfile,
|
||||||
? () {}
|
|
||||||
: () {
|
|
||||||
print('🔘 Save Changes button pressed');
|
|
||||||
_updateProfile();
|
|
||||||
},
|
|
||||||
isLoading: _isLoading,
|
isLoading: _isLoading,
|
||||||
child: _isLoading
|
child: _isLoading
|
||||||
? const SizedBox(
|
? const SizedBox(
|
||||||
|
|||||||
Reference in New Issue
Block a user