From 68316601f1fc0e8e47fb1e44a3575380ff3e1160 Mon Sep 17 00:00:00 2001 From: betich Date: Tue, 14 May 2024 00:53:28 +0700 Subject: [PATCH] feat: fetch student data as surname is validated & building name utils --- .../get_room/03_display_data.client.tsx | 81 +++++-------------- .../announce/get_room/get_room.client.tsx | 15 ++-- src/lib/utils.ts | 33 +++++++- src/schema.ts | 15 ++++ 4 files changed, 75 insertions(+), 69 deletions(-) diff --git a/src/components/announce/get_room/03_display_data.client.tsx b/src/components/announce/get_room/03_display_data.client.tsx index b99e86d..5ba3a76 100644 --- a/src/components/announce/get_room/03_display_data.client.tsx +++ b/src/components/announce/get_room/03_display_data.client.tsx @@ -1,37 +1,13 @@ "use client"; +import { getBuildingName } from "@/lib/utils"; import { type Process } from "./get_room.client"; import { AnnounceHeading } from "./heading"; import { Skeleton } from "@/components/ui/skeleton"; -import { useFetch } from "@/lib/fetch"; +import type { student as StudentData } from "@/schema"; interface DisplayDataProps { process: Process; - studentId: string; -} - -/* -ex. -ชื่อ นางสาวสุทธิชา สีผาย -เลขประจำตัว 63012 -แผนการเรียน วิทย์ - คณิต -ชั้น ม.5 -ตึก คุณหญิงหรั่ง กันตารัติ -ห้อง 833 -เลขที่ 20 -ครูที่ปรึกษา เรียนเด่น เล่นดี - เรียนเด่น เล่นดี -*/ - -// to be replaced with actual type -interface StudentData { - name: string; - studentId: string; - academicProgram: string; - grade: string; - building: string; - room: string; - number: string; - advisor: string[]; + studentData: StudentData; } const ClipboardIcon = () => ( @@ -99,19 +75,19 @@ const StudentDataPanel = ({ studentData }: { studentData: StudentData }) => (

เลขประจำตัว

-

{studentData.studentId}

+

{studentData.id}

แผนการเรียน

-

{studentData.academicProgram}

+

{studentData.program}

ชั้น

-

{studentData.grade}

+

{studentData.level}

ตึก

-

{studentData.building}

+

{getBuildingName(studentData.room)}

ห้อง

@@ -124,36 +100,28 @@ const StudentDataPanel = ({ studentData }: { studentData: StudentData }) => (

ครูที่ปรึกษา

- {studentData.advisor.map((advisor, index) => ( + {/* {studentData.advisor.map((advisor, index) => (

{advisor}

- ))} + ))} */}
+
+

รหัสไวไฟ

+
{studentData.wifi}
+
+
+

อีเมล

+
{studentData.outlook}
+
); -const mock = true; - -export default function DisplayData({ studentId, process }: DisplayDataProps) { - const { data: studentData, loading: studentDataLoading } = - useFetch( - `/api/student/${studentId}/data`, - mock - ? { - name: "นางสาวสุทธิชา สีผาย", - studentId: "63012", - academicProgram: "วิทย์ - คณิต", - grade: "ม.5", - building: "คุณหญิงหรั่ง กันตารัติ", - room: "833", - number: "20", - advisor: ["เรียนเด่น เล่นดี", "เรียนเด่น เล่นดี"], - } - : undefined, - ); - +export default function DisplayData({ + studentData, + process, +}: DisplayDataProps) { return (
ข้อมูลนักเรียน
- {/* student data */} - {studentDataLoading ? ( - - ) : ( - - )} + )} diff --git a/src/components/announce/get_room/get_room.client.tsx b/src/components/announce/get_room/get_room.client.tsx index a02125a..27ed870 100644 --- a/src/components/announce/get_room/get_room.client.tsx +++ b/src/components/announce/get_room/get_room.client.tsx @@ -1,9 +1,9 @@ "use client"; -import { useCallback, useState } from "react"; +import { useState } from "react"; import StudentId from "./01_student_id.client"; import ValidateSurname from "./02_validate_surname.client"; import DisplayData from "./03_display_data.client"; -import { httpFetch, mockFetch } from "@/lib/fetch"; +import { student as StudentData, emptyStudent } from "@/schema"; export type Process = "idle" | "editing" | "done"; @@ -24,6 +24,7 @@ export default function GetRoom() { studentId: "", firstNameCheck: "", }); + const [studentData, setStudentData] = useState(emptyStudent); const [process, setProcess] = useState< Record<"step_1" | "step_2" | "step_3", Process> @@ -50,7 +51,7 @@ export default function GetRoom() { setStudentInput({ studentId: studentId, - firstNameCheck: studentName.firstname, + firstNameCheck: studentName, }); }; @@ -66,6 +67,9 @@ export default function GetRoom() { if (validateSurname.status === 404) { throw new Error("Invalid surname"); } + + const studentData = await validateSurname.json(); + setStudentData(studentData); }; return ( @@ -108,10 +112,7 @@ export default function GetRoom() { saveInput={handleSaveSurname} firstNameCheck={studentInput.firstNameCheck} /> - + ); diff --git a/src/lib/utils.ts b/src/lib/utils.ts index d084cca..ea03881 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,6 +1,33 @@ -import { type ClassValue, clsx } from "clsx" -import { twMerge } from "tailwind-merge" +import { type ClassValue, clsx } from "clsx"; +import { twMerge } from "tailwind-merge"; export function cn(...inputs: ClassValue[]) { - return twMerge(clsx(inputs)) + return twMerge(clsx(inputs)); +} + +const buildingNames: Record = { + "1": "ตึก 2", + "2": "ตึก 3", + "3": "ตึก 60 ปี", + "4": "ตึกเฉลิมพระเกียรติฯ 72 พรรษา", + "6": "ตึก 55 ปี", + "8": "ตึก คุณหญิงหรั่งฯ", + "9": "ตึก 9", +}; + +const building1 = [3, 4, 5]; +const artBuilding = [6, 7, 8]; + +export function getBuildingName(room: string) { + // if building is two digits and the first digit is 3,4,5 then it is building 1 + + if (room.length === 2 && building1.includes(parseInt(room[0]))) { + return "ตึก 1"; + } + + if (room.length === 2 && artBuilding.includes(parseInt(room[0]))) { + return "ตึกศิลปะ"; + } + + return buildingNames[room[0]]; } diff --git a/src/schema.ts b/src/schema.ts index 2951dd2..72c8395 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -16,4 +16,19 @@ export const StudentsTable = pgTable("students", { level: integer("level").notNull(), }); +export const emptyStudent = { + id: 0, + title: "", + name: "", + lastname: "", + room: "", + number: 0, + program: "", + gmail: "", + outlook: "", + wifi: "", + password: "", + level: 0, +}; + export type student = InferSelectModel;