|
2984 | 2984 | if (tagSelect.value === '__new__') { |
2985 | 2985 | const newTagName = prompt('Enter new tag name:'); |
2986 | 2986 | if (newTagName && newTagName.trim()) { |
| 2987 | + const trimmedName = newTagName.trim(); |
| 2988 | + |
| 2989 | + // Check if tag already exists in dropdown |
| 2990 | + const existingOpt = Array.from(tagSelect.options).find(o => o.textContent === trimmedName); |
| 2991 | + if (existingOpt) { |
| 2992 | + tagSelect.value = existingOpt.value; |
| 2993 | + return; |
| 2994 | + } |
| 2995 | + |
2987 | 2996 | try { |
2988 | | - const res = await api('tags/', 'POST', { name: newTagName.trim() }); |
2989 | | - // Add the new tag to the dropdown and select it |
| 2997 | + const res = await api('tags/', 'POST', { name: trimmedName }); |
| 2998 | + // Add the new tag to the dropdown with ID as data attribute |
2990 | 2999 | const opt = document.createElement('option'); |
2991 | | - opt.value = res.name; |
2992 | | - opt.textContent = res.name; |
| 3000 | + opt.value = trimmedName; |
| 3001 | + opt.textContent = trimmedName; |
| 3002 | + opt.dataset.tagId = res.id; // Store the ID |
2993 | 3003 | tagSelect.insertBefore(opt, tagSelect.lastChild); |
2994 | | - tagSelect.value = res.name; |
2995 | | - // Refresh allTags |
2996 | | - await loadTags(); |
| 3004 | + tagSelect.value = trimmedName; |
| 3005 | + // Add to allTags so lookup works |
| 3006 | + allTags.push({ id: res.id, name: res.name }); |
2997 | 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 | + } |
| 3021 | + } |
2998 | 3022 | alert('Failed to create tag: ' + e.message); |
2999 | 3023 | tagSelect.value = ''; |
3000 | 3024 | } |
|
3005 | 3029 | }; |
3006 | 3030 | } |
3007 | 3031 |
|
| 3032 | + // Get the tag ID from the selected option (either from data attribute or allTags lookup) |
| 3033 | + function getSelectedTagId() { |
| 3034 | + const tagSelect = document.getElementById('ai-tag'); |
| 3035 | + const tagName = tagSelect.value; |
| 3036 | + if (!tagName || tagName === '__new__') return null; |
| 3037 | + |
| 3038 | + // First check if the option has a stored tagId |
| 3039 | + const selectedOption = tagSelect.options[tagSelect.selectedIndex]; |
| 3040 | + if (selectedOption && selectedOption.dataset.tagId) { |
| 3041 | + return parseInt(selectedOption.dataset.tagId); |
| 3042 | + } |
| 3043 | + |
| 3044 | + // Fall back to looking up in allTags |
| 3045 | + const tag = allTags.find(t => t.name === tagName); |
| 3046 | + return tag ? tag.id : null; |
| 3047 | + } |
| 3048 | + |
3008 | 3049 | // AI Generation - File Drop Handlers |
3009 | 3050 | function handleDragOver(e) { |
3010 | 3051 | e.preventDefault(); |
|
3166 | 3207 | }; |
3167 | 3208 |
|
3168 | 3209 | if (tagName && tagName !== '__new__') { |
3169 | | - const tag = allTags.find(t => t.name === tagName); |
3170 | | - console.log('[AddAll] Looking for tag:', tagName, 'Found:', tag); |
3171 | | - if (tag) payload.tag_ids = [tag.id]; |
| 3210 | + const tagId = getSelectedTagId(); |
| 3211 | + console.log('[AddAll] Tag:', tagName, 'ID:', tagId); |
| 3212 | + if (tagId) payload.tag_ids = [tagId]; |
3172 | 3213 | } |
3173 | 3214 |
|
3174 | 3215 | await api('questions/', 'POST', payload); |
|
3209 | 3250 | }; |
3210 | 3251 |
|
3211 | 3252 | // If tag selected, find its ID |
3212 | | - if (tagName) { |
3213 | | - const tag = allTags.find(t => t.name === tagName); |
3214 | | - if (tag) payload.tag_ids = [tag.id]; |
| 3253 | + if (tagName && tagName !== '__new__') { |
| 3254 | + const tagId = getSelectedTagId(); |
| 3255 | + if (tagId) payload.tag_ids = [tagId]; |
3215 | 3256 | } |
3216 | 3257 |
|
3217 | 3258 | await api('questions/', 'POST', payload); |
|
0 commit comments