Skip to content

Commit

Permalink
Fixed Redirect Button
Browse files Browse the repository at this point in the history
- if came from foreign page, take to home page
- if no href was passed down, take to wherever they were before
- if an explicit href was passed down, take client to wherever the href is
  • Loading branch information
JohnLu2004 committed Jul 30, 2024
1 parent 4d35524 commit 745dc75
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
31 changes: 28 additions & 3 deletions src/components/Back/Back.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
---
const { href, label } = Astro.props;
import "./Back.scss";
const usedLabel = label ? label : "Back";
---

<a class="Back" href={href}
><i class="mdi mdi-keyboard-backspace"></i> {label}</a
>
<div class="Back" onclick={`handleBackClick('${href}')`}>
<i class="mdi mdi-keyboard-backspace"></i>
{usedLabel}
</div>

<script>
document.addEventListener("DOMContentLoaded", function () {
function handleBackClick(href: string) {
const previousPageURL = document.referrer;
const currentPageRoot = window.origin;

if (previousPageURL.includes(currentPageRoot)) {
if (href != "undefined") {
window.location.href = href;
} else {
window.history.back();
}
} else {
window.location.href = "/";
}
}

// Make sure handleBackClick is available globally
window.handleBackClick = handleBackClick;
});
</script>
2 changes: 1 addition & 1 deletion src/pages/practice/tag/[tag].astro
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const tabTitle = tag
<Layout title={tabTitle}>
<div class="Question__bar">
<div>
<Back href={`/questions/${randomQuestion.data.path}`} label="Back" />
<Back />
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion src/pages/questions/[...path].astro
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const title = formatString(path);

<Layout title={title + " Question"}>
<div class="Question__bar">
<div><Back href={`/evaluations`} label="Evaluations" /></div>
<div><Back /></div>
</div>
<h1>{title}</h1>
<Content />
Expand Down
2 changes: 1 addition & 1 deletion src/pages/questions/solution/[...path].astro
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const question = await loadQuestion();

<Layout title="Solution">
<div class="Question__bar">
<div><Back href={`/questions/${path}`} label="Back" /></div>
<div><Back /></div>
</div>
<Content />
<div>
Expand Down

0 comments on commit 745dc75

Please sign in to comment.