Skip to content

Commit

Permalink
refactor duplicate ID Validation code into a utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminCharmes committed Oct 1, 2024
1 parent 1d63eb6 commit d8410d0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 90 deletions.
26 changes: 4 additions & 22 deletions webapp/src/components/BatchCreateItemModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
import Modal from "@/components/Modal.vue";
import ItemSelect from "@/components/ItemSelect.vue";
import { createNewSamples } from "@/server_fetch_utils.js";
import { IDValidationMessage } from "@/field_utils.js";
import { itemTypes, SAMPLE_TABLE_TYPES } from "@/resources.js";
export default {
name: "BatchCreateItemModal",
Expand Down Expand Up @@ -421,9 +422,8 @@ export default {
return this.items.map((item, index, items) => {
if (item.item_id == null) {
return "";
} // Don't throw an error before the user starts typing
// check that item id isn't repeated in this table
}
// Check if item ID is repeated in the table
if (
items
.slice(0, index)
Expand All @@ -433,25 +433,7 @@ export default {
return "ID is repeated from an above row.";
}
if (
this.takenItemIds.includes(item.item_id) ||
this.takenSampleIds.includes(item.item_id)
) {
return `<a href='edit/${item.item_id}'>${item.item_id}</a> already in use.`;
}
if (!/^[a-zA-Z0-9._-]+$/.test(item.item_id)) {
return "ID can only contain alphanumeric characters, dashes ('-') and underscores ('_') and periods ('.')";
}
if (/^[._-]/.test(item.item_id) | /[._-]$/.test(item.item_id)) {
return "ID cannot start or end with puncutation";
}
if (/\s/.test(item.item_id)) {
return "ID cannot have any spaces";
}
if (item.item_id.length < 1 || item.item_id.length > 40) {
return "ID must be between 1 and 40 characters in length";
}
return "";
return IDValidationMessage(item.item_id, this.takenItemIds, this.takenSampleIds);
});
},
},
Expand Down
23 changes: 6 additions & 17 deletions webapp/src/components/CollectionSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@search="debouncedAsyncSearch"
>
<template #no-options="{ searching }">
<span v-if="validationMessage" class="form-error">{{ validationMessage }}</span>
<span v-if="IDValidationMessage" class="form-error">{{ IDValidationMessage }}</span>
<span v-else-if="searching"> Collection already selected </span>
<span v-else class="empty-search"> Search for a collection... </span>
</template>
Expand Down Expand Up @@ -39,6 +39,7 @@
import vSelect from "vue-select";
import FormattedCollectionName from "@/components/FormattedCollectionName.vue";
import { searchCollections, createNewCollection } from "@/server_fetch_utils.js";
import { IDValidationMessage } from "@/field_utils.js";
import { debounceTime } from "@/resources.js";
export default {
Expand Down Expand Up @@ -82,7 +83,7 @@ export default {
this.searchQuery &&
!this.collections.some((item) => item.collection_id === this.searchQuery) &&
!valueSafe.some((item) => item.collection_id === this.searchQuery) &&
!this.IDValidationMessage()
!this.IDValidationMessage
) {
return [
...this.collections,
Expand All @@ -94,8 +95,8 @@ export default {
}
return this.collections;
},
validationMessage() {
return this.IDValidationMessage();
IDValidationMessage() {
return IDValidationMessage(this.searchQuery);
},
},
methods: {
Expand Down Expand Up @@ -126,20 +127,8 @@ 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 (/^[._-]/.test(this.searchQuery) | /[._-]$/.test(this.searchQuery)) {
return "ID cannot start or end with punctuation";
}
if ((this.searchQuery && this.searchQuery.length < 1) || this.searchQuery.length > 40) {
return "ID must be between 1 and 40 characters.";
}
return "";
},
async handleCreateNewCollection() {
if (this.IDValidationMessage()) {
if (this.IDValidationMessage) {
return;
} else {
try {
Expand Down
16 changes: 2 additions & 14 deletions webapp/src/components/CreateCollectionModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import Modal from "@/components/Modal.vue";
import ItemSelect from "@/components/ItemSelect.vue";
import { createNewCollection } from "@/server_fetch_utils.js";
import { IDValidationMessage } from "@/field_utils.js";
export default {
name: "CreateCollectionModal",
Expand All @@ -73,20 +74,7 @@ export default {
: [];
},
IDValidationMessage() {
if (this.collection_id == null) {
return "";
} // Don't throw an error before the user starts typing
if (this.takenCollectionIds.includes(this.collection_id)) {
return `<a href='edit/${this.collection_id}'>${this.collection_id}</a> already in use.`;
}
if (!/^[a-zA-Z0-9_-]+$/.test(this.collection_id)) {
return "ID can only contain alphanumeric characters, dashes ('-') and underscores ('_')";
}
if (this.collection_id.length < 1 || this.collection_id.length > 40) {
return "ID must be between 1 and 40 characters in length";
}
return "";
return IDValidationMessage(this.collection_id, this.takenCollectionIds);
},
},
methods: {
Expand Down
22 changes: 2 additions & 20 deletions webapp/src/components/CreateEquipmentModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import Modal from "@/components/Modal.vue";
import ItemSelect from "@/components/ItemSelect.vue";
import { createNewItem } from "@/server_fetch_utils.js";
import { IDValidationMessage } from "@/field_utils.js";
import { itemTypes } from "@/resources.js";
// import CollectionSelect from "@/components/CollectionSelect.vue";
export default {
Expand Down Expand Up @@ -139,26 +140,7 @@ export default {
: [];
},
equipmentIDValidationMessage() {
if (this.item_id == null) {
return "";
} // Don't throw an error before the user starts typing
if (
this.takenItemIds.includes(this.item_id) ||
this.takenEquipmentIds.includes(this.item_id)
) {
return `<a href='edit/${this.item_id}'>${this.item_id}</a> already in use.`;
}
if (!/^[a-zA-Z0-9._-]+$/.test(this.item_id)) {
return "ID can only contain alphanumeric characters, dashes ('-') and underscores ('_') and periods ('.')";
}
if (/^[._-]/.test(this.item_id) | /[._-]$/.test(this.item_id)) {
return "ID cannot start or end with puncutation";
}
if (this.item_id.length < 1 || this.item_id.length > 40) {
return "ID must be between 1 and 40 characters in length";
}
return "";
return IDValidationMessage(this.item_id, this.takenItemIds, this.takenEquipmentIds);
},
},
methods: {
Expand Down
19 changes: 2 additions & 17 deletions webapp/src/components/CreateItemModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
import Modal from "@/components/Modal.vue";
import ItemSelect from "@/components/ItemSelect.vue";
import { createNewItem } from "@/server_fetch_utils.js";
import { IDValidationMessage } from "@/field_utils.js";
import { itemTypes, SAMPLE_TABLE_TYPES } from "@/resources.js";
import CollectionSelect from "@/components/CollectionSelect.vue";
export default {
Expand Down Expand Up @@ -157,23 +158,7 @@ export default {
: [];
},
itemIDValidationMessage() {
if (this.item_id == null) {
return "";
} // Don't throw an error before the user starts typing
if (this.takenItemIds.includes(this.item_id) || this.takenSampleIds.includes(this.item_id)) {
return `<a href='edit/${this.item_id}'>${this.item_id}</a> already in use.`;
}
if (!/^[a-zA-Z0-9._-]+$/.test(this.item_id)) {
return "ID can only contain alphanumeric characters, dashes ('-') and underscores ('_') and periods ('.')";
}
if (/^[._-]/.test(this.item_id) | /[._-]$/.test(this.item_id)) {
return "ID cannot start or end with puncutation";
}
if (this.item_id.length < 1 || this.item_id.length > 40) {
return "ID must be between 1 and 40 characters in length";
}
return "";
return IDValidationMessage(this.item_id, this.takenItemIds, this.takenSampleIds);
},
},
created() {
Expand Down
32 changes: 32 additions & 0 deletions webapp/src/field_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,35 @@ export function createComputedSetterForCollectionField(collection_field) {
},
};
}

export function IDValidationMessage(
id,
takenItemIds = [],
takenSampleIds = [],
takenCollectionIds = [],
takenEquipmentIds = [],
) {
if (id == null) {
return "";
}

if (
takenItemIds.includes(id) ||
takenSampleIds.includes(id) ||
takenCollectionIds.includes(id) ||
takenEquipmentIds.includes(id)
) {
return `<a href='edit/${id}'>${id}</a> already in use.`;
}

if (id && !/^[a-zA-Z0-9_-]+$/.test(id)) {
return "ID can only contain alphanumeric characters, dashes ('-'), and underscores ('_').";
}
if (/^[._-]/.test(id) | /[._-]$/.test(id)) {
return "ID cannot start or end with punctuation";
}
if ((id && id.length < 1) || id.length > 40) {
return "ID must be between 1 and 40 characters.";
}
return "";
}

0 comments on commit d8410d0

Please sign in to comment.