feat: add Tjing course import
Search and import courses with layouts from Tjing's GraphQL API. Total par is calculated from individual hole data. Courses are saved with a tjing.se link as unique identifier to prevent duplicates.
This commit is contained in:
@@ -131,3 +131,37 @@
|
||||
opacity: 0.6;
|
||||
border-style: dashed;
|
||||
}
|
||||
|
||||
/* ── Tjing Import ────────────────────────────── */
|
||||
|
||||
.tjing-result {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 12px 14px;
|
||||
margin: 6px 0;
|
||||
background: var(--surface-2);
|
||||
border-radius: var(--radius-sm);
|
||||
border: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.tjing-result-info {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.tjing-result-name {
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.tjing-result-address {
|
||||
font-size: 13px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
#tjing-results {
|
||||
margin-top: 12px;
|
||||
}
|
||||
|
||||
@@ -54,6 +54,88 @@ async function scrapeCourses() {
|
||||
}
|
||||
}
|
||||
|
||||
async function searchTjing() {
|
||||
var input = document.getElementById('tjing-search');
|
||||
var query = input.value.trim();
|
||||
if (query.length < 2) return;
|
||||
|
||||
var btn = document.getElementById('tjing-search-btn');
|
||||
btn.disabled = true;
|
||||
|
||||
try {
|
||||
var response = await fetch('/api/tjing/search?q=' + encodeURIComponent(query));
|
||||
var courses = await response.json();
|
||||
var container = document.getElementById('tjing-results');
|
||||
container.textContent = '';
|
||||
|
||||
if (courses.length === 0) {
|
||||
var p = document.createElement('p');
|
||||
p.className = 'no-layouts';
|
||||
p.textContent = 'No courses found on Tjing';
|
||||
container.appendChild(p);
|
||||
return;
|
||||
}
|
||||
|
||||
courses.forEach(function(course) {
|
||||
var item = document.createElement('div');
|
||||
item.className = 'tjing-result';
|
||||
|
||||
var info = document.createElement('div');
|
||||
info.className = 'tjing-result-info';
|
||||
|
||||
var name = document.createElement('span');
|
||||
name.className = 'tjing-result-name';
|
||||
name.textContent = course.name;
|
||||
|
||||
var addr = document.createElement('span');
|
||||
addr.className = 'tjing-result-address';
|
||||
addr.textContent = course.address || '';
|
||||
|
||||
info.appendChild(name);
|
||||
info.appendChild(addr);
|
||||
|
||||
var importBtn = document.createElement('button');
|
||||
importBtn.className = 'btn';
|
||||
importBtn.textContent = 'Import';
|
||||
importBtn.addEventListener('click', function() { importFromTjing(course.id, importBtn); });
|
||||
|
||||
item.appendChild(info);
|
||||
item.appendChild(importBtn);
|
||||
container.appendChild(item);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error searching Tjing:', error);
|
||||
alert('Failed to search Tjing');
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function importFromTjing(tjingId, btn) {
|
||||
btn.disabled = true;
|
||||
btn.textContent = 'Importing...';
|
||||
|
||||
try {
|
||||
var response = await fetch('/api/tjing/import/' + tjingId, { method: 'POST' });
|
||||
var data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
btn.textContent = 'Imported!';
|
||||
btn.style.background = 'var(--green)';
|
||||
htmx.ajax('GET', '/partials/course-table', '#courses-table');
|
||||
} else {
|
||||
alert(data.error || 'Failed to import');
|
||||
btn.disabled = false;
|
||||
btn.textContent = 'Import';
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error importing from Tjing:', error);
|
||||
alert('Failed to import course');
|
||||
btn.disabled = false;
|
||||
btn.textContent = 'Import';
|
||||
}
|
||||
}
|
||||
|
||||
async function scrapeLayouts(courseId, courseName) {
|
||||
const icon = document.querySelector(`#row-${courseId} .refresh-icon`);
|
||||
icon.classList.add('spinning');
|
||||
|
||||
Reference in New Issue
Block a user