Skip to content

Commit

Permalink
rough score
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Reynolds committed May 3, 2024
1 parent e209bf5 commit 3be8715
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
57 changes: 34 additions & 23 deletions labapp/app/static/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,57 @@ function getCookie(name) {
return null;
}

async function makeHttpRequest(buttonId, requestUrl, resultDivId) {
const button = document.getElementById(buttonId);
const resultDiv = document.getElementById(resultDivId);
button.disabled = true;
try {
const response = await axios.get(requestUrl);
if (response.data.status === 'success') {
const prettyJson = JSON.stringify(response.data.data, null, 4);
resultDiv.innerHTML = `<div class="alert alert-success"><b>Request Succeeded:</b><br><pre><code class="hljs">${prettyJson}</code></pre></div>`;
} else {
const errJson = JSON.stringify(response.data.error, null, 4);
resultDiv.innerHTML = `<div class="alert alert-danger"><b>Request Failed:</b><br><pre><code class="hljs">${errJson}</code></pre></div>`;
}
} catch (error) {
resultDiv.innerHTML = `<div class="alert alert-danger">Error: ${error.message}</div>`;
} finally {
button.disabled = false;
resultDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
async function testHttpRequest(buttonId, requestUrl, resultDivId) {
const button = document.getElementById(buttonId);
const resultDiv = document.getElementById(resultDivId);
button.disabled = true;
try {
const response = await axios.get(requestUrl);
if (response.data.status === 'success') {
const prettyJson = JSON.stringify(response.data.data, null, 4);
resultDiv.innerHTML = `<div class="alert alert-success"><b>Request Succeeded:</b><br><pre><code class="hljs rounded">${prettyJson}</code></pre></div>`;
updateScoreCookie(requestUrl, 'pass');
} else {
const errJson = JSON.stringify(response.data.error, null, 4);
resultDiv.innerHTML = `<div class="alert alert-danger"><b>Request Failed:</b><br><pre><code class="hljs rounded">${errJson}</code></pre></div>`;
updateScoreCookie(requestUrl, 'fail');
}
} catch (error) {
resultDiv.innerHTML = `<div class="alert alert-danger">Error: ${error.message}</div>`;
updateScoreCookie(requestUrl, 'fail');
} finally {
button.disabled = false;
resultDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
}
}

async function makePostRequest(buttonId, requestUrl, resultDivId, inputDataId) {
async function testPostRequest(buttonId, requestUrl, resultDivId, inputDataId) {
const button = document.getElementById(buttonId);
const resultDiv = document.getElementById(resultDivId);
const inputData = document.getElementById(inputDataId).value;
button.disabled = true;

try {
const response = await axios.post(requestUrl, { userInput: inputData });
if (response.data.status === 'success') {
const prettyJson = JSON.stringify(response.data.data, null, 4);
resultDiv.innerHTML = `<div class="alert alert-success"><b>Request Succeeded:</b><br><pre><code class="hljs">${prettyJson}</code></pre></div>`;
resultDiv.innerHTML = `<div class="alert alert-success"><b>Request Succeeded:</b><br><pre><code class="hljs rounded">${prettyJson}</code></pre></div>`;
updateScoreCookie(requestUrl, 'pass');
} else {
const errJson = JSON.stringify(response.data.error, null, 4);
resultDiv.innerHTML = `<div class="alert alert-danger"><b>Request Failed:</b><br><pre><code class="hljs">${errJson}</code></pre></div>`;
resultDiv.innerHTML = `<div class="alert alert-danger"><b>Request Failed:</b><br><pre><code class="hljs rounded">${errJson}</code></pre></div>`;
updateScoreCookie(requestUrl, 'fail');
}
} catch (error) {
resultDiv.innerHTML = `<div class="alert alert-danger">Error: ${error.message}</div>`;
updateScoreCookie(requestUrl, 'fail');
} finally {
button.disabled = false;
resultDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
}

function updateScoreCookie(requestUrl, status) {
let progress = JSON.parse(getCookie('score') || '{}');
progress[encodeURIComponent(requestUrl)] = status;
document.cookie = `progress=${encodeURIComponent(JSON.stringify(progress))}; path=/; expires=${new Date(new Date().getTime() + 86400e3).toUTCString()};`;
}
24 changes: 24 additions & 0 deletions labapp/app/templates/score.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- score.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scoreboard</title>
</head>
<body>
<h1>Scoreboard</h1>
<table>
<tr>
<th>Request URL</th>
<th>Status</th>
</tr>
{% for url, status in completed_requests.items() %}
<tr>
<td>{{ url }}</td>
<td>{{ status }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>

0 comments on commit 3be8715

Please sign in to comment.