generated from codersforcauses/django-nextjs-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Issue 46 add custom 404 page #82
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
Open
saltyypringle
wants to merge
7
commits into
main
Choose a base branch
from
issue-46-Add_custom_404_page
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+603
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b620976
404 page stuffs
saltyypringle 0f73c1a
fixed small styling
saltyypringle fa47c87
Merge remote-tracking branch 'origin/main' into issue-46-Add_custom_4…
saltyypringle 19b8412
typo
saltyypringle aca436a
moved the trivia to a json file
saltyypringle ebfd773
prettier
saltyypringle ddbde5a
Update client/src/pages/404.tsx
saltyypringle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| "use client"; | ||
|
|
||
| import Link from "next/link"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| import { Button } from "@/components/ui/button"; | ||
| import TRIVIA from "@/trivia.json"; | ||
|
|
||
| interface Trivia { | ||
| question: string; | ||
| answer: string; | ||
| } | ||
|
|
||
| export default function Custom404() { | ||
| const [gameQuestions, setGameQuestions] = useState<Trivia[]>([]); | ||
| const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0); | ||
| const [score, setScore] = useState(0); | ||
| const [gameActive, setGameActive] = useState(false); | ||
| const [timeLeft, setTimeLeft] = useState(30); | ||
| const [answered, setAnswered] = useState(false); | ||
| const [selectedAnswer, setSelectedAnswer] = useState<string | null>(null); | ||
| const [options, setOptions] = useState<string[]>([]); | ||
|
|
||
| const currentTrivia = gameQuestions[currentQuestionIndex]; | ||
|
|
||
| const generateQuestions = () => { | ||
| const shuffled = TRIVIA.sort(() => Math.random() - 0.5).slice(0, 10); | ||
| setGameQuestions(shuffled); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (!currentTrivia) return; | ||
|
|
||
| const wrongAnswers = TRIVIA.filter((t) => t.answer !== currentTrivia.answer) | ||
| .sort(() => Math.random() - 0.5) | ||
| .slice(0, 3) | ||
| .map((t) => t.answer); | ||
|
|
||
| const allOptions = [currentTrivia.answer, ...wrongAnswers].sort( | ||
| () => Math.random() - 0.5, | ||
| ); | ||
| setOptions(allOptions); | ||
| }, [currentQuestionIndex, currentTrivia]); | ||
|
|
||
| useEffect(() => { | ||
| if (!gameActive || timeLeft <= 0) return; | ||
|
|
||
| const timer = setInterval(() => { | ||
| setTimeLeft((prev) => { | ||
| if (prev <= 1) { | ||
| setGameActive(false); | ||
| return 0; | ||
| } | ||
| return prev - 1; | ||
| }); | ||
| }, 1000); | ||
|
|
||
| return () => clearInterval(timer); | ||
| }, [gameActive, timeLeft]); | ||
|
|
||
| const startGame = () => { | ||
| generateQuestions(); | ||
| setGameActive(true); | ||
| setCurrentQuestionIndex(0); | ||
| setScore(0); | ||
| setTimeLeft(30); | ||
| setAnswered(false); | ||
| setSelectedAnswer(null); | ||
| }; | ||
|
|
||
| const handleAnswer = (answer: string) => { | ||
| if (answered) return; | ||
|
|
||
| setSelectedAnswer(answer); | ||
| setAnswered(true); | ||
|
|
||
| if (answer === currentTrivia.answer) { | ||
| setScore((prev) => prev + 1); | ||
| } | ||
|
|
||
| setTimeout(() => { | ||
| if (currentQuestionIndex < gameQuestions.length - 1) { | ||
| setCurrentQuestionIndex((prev) => prev + 1); | ||
| setAnswered(false); | ||
| setSelectedAnswer(null); | ||
| } else { | ||
| setGameActive(false); | ||
| } | ||
| }, 1000); | ||
| }; | ||
|
|
||
| const getButtonClass = (option: string) => { | ||
| const baseClass = | ||
| "w-full p-3 text-left rounded border transition-all cursor-pointer"; | ||
|
|
||
| if (!answered) { | ||
| return `${baseClass} bg-card border-border text-foreground hover:border-accent`; | ||
| } | ||
|
|
||
| if (option === currentTrivia.answer) { | ||
| return `${baseClass} bg-accent border-accent text-accent-foreground font-semibold`; | ||
| } | ||
|
|
||
| if (option === selectedAnswer && option !== currentTrivia.answer) { | ||
| return `${baseClass} bg-secondary border-secondary text-secondary-foreground`; | ||
| } | ||
|
|
||
| return `${baseClass} bg-muted border-border text-muted-foreground`; | ||
| }; | ||
|
|
||
| return ( | ||
| <main className="mx-auto min-h-dvh max-w-6xl px-6 py-16 md:px-20"> | ||
| <div className="mx-auto w-full max-w-2xl"> | ||
| <h1 className="mb-8 text-center font-jersey10 text-6xl text-primary"> | ||
| 404 | ||
| </h1> | ||
| <h2 className="mb-4 text-center font-jersey10 text-2xl text-foreground"> | ||
| Page Not Found | ||
| </h2> | ||
|
|
||
| <p className="mb-8 text-center text-foreground"> | ||
| Test your game knowledge with some rapid-fire trivia instead!!! | ||
| </p> | ||
|
|
||
| {!gameActive ? ( | ||
| <div className="mb-8 space-y-4"> | ||
| {gameQuestions.length === 0 ? ( | ||
| <> | ||
| <p className="mb-4 text-muted-foreground"> | ||
| Answer 10 random gaming trivia questions in 30 seconds! | ||
| </p> | ||
| <Button onClick={startGame} className="w-full"> | ||
| Start Trivia | ||
| </Button> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <p className="mb-4 text-center font-jersey10 text-2xl font-bold text-primary"> | ||
| Game Over! | ||
| </p> | ||
| <p className="mb-4 text-center text-foreground"> | ||
| Final Score: | ||
| <span className="text-xl font-bold text-primary"> | ||
| {score} | ||
| </span>{" "} | ||
| / 10 | ||
| </p> | ||
| <Button onClick={startGame} className="w-full"> | ||
| Play Again | ||
| </Button> | ||
| </> | ||
| )} | ||
| </div> | ||
| ) : ( | ||
| <div className="mb-8 space-y-6"> | ||
| <div className="mb-4 flex items-center justify-between"> | ||
| <p className="text-sm text-muted-foreground"> | ||
| Question {currentQuestionIndex + 1} / 10 | ||
| </p> | ||
| <div | ||
| className={`font-jersey10 text-3xl font-bold ${ | ||
| timeLeft <= 10 ? "text-destructive" : "text-primary" | ||
| }`} | ||
| > | ||
| {timeLeft}s | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className="space-y-6 rounded border border-border bg-card p-6"> | ||
| <p className="text-lg text-foreground"> | ||
| {currentTrivia.question} | ||
| </p> | ||
|
|
||
| <div className="space-y-3"> | ||
| {options.map((option, idx) => ( | ||
| <button | ||
| key={idx} | ||
| onClick={() => handleAnswer(option)} | ||
| className={getButtonClass(option)} | ||
| disabled={answered} | ||
| > | ||
| {option} | ||
| </button> | ||
| ))} | ||
| </div> | ||
|
|
||
| <p className="text-sm text-foreground"> | ||
| Score:{" "} | ||
| <span className="font-jersey10 font-bold text-primary"> | ||
| {score} / 10 | ||
| </span> | ||
| </p> | ||
| </div> | ||
| </div> | ||
| )} | ||
|
|
||
| <Link href="/"> | ||
| <Button variant="outline" className="w-full"> | ||
| Go Home | ||
| </Button> | ||
| </Link> | ||
| </div> | ||
| </main> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Swap these colours around so it's easier to differentiate between colours