Skip to content

Commit

Permalink
feature use usehooks-ts to handle localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent550102 committed Feb 27, 2024
1 parent 1179b59 commit a46609f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"react-toastify": "^10.0.4",
"tailwind-merge": "^2.2.1",
"tailwind-variants": "^0.1.20",
"usehooks-ts": "^2.15.1",
"vaul": "^0.9.0"
},
"devDependencies": {
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions src/app/(game)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Head from "next/head";
import { Nav } from "@/components/Nav";
import { Footer } from "@/components/Footer";
import { useReadLocalStorage } from "usehooks-ts";

import "../globals.css";
import NonTokenModalContent from "@/components/NonTokenModalContent";
Expand All @@ -12,11 +13,12 @@ export default function RootLayout({
}: Readonly<{
children: React.ReactNode;
}>) {
const [mounted, setMounted] = useState(false);
const [isClient, setIsClient] = useState(false);
const token = useReadLocalStorage("token");
useEffect(() => {
setMounted(true);
return () => setMounted(false);
setIsClient(true);
}, []);

return (
<html lang="zh-TW">
<Head>
Expand All @@ -34,8 +36,8 @@ export default function RootLayout({
<body>
<div className="mx-auto flex h-[100svh] w-screen max-w-[768px] flex-col font-sans">
<Nav />
{mounted && localStorage.getItem("token")
? <p>playerToken: {localStorage.getItem("token")}</p>
{isClient && token
? <div>playerToken: {token}</div>
: <NonTokenModalContent />}
<div className="grow overflow-y-scroll">{children}</div>
<Footer />
Expand Down
3 changes: 2 additions & 1 deletion src/app/(game)/scanner/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import Scanner from "@/components/Scanner";
import { InfoWindow } from "@/components/InfoWindow";
import { getBoothToken } from "@/lib/getBoothToken";
import { sendPuzzle2Player } from "@/lib/sendPuzzle2Player";
import { useReadLocalStorage } from "usehooks-ts";
import { invalidToken, puzzleSuccess, puzzleTaken } from "@/lib/const";

export default function Page() {
const [result, setResult] = useState<string | null>(null);
const [info, setInfo] = useState({ title: "", msg: "" });
const [showInfo, setShowInfo] = useState(false);
const playerToken: string | null = useReadLocalStorage("token");

useEffect(() => {
const handleResult = async () => {
if (typeof result !== "string") return;
var boothToken = await getBoothToken(result);
setResult(null);
if (boothToken) {
const playerToken = localStorage.getItem("token");
if (!playerToken) {
setInfo({ title: "失敗", msg: "請先報到。" });
setShowInfo(true);
Expand Down
5 changes: 3 additions & 2 deletions src/components/NonTokenModalContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ import React, { useEffect, useState } from "react";
import Dialog from "./Dialog";
import Scanner from "./Scanner";
import { getPlayerPuzzle } from "@/lib/getPlayerPuzzle";
import { useLocalStorage } from "usehooks-ts";

const NonTokenModalContent = () => {
const [isModalOpen, setModalOpen] = useState(true);
const [manualInputModal, setManualInputModal] = useState<boolean>(false);
const [scanQRCodeModal, setScanQRCodeModal] = useState<boolean>(false);
const [inputValue, setInputValue] = useState("");
const [result, setResult] = useState<string | null>(null);
const [token, setToken] = useLocalStorage("token", "");

useEffect(() => {
const handleLogin = async () => {
console.log(result);
if (typeof result !== "string") return;
var puzzle = await getPlayerPuzzle(result);
setResult(null);
if (puzzle !== "Invalid token, please try again after checkin.") {
console.log(puzzle);
localStorage.setItem("token", result);
setToken(() => result);
// setModalOpen(false);
} else {
setModalOpen(true);
Expand Down

0 comments on commit a46609f

Please sign in to comment.