修正時間到上船數據問題
This commit is contained in:
@@ -10,6 +10,7 @@ import { Progress } from "@/components/ui/progress"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { calculateCombinedScore } from "@/lib/utils/score-calculator"
|
||||
import { useAuth } from "@/lib/hooks/use-auth"
|
||||
import { TimeWarningModal } from "@/components/time-warning-modal"
|
||||
|
||||
interface LogicQuestion {
|
||||
id: number
|
||||
@@ -47,11 +48,22 @@ export default function CombinedTestPage() {
|
||||
const [creativeQuestions, setCreativeQuestions] = useState<CreativeQuestion[]>([])
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
const [hasTimedOut, setHasTimedOut] = useState(false) // 防止重複提交
|
||||
const [timeoutSubmitted, setTimeoutSubmitted] = useState(false) // 確保只提交一次
|
||||
|
||||
// 彈窗狀態
|
||||
const [showWarningModal, setShowWarningModal] = useState(false)
|
||||
const [showTimeoutModal, setShowTimeoutModal] = useState(false)
|
||||
const [showSuccessModal, setShowSuccessModal] = useState(false)
|
||||
const [modalMessage, setModalMessage] = useState('')
|
||||
|
||||
// Load questions from database
|
||||
useEffect(() => {
|
||||
const loadQuestions = async () => {
|
||||
try {
|
||||
// 清除之前的提交標記
|
||||
localStorage.removeItem('combinedTestTimeoutSubmitted')
|
||||
|
||||
// Load logic questions
|
||||
const logicResponse = await fetch('/api/logic-questions')
|
||||
const logicData = await logicResponse.json()
|
||||
@@ -87,12 +99,25 @@ export default function CombinedTestPage() {
|
||||
// 檢查是否剩餘5分鐘(300秒)且尚未顯示警告
|
||||
if (newTime <= 300 && !hasShownWarning) {
|
||||
setHasShownWarning(true)
|
||||
alert('⚠️ 注意:距離測試結束還有5分鐘,請盡快完成剩餘題目!')
|
||||
setModalMessage('距離測試結束還有5分鐘,請盡快完成剩餘題目!')
|
||||
setShowWarningModal(true)
|
||||
}
|
||||
|
||||
// 時間到,強制提交
|
||||
if (newTime <= 0) {
|
||||
if (newTime <= 0 && !hasTimedOut && !timeoutSubmitted) {
|
||||
// 檢查 localStorage 是否已經提交過
|
||||
const alreadySubmitted = localStorage.getItem('combinedTestTimeoutSubmitted')
|
||||
if (alreadySubmitted) {
|
||||
console.log('⏰ 已經提交過時間到結果,跳過重複提交')
|
||||
setHasTimedOut(true)
|
||||
setTimeoutSubmitted(true)
|
||||
return 0
|
||||
}
|
||||
|
||||
console.log('⏰ 時間到!強制提交測驗...')
|
||||
localStorage.setItem('combinedTestTimeoutSubmitted', 'true')
|
||||
setHasTimedOut(true) // 防止重複提交
|
||||
setTimeoutSubmitted(true) // 確保只提交一次
|
||||
handleTimeoutSubmit()
|
||||
return 0
|
||||
}
|
||||
@@ -102,7 +127,7 @@ export default function CombinedTestPage() {
|
||||
}, 1000)
|
||||
|
||||
return () => clearInterval(timer)
|
||||
}, [logicQuestions, creativeQuestions, hasShownWarning])
|
||||
}, [logicQuestions, creativeQuestions, hasShownWarning, hasTimedOut, timeoutSubmitted])
|
||||
|
||||
const formatTime = (seconds: number) => {
|
||||
const mins = Math.floor(seconds / 60)
|
||||
@@ -164,17 +189,33 @@ export default function CombinedTestPage() {
|
||||
}
|
||||
|
||||
const handleTimeoutSubmit = async () => {
|
||||
// 防止重複提交 - 多重檢查
|
||||
if (isSubmitting || hasTimedOut || timeoutSubmitted) {
|
||||
console.log('⏰ 已經在處理時間到提交,跳過重複請求')
|
||||
return
|
||||
}
|
||||
|
||||
// 再次檢查 localStorage
|
||||
const alreadySubmitted = localStorage.getItem('combinedTestTimeoutSubmitted')
|
||||
if (alreadySubmitted) {
|
||||
console.log('⏰ localStorage 顯示已經提交過,跳過重複請求')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('⏰ 時間到!強制提交綜合測試...')
|
||||
console.log('用戶狀態:', user)
|
||||
|
||||
if (!user) {
|
||||
console.log('❌ 用戶未登入')
|
||||
alert('⏰ 時間到!但用戶未登入,無法提交結果。')
|
||||
setModalMessage('時間到!但用戶未登入,無法提交結果。')
|
||||
setShowTimeoutModal(true)
|
||||
return
|
||||
}
|
||||
|
||||
console.log('✅ 用戶已登入,用戶ID:', user.id)
|
||||
setIsSubmitting(true)
|
||||
setHasTimedOut(true)
|
||||
setTimeoutSubmitted(true)
|
||||
|
||||
try {
|
||||
// Calculate logic score
|
||||
@@ -267,12 +308,18 @@ export default function CombinedTestPage() {
|
||||
}
|
||||
|
||||
// 顯示時間到提示
|
||||
alert('⏰ 測試時間已到!系統已自動提交您的答案。')
|
||||
router.push("/results/combined")
|
||||
setModalMessage('測試時間已到!系統已自動提交您的答案。')
|
||||
setShowTimeoutModal(true)
|
||||
|
||||
// 延遲跳轉,讓用戶看到提示
|
||||
setTimeout(() => {
|
||||
router.push("/results/combined")
|
||||
}, 3000)
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 強制提交測驗失敗:', error)
|
||||
alert('⏰ 時間到!但提交失敗,請聯繫管理員。')
|
||||
setModalMessage('時間到!但提交失敗,請聯繫管理員。')
|
||||
setShowTimeoutModal(true)
|
||||
} finally {
|
||||
setIsSubmitting(false)
|
||||
}
|
||||
@@ -608,6 +655,37 @@ export default function CombinedTestPage() {
|
||||
{Object.keys(phase === "logic" ? logicAnswers : creativeAnswers).length} / {currentQuestions.length} 題)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 時間警告彈窗 */}
|
||||
<TimeWarningModal
|
||||
isOpen={showWarningModal}
|
||||
onClose={() => setShowWarningModal(false)}
|
||||
type="warning"
|
||||
title="⚠️ 時間提醒"
|
||||
message={modalMessage}
|
||||
showCountdown={false}
|
||||
/>
|
||||
|
||||
{/* 時間到彈窗 */}
|
||||
<TimeWarningModal
|
||||
isOpen={showTimeoutModal}
|
||||
onClose={() => setShowTimeoutModal(false)}
|
||||
type="timeout"
|
||||
title="⏰ 時間到"
|
||||
message={modalMessage}
|
||||
showCountdown={true}
|
||||
countdownSeconds={3}
|
||||
/>
|
||||
|
||||
{/* 成功提交彈窗 */}
|
||||
<TimeWarningModal
|
||||
isOpen={showSuccessModal}
|
||||
onClose={() => setShowSuccessModal(false)}
|
||||
type="success"
|
||||
title="✅ 提交成功"
|
||||
message={modalMessage}
|
||||
showCountdown={false}
|
||||
/>
|
||||
</TestLayout>
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user