fix: enhance tag duplication check and handle varied response structures in DetailTag component
This commit is contained in:
@@ -84,12 +84,32 @@ const DetailTag = (props) => {
|
||||
const params = new URLSearchParams({ limit: 10000 });
|
||||
const response = await getAllTag(params);
|
||||
|
||||
if (response && response.data && response.data.data) {
|
||||
const existingTags = response.data.data;
|
||||
// Handle different response structures
|
||||
let existingTags = [];
|
||||
if (response) {
|
||||
if (Array.isArray(response)) {
|
||||
existingTags = response;
|
||||
} else if (response.data && Array.isArray(response.data)) {
|
||||
existingTags = response.data;
|
||||
} else if (response.data && response.data.data && Array.isArray(response.data.data)) {
|
||||
existingTags = response.data.data;
|
||||
}
|
||||
}
|
||||
|
||||
if (existingTags.length > 0) {
|
||||
const isDuplicate = existingTags.some((tag) => {
|
||||
const isSameNumber = Number(tag.tag_number) === tagNumberInt;
|
||||
const isDifferentTag = formData.tag_id ? tag.tag_id !== formData.tag_id : true;
|
||||
// Handle both string and number tag_number
|
||||
const existingTagNumber = Number(tag.tag_number);
|
||||
const currentTagNumber = Number(formData.tag_number);
|
||||
|
||||
// Check if numbers are valid and equal
|
||||
const isSameNumber = !isNaN(existingTagNumber) && !isNaN(currentTagNumber) &&
|
||||
existingTagNumber === currentTagNumber;
|
||||
|
||||
// For edit mode, exclude the current tag from duplicate check
|
||||
const isDifferentTag = formData.tag_id ?
|
||||
String(tag.tag_id) !== String(formData.tag_id) : true;
|
||||
|
||||
return isSameNumber && isDifferentTag;
|
||||
});
|
||||
|
||||
@@ -97,7 +117,7 @@ const DetailTag = (props) => {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: `Tag Number ${tagNumberInt} sudah digunakan. Silakan gunakan nomor yang berbeda.`,
|
||||
message: `Tag Number ${formData.tag_number} sudah digunakan. Silakan gunakan nomor yang berbeda.`,
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user