Skip to content

Commit

Permalink
Update back button logic (#18)
Browse files Browse the repository at this point in the history
* Fixed Redirect Button

- 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

* Add ts ignore

---------

Co-authored-by: Matthew M-B <matthew.mb@getjobber.com>
  • Loading branch information
JohnLu2004 and MathyouMB authored Jul 30, 2024
1 parent 4d35524 commit 9c3677a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
26 changes: 22 additions & 4 deletions src/components/Back/Back.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
---
const { href, label } = Astro.props;
const { href = "", label = "Back" } = Astro.props;
import "./Back.scss";
---

<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>
{label}
</div>

<script>
document.addEventListener("DOMContentLoaded", function () {
function handleBackClick(href: string) {
const isSameOrigin = document.referrer.includes(window.origin);
if (!isSameOrigin) {
window.location.href = "/";
} else if (href !== "") {
window.location.href = href;
} else {
window.history.back();
}
}
// @ts-ignore
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 9c3677a

Please sign in to comment.