|
2993 | 2993 | return; |
2994 | 2994 | } |
2995 | 2995 |
|
2996 | | - try { |
2997 | | - const res = await api('tags/', 'POST', { name: trimmedName }); |
2998 | | - // Add the new tag to the dropdown with ID as data attribute |
| 2996 | + // Check if tag already exists in allTags |
| 2997 | + let existingTag = allTags.find(t => t.name === trimmedName); |
| 2998 | + if (existingTag) { |
2999 | 2999 | const opt = document.createElement('option'); |
3000 | 3000 | opt.value = trimmedName; |
3001 | 3001 | opt.textContent = trimmedName; |
3002 | | - opt.dataset.tagId = res.id; // Store the ID |
| 3002 | + opt.dataset.tagId = existingTag.id; |
3003 | 3003 | tagSelect.insertBefore(opt, tagSelect.lastChild); |
3004 | 3004 | tagSelect.value = trimmedName; |
3005 | | - // Add to allTags so lookup works |
3006 | | - allTags.push({ id: res.id, name: res.name }); |
3007 | | - } catch (e) { |
3008 | | - // If tag already exists, try to find and select it |
3009 | | - if (e.message && e.message.includes('already exists')) { |
3010 | | - await loadTags(); // Refresh to get the existing tag |
3011 | | - const existingTag = allTags.find(t => t.name === trimmedName); |
3012 | | - if (existingTag) { |
3013 | | - const opt = document.createElement('option'); |
3014 | | - opt.value = trimmedName; |
3015 | | - opt.textContent = trimmedName; |
3016 | | - opt.dataset.tagId = existingTag.id; |
3017 | | - tagSelect.insertBefore(opt, tagSelect.lastChild); |
3018 | | - tagSelect.value = trimmedName; |
3019 | | - return; |
3020 | | - } |
| 3005 | + return; |
| 3006 | + } |
| 3007 | + |
| 3008 | + const res = await api('tags/', 'POST', { name: trimmedName }); |
| 3009 | + |
| 3010 | + // Check if response indicates error (400 returns error object) |
| 3011 | + if (res.name && res.name[0] && res.name[0].includes('already exists')) { |
| 3012 | + // Tag exists - reload tags and find it |
| 3013 | + await loadTags(); |
| 3014 | + existingTag = allTags.find(t => t.name === trimmedName); |
| 3015 | + if (existingTag) { |
| 3016 | + const opt = document.createElement('option'); |
| 3017 | + opt.value = trimmedName; |
| 3018 | + opt.textContent = trimmedName; |
| 3019 | + opt.dataset.tagId = existingTag.id; |
| 3020 | + tagSelect.insertBefore(opt, tagSelect.lastChild); |
| 3021 | + tagSelect.value = trimmedName; |
| 3022 | + return; |
3021 | 3023 | } |
3022 | | - alert('Failed to create tag: ' + e.message); |
| 3024 | + alert('Tag exists but could not be found. Please refresh the page.'); |
3023 | 3025 | tagSelect.value = ''; |
| 3026 | + return; |
3024 | 3027 | } |
| 3028 | + |
| 3029 | + if (!res.id) { |
| 3030 | + console.error('[Tag] Unexpected response:', res); |
| 3031 | + alert('Failed to create tag'); |
| 3032 | + tagSelect.value = ''; |
| 3033 | + return; |
| 3034 | + } |
| 3035 | + |
| 3036 | + // Success - add the new tag |
| 3037 | + const opt = document.createElement('option'); |
| 3038 | + opt.value = trimmedName; |
| 3039 | + opt.textContent = trimmedName; |
| 3040 | + opt.dataset.tagId = res.id; |
| 3041 | + tagSelect.insertBefore(opt, tagSelect.lastChild); |
| 3042 | + tagSelect.value = trimmedName; |
| 3043 | + allTags.push({ id: res.id, name: res.name }); |
3025 | 3044 | } else { |
3026 | 3045 | tagSelect.value = ''; |
3027 | 3046 | } |
|
0 commit comments