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

54 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-04-07 22:59:53 +07:00
2023-04-08 11:27:09 +07:00
import { error } from '@sveltejs/kit';
import type {CalendarEvent,Exam} from '$lib/data/CMSDataType'
2023-04-07 22:59:53 +07:00
import dayjs from 'dayjs'
2023-03-27 16:43:56 +07:00
import type { PageServerLoad } from './$types'
export const load: PageServerLoad = async ({fetch}) => {
let content = ""
let exams:Exam[] = []
let res = await fetch("/api/content?page=qualifying")
if(res.ok){
content = (await res.json()).content
}
res = await fetch("/api/qualifying")
2023-04-08 11:27:09 +07:00
if(!res.ok){
throw error(res.status, 'ไม่สามารถอ่านข้อมูลการสอบได้');
}
2023-04-08 11:27:09 +07:00
exams = await res.json()
2023-04-04 18:32:58 +07:00
const events:CalendarEvent[]=[]
2023-04-07 22:59:53 +07:00
exams.forEach((e)=>{
if(!e.start){
e.date=""
return
}
2023-04-08 11:27:09 +07:00
e.date = e.start? dayjs(e.start).format("DD MMM BBBB"):""
2023-04-07 22:59:53 +07:00
const {id,title,start,end,category_id} = e
let backgroundColor =""
2023-04-07 22:59:53 +07:00
switch (category_id) {
case "1":
backgroundColor="rgba(251,64,75,.2)"
2023-04-04 18:32:58 +07:00
break;
2023-04-07 22:59:53 +07:00
case "2":
backgroundColor="#23ccef"
break;
2023-04-07 22:59:53 +07:00
case "3":
backgroundColor="rgba(68,125,247,.2)"
2023-04-04 18:32:58 +07:00
break;
default:
break;
}
const url = "/qualifying/"+id
2023-04-07 22:59:53 +07:00
events.push({id,title,start,end,url,backgroundColor})
if(e.exam_date){
events.push({id,"title":"สอบ","start":e.exam_date,url,backgroundColor})
}
if(e.announcement_date){
events.push({id,"title":"ผลสอบสอบ","start":e.announcement_date,url,backgroundColor})
}
2023-04-04 18:32:58 +07:00
})
2023-04-05 19:35:08 +07:00
return {exams,events,content}
2023-03-27 16:43:56 +07:00
}