Skip to content

Commit

Permalink
display error message in collection options in vSelect
Browse files Browse the repository at this point in the history
Fix all-select checkbox

Default row: 20
  • Loading branch information
BenjaminCharmes committed Sep 26, 2024
1 parent dffd7b5 commit d0f8042
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
59 changes: 44 additions & 15 deletions webapp/src/components/CollectionSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
@search="debouncedAsyncSearch"
>
<template #no-options="{ searching }">
<span v-if="searching"> Collection already selected </span>
<span v-if="validationMessage" class="form-error">{{ validationMessage }}</span>
<span v-else-if="searching"> Collection already selected </span>
<span v-else class="empty-search"> Search for a collection... </span>
</template>
<template #option="{ collection_id, title }">
Expand Down Expand Up @@ -80,7 +81,8 @@ export default {
if (
this.searchQuery &&
!this.collections.some((item) => item.collection_id === this.searchQuery) &&
!valueSafe.some((item) => item.collection_id === this.searchQuery)
!valueSafe.some((item) => item.collection_id === this.searchQuery) &&
!this.IDValidationMessage()
) {
return [
...this.collections,
Expand All @@ -92,6 +94,9 @@ export default {
}
return this.collections;
},
validationMessage() {
return this.IDValidationMessage();
},
},
methods: {
async debouncedAsyncSearch(query, loading) {
Expand Down Expand Up @@ -121,23 +126,47 @@ export default {
loading(false);
}, debounceTime);
},
IDValidationMessage() {
if (this.searchQuery && !/^[a-zA-Z0-9_-]+$/.test(this.searchQuery)) {
return "ID can only contain alphanumeric characters, dashes ('-'), and underscores ('_').";
}
if ((this.searchQuery && this.searchQuery.length < 1) || this.searchQuery.length > 40) {
return "ID must be between 1 and 40 characters.";
}
return "";
},
async handleCreateNewCollection() {
try {
let collection_id = this.searchQuery;
const newCollection = await createNewCollection(this.searchQuery);
if (newCollection) {
this.value = [
...this.value.filter((item) => item.collection_id !== null),
{
collection_id: collection_id,
},
];
if (this.IDValidationMessage()) {
return;
} else {
try {
let collection_id = this.searchQuery;
const newCollection = await createNewCollection(this.searchQuery);
if (newCollection) {
this.value = [
...this.value.filter((item) => item.collection_id !== null),
{
collection_id: collection_id,
},
];
}
} catch (error) {
console.error("Error:", error);
alert("An error occurred while creating the collection. Please try again.");
}
} catch (error) {
console.error("Error:", error);
alert("An error occurred while creating the collection. Please try again.");
}
},
},
};
</script>

<style scoped>
.form-error {
color: red;
}
:deep(.form-error a) {
color: #820000;
font-weight: 600;
}
</style>
22 changes: 12 additions & 10 deletions webapp/src/components/DynamicDataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,6 @@ export default {
return this.itemsSelected.length === this.data.length;
},
},
watch: {
itemsSelected(newSelection) {
console.log("Selected items:", newSelection);
console.log("Selected items:", newSelection.length);
},
},
created() {
this.editable_inventory = EDITABLE_INVENTORY;
},
Expand Down Expand Up @@ -287,11 +281,16 @@ export default {
if (event.checked) {
this.allSelected = event.checked;
const newItems = this.data.slice(this.page * this.rows, (this.page + 1) * this.rows);
this.itemsSelected.push(...newItems);
newItems.forEach((item) => {
if (!this.itemsSelected.includes(item)) {
this.itemsSelected.push(item);
}
});
} else {
const itemsToRemove = this.data.slice(this.page * this.rows, (this.page + 1) * this.rows);
this.itemsSelected = this.itemsSelected.filter((item) => !itemsToRemove.includes(item));
this.allSelected = event.checked;
const itemsToRemove = this.data.slice(this.page * this.rows, (this.page + 1) * this.rows);
const idsToRemove = new Set(itemsToRemove.map((item) => item.item_id));
this.itemsSelected = this.itemsSelected.filter((item) => !idsToRemove.has(item.item_id));
}
},
deleteSelectedItems() {
Expand All @@ -303,7 +302,10 @@ export default {
onPageChange(event) {
this.page = event.page;
this.rows = event.rows;
this.allSelected = false;
const currentItems = this.data.slice(this.page * this.rows, (this.page + 1) * this.rows);
this.allSelected = currentItems.every((currentItem) =>
this.itemsSelected.some((selectedItem) => selectedItem.item_id === currentItem.item_id),
);
},
},
};
Expand Down

0 comments on commit d0f8042

Please sign in to comment.