Skip to content

Commit 8bd9407

Browse files
jsb2092claude
andcommitted
Fix tag ID lookup for AI-generated questions
- Store tag ID in option data attribute when created - Add getSelectedTagId() helper to find tag ID reliably - Handle "tag already exists" error by selecting existing tag - Add tag to allTags array when created so lookup works Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 06ef537 commit 8bd9407

1 file changed

Lines changed: 54 additions & 13 deletions

File tree

‎questionbank/static/js/app.js‎

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,17 +2984,41 @@
29842984
if (tagSelect.value === '__new__') {
29852985
const newTagName = prompt('Enter new tag name:');
29862986
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+
29872996
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
29902999
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
29933003
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 });
29973007
} 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+
}
29983022
alert('Failed to create tag: ' + e.message);
29993023
tagSelect.value = '';
30003024
}
@@ -3005,6 +3029,23 @@
30053029
};
30063030
}
30073031

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+
30083049
// AI Generation - File Drop Handlers
30093050
function handleDragOver(e) {
30103051
e.preventDefault();
@@ -3166,9 +3207,9 @@
31663207
};
31673208

31683209
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];
31723213
}
31733214

31743215
await api('questions/', 'POST', payload);
@@ -3209,9 +3250,9 @@
32093250
};
32103251

32113252
// 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];
32153256
}
32163257

32173258
await api('questions/', 'POST', payload);

0 commit comments

Comments
 (0)