From 12d3bcc5b9b8145d9268a5b4338c98b16d0d09bf Mon Sep 17 00:00:00 2001 From: Vincent550102 <5020559@gmail.com> Date: Mon, 26 Feb 2024 04:46:05 +0800 Subject: [PATCH] update NonTokenModal to get valid playerToken --- src/app/(game)/page.tsx | 12 ++- src/components/NonTokenModalContent.tsx | 133 ++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 src/components/NonTokenModalContent.tsx diff --git a/src/app/(game)/page.tsx b/src/app/(game)/page.tsx index d2e39be..cdca5dc 100644 --- a/src/app/(game)/page.tsx +++ b/src/app/(game)/page.tsx @@ -2,10 +2,11 @@ import { useDetail } from "@/components/Detail"; import { Menu } from "@/components/Menu"; +import NonTokenModalContent from "@/components/NonTokenModalContent"; import { activities, type ActivityType } from "@/data/activity"; import { heading } from "@/varients/heading"; import { text } from "@/varients/text"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { twMerge } from "tailwind-merge"; function Activity({ @@ -55,6 +56,14 @@ export default function Home() { const finished = [[1, 2, 5], [1, 2], [4]][active]; // TODO: useFinished('session') const [filter, setFilter] = useState("all"); const [Detail, setOpen] = useDetail(); + const [token, setToken] = useState(""); + + useEffect(() => { + const storedToken = localStorage.getItem("token"); + if (storedToken) { + setToken(storedToken); + } + }, []); // This maybe need to be modified function isActivityFinished(activity: ActivityType, index: number) { @@ -77,6 +86,7 @@ export default function Home() { active={active} setActive={setActive} /> + {token ?

playerToken: {token}

: }
diff --git a/src/components/NonTokenModalContent.tsx b/src/components/NonTokenModalContent.tsx new file mode 100644 index 0000000..ae93ab5 --- /dev/null +++ b/src/components/NonTokenModalContent.tsx @@ -0,0 +1,133 @@ +"use client"; +import React, { useEffect, useState } from "react"; +import Dialog from "./Dialog"; +import Scanner from "./Scanner"; +import { getPlayerPuzzle } from "@/lib/getPlayerPuzzle"; + +const NonTokenModalContent = () => { + const [isModalOpen, setModalOpen] = useState(true); + const [manualInputModal, setManualInputModal] = useState(false); + const [scanQRCodeModal, setScanQRCodeModal] = useState(false); + const [inputValue, setInputValue] = useState(""); + const [result, setResult] = useState(null); + + 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); + // setModalOpen(false); + } else { + setModalOpen(true); + alert("驗證失敗,請再試一次。"); + } + }; + handleLogin(); + }); + + return ( +
+ setModalOpen(false)}> +
+

+ 請檢查 OPass 是否已成功報到,或透過下列方式進入遊戲。 +

+
+
setManualInputModal(true)} + > +
+ +
+
手動輸入票券代碼
+
+
+
+
setScanQRCodeModal(true)} + > +
+ +
+
掃描票券 QR Code
+
+
+
+
+ + setManualInputModal(false)} + > +
+
手動輸入票券代碼
+
+

請輸入 OPass 票券代碼

+ setInputValue(e.target.value)} + placeholder="請輸入票券代碼" + /> +
+
+ + +
+
+
+ + setScanQRCodeModal(false)} + > +
+
掃描票券 QR Code
+
+

+ 請開啟 OPass 中的我的票券,並掃描其中的 QR Code +

+ { + setResult(result); + + // Dirty hack to close the modal. + setTimeout(() => { + setModalOpen(false); + }, 1000); + }} + /> +
+ +
+
+
+
+
+ ); +}; + +export default NonTokenModalContent;