Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ui/src/components/folder-tree/CreateFolderDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import folderApi from '@/api/folder'
import { MsgSuccess, MsgAlert } from '@/utils/message'
import { t } from '@/locales'
import useStore from '@/stores'
const { tool, knowledge, folder } = useStore()
const { user, tool, knowledge, folder } = useStore()
const emit = defineEmits(['refresh'])

const props = defineProps({
Expand Down Expand Up @@ -129,7 +129,13 @@ const submitHandle = async () => {
dialogVisible.value = false
})
} else {
folderApi.postFolder(sourceType.value, folderForm.value, loading).then((res) => {
folderApi.postFolder(sourceType.value, folderForm.value, loading)
.then((res) => {
return user.profile().then(() => {
return res
})
})
.then((res) => {
MsgSuccess(t('common.createSuccess'))
folder.setCurrentFolder(res.data)
folder.asyncGetFolder(sourceType.value, {}, loading)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code has several issues:

  1. Re-definition of useStore: You've defined it twice in the same component, which can lead to confusion and errors if you're trying to use it multiple times.

  2. Optimization Suggestions:

    • The .profile() method is called within an asynchronous chain without waiting for its completion before proceeding with further operations. It would be beneficial to add a .catch() block or await it properly after calling .profile().
    • Consider using TypeScript more extensively to ensure type safety throughout the function, especially when dealing with Promises.
  3. General Improvements:

    • Ensure that dialogVisible, sourceType, and folderForm are correctly defined and used within their respective scopes.
    • Check for any unnecessary empty lines and adjust indentation where necessary for better readability.

Here's a revised version of the code incorporating some of these suggestions along with improvements in structure and comments:

import { ref, defineEmits, watch } from 'vue';
import folderApi from '@/api/folder';
import { MsgSuccess, MsgAlert } from '@/utils/message';
import { t } from '@/locales';
import useStore from '@/stores';

const store = useStore();
const dialogVisible = ref(false);
const sourceType = ref<string>('');
const folderForm = ref<any>({});

const emit = defineEmits(['refresh']);

watch(folderForm, (newForm) => console.log(newForm)); // Example: logging form changes

const submitHandle = async () => {
  try {
    let result;
    
    if (dialogVisible.value && !formValidated()) {
      handleValidateError(); // Function to show validation error message
      return;
    }

    if (!folderId || folderId === '-1') { // Assuming this variable holds the current folder ID
      result = await folderApi.postFolder(sourceType.value, folderForm.value, loading);
    }
    else {
      result = await folder.updateFolder(folderId, folderForm.value); // Assuming there's a method to update folders
    }

    await store.user.profile(); // Fetch user profile before handling success
    MsgSuccess(t('common.createSuccess'));
    folder.setCurrentFolder(result.data);
    folder.asyncGetFolder(sourceType.value, {}, loading);

    dialogVisible.value = false;

    // Handle successful response here
    if(result.success) {
      console log("Folder created successfully:", result);
    }

  } catch (error) {
    const errorMessage = error.response ? error.response.data.message : 'Something went wrong.';
    MsgFailed(t(`errors.${errorMessage}`));
    console.error(error);
  }
};

// Helper methods
function formValidated(): boolean {
  // Implement your validation logic here
  return true; // Return false if any field does not validate correctly
}

function handleValidateError(): void {
  Message.error(t('common.formValidationErrors'));
}

Key Changes:

  • Removed repeated definition and usage of useStore.
  • Ensured proper chaining of promises in the submitHandle function.
  • Added basic input checking and error handling.
  • Included comments for clarity and improved maintainability.
  • Assumed store.user.profile().then(() => ...) fetches the user profile asynchronously.

These adjustments should help enhance the robustness and functionality of your Vue.js application component.

Expand Down
Loading