fix: Setting avatar save prompt error(#2523)#2535
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| MsgSuccess(t('common.saveSuccess')) | ||
| dialogVisible.value = false | ||
| }) | ||
| } else { |
There was a problem hiding this comment.
The code you provided has several potential issues that could be addressed:
-
Lack of Consistent Error Handling: There is no error handling in case something goes wrong during API requests (
asyncPutApplicationoroverviewApi.putAppIcon). This can result in unexpected behavior if the server responds with errors. -
Use of Raw File Data: The line
fd.append('file', iconFile.value.raw)assumes thaticonFile.value.rawalready contains the raw data from the file input element, which might not always be reliable depending on the browser's implementation. It would be safer to work directly with the file object without assuming its raw property exists. -
Redundant Code Blocks: Both conditions for setting an avatar use similar logic, including appending a form data entry and updating
dialogVisible. These blocks could be extracted into a separate function to reduce redundancy. -
Variable Naming: Variable names like
defaultIcon,radioType, etc., should be more descriptive if possible, especially since they refer to user-facing elements.
Here’s a modified version of the code addressing some of these points:
function saveAvatar() {
const id = id as string; // Assuming id is defined somewhere in scope
const uploadImage = async () => {
try {
await appApi.uploadAppIcon(id, iconFile.value);
emit('refresh');
MsgSuccess(t('common.saveSuccess'));
dialogVisible.value = false;
} catch (error) {
console.error("Failed to set Avatar:", error);
}
};
if (radioType.value === 'default') {
uploadImage();
} else if (radioType.value === 'custom' && iconFile.value) {
uploadImage();
}
}
// In your main application component, call this function when you want to save the avatarKey Changes:
- Function Extraction: The upload logic is now inside the
saveAvatarfunction to prevent duplication. - Error Handling: Added a generic error handler using a promise catch block to ensure any failed uploads are logged.
- Consistent Error Message: Replaced the success message specific to the Avatar Dialog with a common "Save Success" message.
This refactored version improves maintainability and robustness by encapsulating the core functionality in one place and providing better feedback on potential failures.
What this PR does / why we need it?
Summary of your change
Please indicate you've done the following: