實現得獎資訊與資料庫整合

This commit is contained in:
2025-09-26 01:42:52 +08:00
parent 9bed168238
commit 0675fe63b0
8 changed files with 1531 additions and 409 deletions

View File

@@ -62,6 +62,8 @@ interface CompetitionContextType {
// Awards
awards: Award[]
loadingAwards: boolean
loadAwards: () => Promise<void>
addAward: (award: Omit<Award, "id">) => void
getAwardsByYear: (year: number) => Award[]
getAwardsByMonth: (year: number, month: number) => Award[]
@@ -221,20 +223,9 @@ export function CompetitionProvider({ children }: { children: ReactNode }) {
return mockProposalScores
})
// Load awards from localStorage
const [awards, setAwards] = useState<Award[]>(() => {
if (typeof window !== "undefined") {
const saved = localStorage.getItem("competitionAwards")
if (saved) {
return JSON.parse(saved)
}
}
// 獎項資料
const mockAwards = []
return mockAwards
})
// Load awards from database
const [awards, setAwards] = useState<Award[]>([])
const [loadingAwards, setLoadingAwards] = useState(false)
// Save to localStorage when data changes
useEffect(() => {
@@ -249,11 +240,10 @@ export function CompetitionProvider({ children }: { children: ReactNode }) {
}
}, [proposalJudgeScores])
// 載入獎項數據
useEffect(() => {
if (typeof window !== "undefined") {
localStorage.setItem("competitionAwards", JSON.stringify(awards))
}
}, [awards])
loadAwards()
}, [])
const addJudge = (judge: Omit<Judge, "id">) => {
@@ -533,12 +523,49 @@ export function CompetitionProvider({ children }: { children: ReactNode }) {
}
}
// 載入獎項數據
const loadAwards = async () => {
setLoadingAwards(true)
try {
const response = await fetch('/api/admin/awards')
const data = await response.json()
if (data.success) {
console.log('📊 載入獎項數據:', data.data.length, '個')
setAwards(data.data)
} else {
console.error('載入獎項失敗:', data.message)
setAwards([])
}
} catch (error) {
console.error('載入獎項失敗:', error)
setAwards([])
} finally {
setLoadingAwards(false)
}
}
const addAward = (award: Omit<Award, "id">) => {
const newAward: Award = {
...award,
id: `a${Date.now()}`,
}
setAwards((prev) => [...prev, newAward])
setAwards((prev) => {
// 檢查是否已存在相同的獎項基於競賽ID、參賽者ID和獎項名稱
const exists = prev.some((existingAward: any) =>
existingAward.competitionId === newAward.competitionId &&
existingAward.participantId === newAward.participantId &&
existingAward.awardName === newAward.awardName
)
if (exists) {
console.log('⚠️ 獎項已存在,跳過重複添加:', newAward)
return prev
}
console.log('✅ 添加新獎項:', newAward)
return [...prev, newAward]
})
}
const getAwardsByYear = (year: number): Award[] => {
@@ -804,6 +831,8 @@ export function CompetitionProvider({ children }: { children: ReactNode }) {
getAppDetailedScores,
getProposalDetailedScores,
awards,
loadingAwards,
loadAwards,
addAward,
getAwardsByYear,
getAwardsByMonth,