|
| 1 | +import { fetchUserData } from '@/hooks/fetchUserData'; |
| 2 | +import { type Schedule, type ScheduleSlot } from '@/types/Timetable'; |
| 3 | +import { Time, timeConvert } from '@/utils/Times'; |
| 4 | +import React from 'react' |
| 5 | + |
| 6 | +export default async function ViewAll() { |
| 7 | + const {timetable, ophour} = await fetchUserData(); |
| 8 | + |
| 9 | + const ophours = ophour?.split(","); |
| 10 | + if (ophours?.[0]) { |
| 11 | + for (const ophour of ophours) { |
| 12 | + const [day, hour] = ophour.split("-"); |
| 13 | + const dayIndex = Number.parseInt(day.replace("D", "")) - 1; |
| 14 | + const hourIndex = Number.parseInt(hour.replace("H", "")) - 1; |
| 15 | + |
| 16 | + const slot = timetable.schedule[dayIndex]?.table[hourIndex]; |
| 17 | + if (slot) slot.isOptional = true; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | + return ( |
| 23 | + <main className='max-w-screen h-screen w-screen overflow-hidden flex items-center justify-center'> |
| 24 | + <div className='flex flex-col max-w-[1710px] w-full h-full aspect-[1710/700] relative'> |
| 25 | + <div className='absolute inset-0'> |
| 26 | + <TimeArr /> |
| 27 | + <TimetableImage timetable={timetable.schedule} /> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | + </main> |
| 31 | + ) |
| 32 | +} |
| 33 | + |
| 34 | +function TimeArr() { |
| 35 | + return ( |
| 36 | + <div className="flex flex-row justify-between w-full px-12 h-[50px]"> |
| 37 | + {Time.start.map((start, index) => ( |
| 38 | + <div |
| 39 | + className="flex flex-col items-center justify-center" |
| 40 | + key={index} |
| 41 | + > |
| 42 | + <p className="text-white text-lg opacity-70"> |
| 43 | + {timeConvert(start)} - {timeConvert(Time.end[index])} |
| 44 | + </p> |
| 45 | + </div> |
| 46 | + ))} |
| 47 | + </div> |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +function TimetableImage({ timetable }: { timetable: Schedule[] }) { |
| 52 | + return ( |
| 53 | + <div |
| 54 | + className="grid grid-rows-5" |
| 55 | + > |
| 56 | + {timetable.map((item, index) => ( |
| 57 | + <ImageGenerator timetable={item} key={index} /> |
| 58 | + ))} |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +function ImageGenerator({ timetable }: { timetable: Schedule }) { |
| 64 | + const theoryPosition = timetable?.table |
| 65 | + ?.slice(0, 5) |
| 66 | + .some((item) => item?.courseType === "Theory") |
| 67 | + ? 0 |
| 68 | + : 1; |
| 69 | + |
| 70 | + return ( |
| 71 | + <div className="h-full grid grid-cols-2"> |
| 72 | + <div |
| 73 | + className={`${theoryPosition === 0 ? "bg-[#F2D869]" : "bg-[#69E069]"} h-full grid grid-cols-5`} |
| 74 | + > |
| 75 | + {timetable?.table?.slice(0, 5).map((item, index) => ( |
| 76 | + <TableCell key={index} cell={item} /> |
| 77 | + ))} |
| 78 | + </div> |
| 79 | + <div |
| 80 | + className={`${theoryPosition === 0 ? "bg-[#69E069]" : "bg-[#F2D869]"} h-full grid grid-cols-5`} |
| 81 | + > |
| 82 | + {timetable?.table?.slice(5, 10).map((item, index) => ( |
| 83 | + <TableCell key={index} cell={item} /> |
| 84 | + ))} |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +function TableCell({ cell }: { cell: ScheduleSlot | null }) { |
| 91 | + return ( |
| 92 | + <div |
| 93 | + className={`border-2 flex h-[130px] flex-col text-black relative justify-between items-start px-3 py-2 ${!cell ? "bg-black/80" : cell.isOptional ? "bg-black/40" : ""} border-black/60 w-full`} |
| 94 | + > |
| 95 | + <p className="text-base font-semibold text-left mr-3"> |
| 96 | + {cell?.name.split(":")[0]} |
| 97 | + </p> |
| 98 | + <div className="flex items-end justify-between w-full opacity-60"> |
| 99 | + <p className="text-sm font-semibold">{cell?.roomNo}</p> |
| 100 | + {cell?.isOptional && <p className="text-sm font-semibold">(Optional)</p>} |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + ); |
| 104 | +} |
0 commit comments