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
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
v-model="form"
:fetch-fn="fetchOrganizations"
:create-fn="createOrganization"
placeholder="Select organization"
:placeholder="isCreatingOrganization ? 'Creating organization...' : 'Select organization'"
input-class="organization-input"
:create-if-not-found="true"
:in-memory-filter="false"
:clearable="false"
class="w-full"
:teleported="false"
>
<template v-if="form && (form.displayName || form.name)" #prefix>
<template v-if="isCreatingOrganization" #prefix>
<lf-spinner size="1rem" class="mr-2 text-black" />
</template>
<template v-else-if="form && (form.displayName || form.name)" #prefix>
<div class="flex items-center">
<lf-avatar
:name="form.displayName || form.name"
Expand Down Expand Up @@ -52,7 +55,7 @@
</template>

<script setup lang="ts">
import { computed, onMounted } from 'vue';
import { ref, computed, onMounted } from 'vue';
import { OrganizationService } from '@/modules/organization/organization-service';
import { Organization } from '@/modules/organization/types/Organization';
import LfAvatar from '@/ui-kit/avatar/Avatar.vue';
Expand All @@ -61,6 +64,7 @@ import LfProjectGroupsTags from '@/shared/modules/project-groups/components/proj
import AppAutocompleteOneInput from '@/shared/form/autocomplete-one-input.vue';
import { storeToRefs } from 'pinia';
import { useLfSegmentsStore } from '@/modules/lf/segments/store';
import LfSpinner from '@/ui-kit/spinner/Spinner.vue';

const props = defineProps<{
modelValue: Organization | null,
Expand All @@ -70,6 +74,8 @@ const emit = defineEmits<{(e: 'update:modelValue', value: Organization | null):

const { selectedProjectGroup } = storeToRefs(useLfSegmentsStore());

const isCreatingOrganization = ref<boolean>(false);

const form = computed<Organization | null>({
get() {
return props.modelValue;
Expand All @@ -88,22 +94,29 @@ const fetchOrganizations = async ({ query } : {
segments: [selectedProjectGroup.value?.id],
});

const createOrganization = (value: string) => OrganizationService.create({
name: value,
attributes: {
name: {
default: value,
custom: [value],
const createOrganization = (value: string) => {
isCreatingOrganization.value = true;

return OrganizationService.create({
name: value,
attributes: {
name: {
default: value,
custom: [value],
},
},
},
})
.then((newOrganization) => ({
id: newOrganization.id,
label: newOrganization.displayName || newOrganization.name,
displayName: newOrganization.displayName || newOrganization.name,
name: newOrganization.displayName || newOrganization.name,
}))
.catch(() => null);
})
.then((newOrganization) => ({
id: newOrganization.id,
label: newOrganization.displayName || newOrganization.name,
displayName: newOrganization.displayName || newOrganization.name,
name: newOrganization.displayName || newOrganization.name,
}))
.catch(() => null)
.finally(() => {
isCreatingOrganization.value = false;
});
};
Comment on lines +97 to +119
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error handling and user feedback.

While the loading state management is good, there are a few concerns with the error handling:

  1. Silent error handling might confuse users when organization creation fails
  2. Returning null in the catch block could lead to unexpected behavior
  3. The loading state cleanup is correct but could be more robust

Consider this improved implementation:

 const createOrganization = (value: string) => {
   isCreatingOrganization.value = true;

   return OrganizationService.create({
     name: value,
     attributes: {
       name: {
         default: value,
         custom: [value],
       },
     },
   })
     .then((newOrganization) => ({
       id: newOrganization.id,
       label: newOrganization.displayName || newOrganization.name,
       displayName: newOrganization.displayName || newOrganization.name,
       name: newOrganization.displayName || newOrganization.name,
     }))
-    .catch(() => null)
+    .catch((error) => {
+      // Notify user about the error
+      ElMessage.error('Failed to create organization. Please try again.');
+      // Re-throw to prevent selecting a null value
+      throw error;
+    })
     .finally(() => {
       isCreatingOrganization.value = false;
     });
 };

Committable suggestion skipped: line range outside the PR's diff.


onMounted(() => {
fetchOrganizations({ query: '' });
Expand Down
Loading