-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: collection form function cannot be used normally and will be stuck in the answer #2952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -224,33 +224,41 @@ const validate = () => { | |
| return userFormRef.value?.validate() || Promise.reject(false) | ||
| } | ||
|
|
||
| function sendMessage(val: string, other_params_data?: any, chat?: chatType) { | ||
| function sendMessage(val: string, other_params_data?: any, chat?: chatType): Promise<boolean> { | ||
| if (isUserInput.value) { | ||
| userFormRef.value | ||
| ?.validate() | ||
| .then((ok) => { | ||
| let userFormData = JSON.parse(localStorage.getItem(`${accessToken}userForm`) || '{}') | ||
| const newData = Object.keys(form_data.value).reduce((result: any, key: string) => { | ||
| result[key] = Object.prototype.hasOwnProperty.call(userFormData, key) | ||
| ? userFormData[key] | ||
| : form_data.value[key] | ||
| return result | ||
| }, {}) | ||
| localStorage.setItem(`${accessToken}userForm`, JSON.stringify(newData)) | ||
| showUserInput.value = false | ||
| if (!loading.value && props.applicationDetails?.name) { | ||
| handleDebounceClick(val, other_params_data, chat) | ||
| } | ||
| }) | ||
| .catch((e) => { | ||
| showUserInput.value = true | ||
| return | ||
| }) | ||
| if (userFormRef.value) { | ||
| return userFormRef.value | ||
| ?.validate() | ||
| .then((ok) => { | ||
| let userFormData = JSON.parse(localStorage.getItem(`${accessToken}userForm`) || '{}') | ||
| const newData = Object.keys(form_data.value).reduce((result: any, key: string) => { | ||
| result[key] = Object.prototype.hasOwnProperty.call(userFormData, key) | ||
| ? userFormData[key] | ||
| : form_data.value[key] | ||
| return result | ||
| }, {}) | ||
| localStorage.setItem(`${accessToken}userForm`, JSON.stringify(newData)) | ||
| showUserInput.value = false | ||
| if (!loading.value && props.applicationDetails?.name) { | ||
| handleDebounceClick(val, other_params_data, chat) | ||
| return true | ||
| } | ||
| throw 'err: no send' | ||
| }) | ||
| .catch((e) => { | ||
| showUserInput.value = true | ||
| return false | ||
| }) | ||
| } else { | ||
| return Promise.reject(false) | ||
| } | ||
| } else { | ||
| showUserInput.value = false | ||
| if (!loading.value && props.applicationDetails?.name) { | ||
| handleDebounceClick(val, other_params_data, chat) | ||
| return Promise.resolve(true) | ||
| } | ||
| return Promise.reject(false) | ||
| } | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a few optimizations and adjustments that can be made to improve the readability and efficiency of the
Here's the revised version of the function: const validate = () => {
return userFormRef.value?.validate() || Promise.reject(false)
}
function updateLocalStorageWithUserForm(userFormData, newData) {
localStorage.setItem(`${accessToken}userForm`, JSON.stringify(newData));
}
function updateUserFormData(userFormData):
const newData = Object.keys(form_data.value).reduce((result, key) => {
result[key] = Object.prototype.hasOwnProperty.call(userFormData, key)
? userFormData[key]
: form_data.value[key];
return result;
}, {});
return newData;
}
async function sendMessage(val: string, other_params_data?: any, chat?: chatType): Promise<boolean> {
if (isUserInput.value) {
try {
await userFormRef.value?.validate();
const { data } = await axios.post('/api/send_message', {
value: val,
user_data: updateLocalStorageWithUserForm(JSON.parse(localStorage.getItem(`${accessToken}userForm`) || '{}'), updateUserFormData(form_data.value)),
// Add any other parameters here
});
handleDebounceClick(val, other_params_data, chat);
return true;
} catch (error) {
console.error('Message sending failed:', error);
showUserInput.value = true;
throw new Error('Failed to send message');
}
} else {
showUserInput.value = false;
if (!loading.value && props.applicationDetails?.name) {
await handleDebounceClick(val, other_params_data, chat);
return true;
}
}
showUserInput.value = false;
await handleDebounceClick(val, other_params_data, chat);
return true;
}Changes Made:
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few potential issues and optimizations in that code:
Promise Return from
sendMessage: The originalsendMesagefunction accepts one parameter,other_params_data, but it's currently used without being passed to the actual call within thechatMessagefunction. This might be unintentional and can lead to undefined behavior.Missing Error Handling: There is no error handling when calling
props.sendMessage. If this method throws an error, the flow of execution could potentially get interrupted without proper recovery.Synchronous Call: By awaiting the promise returned by
props.sendMessage, you ensure that the next steps are executed only after the message has been successfully sent. This improves the responsiveness of your application and prevents race conditions where other actions might occur before the response arrives and handles is written back.Reconsider Parameter Usage: Since
other_params_datais optional according to the prop types, consider whether it’s always needed in all calls. Leaving it out with default values can simplify the implementation.Consistency in Function Calls: Ensure that the rest of the functions (
add_answer_text_list,open, andwrite) operate correctly without any assumptions about previous state or side effects.Based on these considerations, here's an optimized version of your code snippet:
In summary, the main improvements involve moving the asynchronous nature of the
sendMessagecall into its own logical unit using anasync/awaitstructure and ensuring proper error handling, which makes the code more robust and predictable.