From aed5942dbd15359c139322aa14b37b64ec3e89d4 Mon Sep 17 00:00:00 2001 From: Pau Matas Date: Sat, 16 Mar 2024 19:47:55 +0100 Subject: [PATCH] fix: hardcoding just isn't for me --- src/app/api/subject/all/value-label/route.ts | 26 ++++ src/components/Combobox.tsx | 10 +- src/components/Form.tsx | 137 +++---------------- 3 files changed, 48 insertions(+), 125 deletions(-) create mode 100644 src/app/api/subject/all/value-label/route.ts diff --git a/src/app/api/subject/all/value-label/route.ts b/src/app/api/subject/all/value-label/route.ts new file mode 100644 index 0000000..1b63fc7 --- /dev/null +++ b/src/app/api/subject/all/value-label/route.ts @@ -0,0 +1,26 @@ +import { db } from "@/lib/db" + +export async function GET(req: Request) { + const subjects = await db.subject.findMany({ + select: { + acronym: true, + name: true, + }, + orderBy: { + semester: "asc", + }, + }) + + const subjectsValueLabel = subjects.map((subject) => { + return { + value: subject.acronym.toLowerCase(), + label: `${subject.acronym} - ${subject.name}`, + } + }) + + return new Response(JSON.stringify(subjectsValueLabel), { + headers: { + "content-type": "application/json", + }, + }) +} diff --git a/src/components/Combobox.tsx b/src/components/Combobox.tsx index d220fee..8ae33d1 100644 --- a/src/components/Combobox.tsx +++ b/src/components/Combobox.tsx @@ -38,18 +38,20 @@ export function Combobox({ variant="outline" role="combobox" aria-expanded={open} - className="w-[200px] justify-between" + className="w-80 justify-between" > {value ? options.find((option) => option.value === value)?.label - : "Select..."} + : "Selecciona..."} - + - No option found. + + No s'han trobat resultats. + {options.map((option) => ( { + async function fetchAssignatures() { + try { + const response = await axios.get("/api/subject/all/value-label") + setAssignatures(response.data) + } catch (error) { + console.error("Error fetching subjects:", error) + } + } + fetchAssignatures() + }, []) + const tipus = [ { value: "apunts", @@ -264,7 +159,7 @@ export function ProfileForm({ label: "Altres", }, ] - // ------------------------------ + return (