hrms-exam/cms/src/routes/competitive/+page.server.ts

35 lines
1.4 KiB
TypeScript
Raw Normal View History

import {competitiveBase} from '$lib/data/CMSDataType'
2023-04-08 11:27:09 +07:00
import { error } from '@sveltejs/kit';
2023-04-07 22:59:53 +07:00
import type {CalendarEvent,Exam} from '$lib/data/CMSDataType'
2023-04-10 14:41:43 +07:00
import {getBgColorDict} from '$lib/utils'
2023-04-07 22:59:53 +07:00
import dayjs from 'dayjs'
2023-04-08 11:27:09 +07:00
import type { PageServerLoad } from './$types'
export const load: PageServerLoad = async ({fetch}) => {
const res = await fetch(competitiveBase+"/competitive")
if(!res.ok)
2023-04-08 11:27:09 +07:00
throw error(res.status, 'ไม่สามารถอ่านข้อมูลการสอบได้');
2023-04-10 14:41:43 +07:00
const exams:Exam[] = await res.json()
2023-04-05 17:59:43 +07:00
const events:CalendarEvent[]=[]
2023-04-10 14:41:43 +07:00
const catColor = {};
exams.forEach((ex)=>{
2023-04-10 14:41:43 +07:00
const {id,title,start,end,category} = ex
if(!category)
return
const backgroundColor = getBgColorDict(category,catColor)
2023-04-05 17:59:43 +07:00
const url = "/competitive/"+id
if(start){
ex.date = dayjs(start).format("DD MMM BBBB")
events.push({id,title,start,end,url,backgroundColor})
}
if(ex.exam_date){
events.push({id,"title":"วันสอบแข่งขัน","start":ex.exam_date,url,backgroundColor})
}
if(ex.announcement_date){
events.push({id,"title":"ผลสอบแข่งขัน","start":ex.announcement_date,url,backgroundColor})
}
2023-04-05 17:59:43 +07:00
})
return {exams,events}
}