This commit is contained in:
Andras Schmelczer 2025-08-31 13:32:58 +01:00
commit 567ffea181
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
7 changed files with 157 additions and 86 deletions

View file

@ -1,5 +1,20 @@
let questions = null;
const API_BASE = window.location.hostname === 'localhost' ? 'http://localhost:3001' : 'https://your-backend-domain.com';
// Auto-detect API base URL
const getApiBase = () => {
const protocol = window.location.protocol;
const hostname = window.location.hostname;
// If running on localhost, assume backend is on port 3001
if (hostname === 'localhost' || hostname === '127.0.0.1') {
return `${protocol}//${hostname}:3001`;
}
// For production, assume backend is on same origin
return "https://fizika-backend.schmelczer.dev"
};
const API_BASE = getApiBase();
const loadQuestions = async (
isSearch,
@ -9,10 +24,25 @@ const loadQuestions = async (
) => {
if (questions === null) {
try {
questions = await (await fetch(`${API_BASE}/api/fizika`)).json();
const response = await fetch(`${API_BASE}/api/fizika`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
questions = await response.json();
console.log('Questions loaded from backend API');
} catch (error) {
console.error('Failed to load questions from API, falling back to local file:', error);
questions = await (await fetch("fizika.json")).json();
console.warn('Failed to load questions from API, falling back to local file:', error);
try {
const fallbackResponse = await fetch("fizika.json");
if (!fallbackResponse.ok) {
throw new Error(`Local file not available: ${fallbackResponse.status}`);
}
questions = await fallbackResponse.json();
console.log('Questions loaded from local fallback file');
} catch (fallbackError) {
console.error('Both API and local file failed:', fallbackError);
throw new Error('Unable to load quiz data from either backend API or local file');
}
}
}
@ -38,7 +68,7 @@ const loadQuestions = async (
<div class="feladat card" id="feladat${id}">
<h2 style="float: left;">${i + 1}.</h2><h2>${source}</h2>
<pre>${description}</pre>
${image ? `<img src="${API_BASE}/api/pics/${image}"><br>` : ""}
${image ? `<img src="${API_BASE}/api/pics/${image}" onerror="this.src='pics/${image}'"><br>` : ""}
<form id="form${id}"">
<input type="radio" id="rad1" name="group">
<label id="label${id}" class="rad1">${a}</label>
@ -49,14 +79,13 @@ const loadQuestions = async (
<input type="radio" id="rad3" name="group">
<label id="label${id}" class="rad3">${c}</label>
<br>
${
d
? `
${d
? `
<input type="radio" id="rad4" name="group">
<label id="label${id}" class="rad4">${d}</label>
<br>`
: ""
}
: ""
}
</form>
<script type="text/javascript">
$(document).ready(function(){