Refactor: Improve test synchronization and logging in FlyerUploader and ProfileManager for better clarity and error handling
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 19m58s
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 19m58s
This commit is contained in:
@@ -109,8 +109,11 @@ export const ProfileManager: React.FC<ProfileManagerProps> = ({ isOpen, onClose,
|
||||
|
||||
const handleProfileSave = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
logger.debug('[handleProfileSave] Save process started.');
|
||||
|
||||
if (!user) {
|
||||
notifyError("Cannot save profile, no user is logged in.");
|
||||
logger.warn('[handleProfileSave] Aborted: No user is logged in.');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -118,8 +121,22 @@ export const ProfileManager: React.FC<ProfileManagerProps> = ({ isOpen, onClose,
|
||||
const profileDataChanged = fullName !== profile?.full_name || avatarUrl !== profile?.avatar_url;
|
||||
const addressDataChanged = JSON.stringify(address) !== JSON.stringify(initialAddress);
|
||||
|
||||
// --- Start Debug Logging ---
|
||||
logger.debug('[handleProfileSave] Checking for data changes.', {
|
||||
profileDataChanged,
|
||||
addressDataChanged,
|
||||
currentFullName: fullName,
|
||||
initialFullName: profile?.full_name,
|
||||
currentAvatarUrl: avatarUrl,
|
||||
initialAvatarUrl: profile?.avatar_url,
|
||||
currentAddress: JSON.stringify(address),
|
||||
initialAddress: JSON.stringify(initialAddress)
|
||||
});
|
||||
// --- End Debug Logging ---
|
||||
|
||||
if (!profileDataChanged && !addressDataChanged) {
|
||||
notifySuccess("No changes to save.");
|
||||
logger.debug('[handleProfileSave] No changes detected. Closing modal.');
|
||||
onClose();
|
||||
return;
|
||||
}
|
||||
@@ -128,17 +145,22 @@ export const ProfileManager: React.FC<ProfileManagerProps> = ({ isOpen, onClose,
|
||||
// Because useApi() catches errors and returns null, we can safely use Promise.all.
|
||||
const promisesToRun = [];
|
||||
if (profileDataChanged) {
|
||||
logger.debug('[handleProfileSave] Queuing profile update promise.');
|
||||
promisesToRun.push(updateProfile({ full_name: fullName, avatar_url: avatarUrl }));
|
||||
}
|
||||
if (addressDataChanged) {
|
||||
logger.debug('[handleProfileSave] Queuing address update promise.');
|
||||
promisesToRun.push(updateAddress(address));
|
||||
}
|
||||
|
||||
try {
|
||||
logger.debug(`[handleProfileSave] Awaiting ${promisesToRun.length} promises...`);
|
||||
const results = await Promise.all(promisesToRun);
|
||||
logger.debug('[handleProfileSave] Promise.all finished.', { results });
|
||||
|
||||
// The useApi hook returns null on failure. Check if any results are null.
|
||||
const allSucceeded = results.every(result => result !== null);
|
||||
logger.debug('[handleProfileSave] All operations succeeded:', allSucceeded);
|
||||
|
||||
if (allSucceeded) {
|
||||
notifySuccess('Profile updated successfully!');
|
||||
@@ -152,11 +174,13 @@ export const ProfileManager: React.FC<ProfileManagerProps> = ({ isOpen, onClose,
|
||||
// A failure occurred. The specific error notification has already been
|
||||
// displayed by the failed useApi hook. We simply log it and keep the
|
||||
// modal open for the user to correct any issues.
|
||||
// A failure occurred. The specific error notification has already been displayed by the failed useApi hook. We simply log it and keep the modal open for the user to correct any issues.
|
||||
logger.warn('handleProfileSave completed with one or more failures.');
|
||||
}
|
||||
} catch (error) {
|
||||
// This catch block is a safeguard. In normal operation, the useApi hook
|
||||
// should prevent any promises from rejecting.
|
||||
// This catch block is a safeguard. In normal operation, the useApi hook should prevent any promises from rejecting.
|
||||
logger.error({ err: error }, 'An unexpected error occurred in handleProfileSave:');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user