Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Previous Problem, Hide and Show Practice Problem Answer #56

Merged
merged 2 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/pages/flash.astro
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ import Button from '../components/Button.astro';
display: none;
}

#previousProblem {
position: fixed;
bottom: 20px;
right: 20px;
display: none;
}

#submitButton {
display: none;
margin: 20px auto;
Expand Down Expand Up @@ -113,6 +120,10 @@ import Button from '../components/Button.astro';
Back
</Button>

<Button id="previousProblem" size="lg" backgroundColor="#4ea7f5" textColor="white" borderColor="transparent">
Previous Problem
</Button>

<div id="resultMessage"></div>
</div>

Expand All @@ -139,6 +150,7 @@ import Button from '../components/Button.astro';
const flashContainer = document.getElementById('flashContainer');
const resultInput = document.getElementById('resultInput');
const backButton = document.getElementById('backButton');

const submitButton = document.getElementById('submitButton');

if (!currentlyFlashing || !flashContainer || !resultInput || !backButton || !submitButton) return;
Expand Down Expand Up @@ -205,6 +217,7 @@ import Button from '../components/Button.astro';
const submitButton = document.getElementById('submitButton') as HTMLButtonElement;
const resultInput = document.getElementById('resultInput') as HTMLInputElement;
const resultMessage = document.getElementById("resultMessage");
const previousProblem = document.getElementById('previousProblem');

if (resultInput && submitButton && resultMessage) {
const userInput = (resultInput as HTMLInputElement).value.trim();
Expand All @@ -228,8 +241,19 @@ import Button from '../components/Button.astro';
resultMessage.className = "incorrect";
}

if (previousProblem) {
previousProblem.addEventListener('click', () => {
localStorage.setItem('previousEntries', JSON.stringify(entries));
localStorage.setItem('previousResult', JSON.stringify(result));

window.location.href = '/previous';
});
}
// Show the result message
resultMessage.style.display = "block";
if (previousProblem) {
previousProblem.style.display = 'block';
}
}
}

Expand Down
75 changes: 75 additions & 0 deletions src/pages/previous.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
import '../styles/styles.css';
import Button from '../components/Button.astro';
---

<head>
<title>Previous Problem</title>
</head>

<body>
<div class="previous-problem-container">
<h1 id="title">Previous</h1>
<div id="problemEntries"></div>
<div id="correctResult"></div>

</div>
<Button
id="backButton"
size="lg"
backgroundColor="#4ea7f5"
textColor="white"
borderColor="transparent"
>
Back
</Button>

<Button
id="toggleResultButton"
size="lg"
backgroundColor="#4ea7f5"
textColor="white"
borderColor="transparent"
>
Show Answer
</Button>

<script>
document.addEventListener('DOMContentLoaded', () => {
const problemEntries = JSON.parse(localStorage.getItem('previousEntries') || "This Shouldn't Be Possible");
const correctResult = localStorage.getItem('previousResult');
const previousURL = localStorage.getItem('previousURL');

const problemEntriesDiv = document.getElementById('problemEntries');
const correctResultDiv = document.getElementById('correctResult');
const backButton = document.getElementById('backButton');
const toggleResultButton = document.getElementById('toggleResultButton');

if (problemEntriesDiv && correctResultDiv) {
problemEntriesDiv.innerHTML = `${problemEntries.map((entry: any) => `(${entry})`).join(' + ')}`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done using a component rather than constructing innerHTML. You can create a new component (perhaps PreviousProblemDisplay) that accepts an array as a property and has the content of

<div>
  {entries.flatMap(e => [' + ', e]).slice(1)}
</div>

correctResultDiv.style.display = 'none';
correctResultDiv.innerHTML = `<strong>= </strong>${correctResult}`;
}

if (toggleResultButton) {
toggleResultButton.addEventListener('click', () => {
if (correctResultDiv) {
if (correctResultDiv.style.display === 'none') {
correctResultDiv.style.display = 'block';
toggleResultButton.textContent = 'Hide Answer';
} else {
correctResultDiv.style.display = 'none';
toggleResultButton.textContent = 'Show Answer';
}
}
});
}

if (backButton && previousURL) {
backButton.addEventListener('click', () => {
window.location.href = previousURL;
});
}
});
</script>
</body>
19 changes: 17 additions & 2 deletions src/styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ font-size: 120px;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
height: 160vh;
gap: 20px;
}
body {
Expand Down Expand Up @@ -57,6 +57,16 @@ body {
white-space: nowrap;
}

.previous-problem-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align:center;
height: 100vh;
font-size: 3.5rem;
}

#backButton {
position: fixed;
bottom: 20px;
Expand All @@ -66,4 +76,9 @@ body {
position: fixed;
bottom: 20px;
right: 20px;
}
}
#toggleResultButton {
position: fixed;
bottom: 20px;
right: 20px;
}
Loading