Skip to content

Commit

Permalink
fix: hardcoding just isn't for me
Browse files Browse the repository at this point in the history
  • Loading branch information
PauMatas committed Mar 16, 2024
1 parent 3273508 commit aed5942
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 125 deletions.
26 changes: 26 additions & 0 deletions src/app/api/subject/all/value-label/route.ts
Original file line number Diff line number Diff line change
@@ -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",
},
})
}
10 changes: 6 additions & 4 deletions src/components/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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..."}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="max-h-80 flex flex-col w-[200px] p-0">
<PopoverContent className="max-h-80 flex flex-col w-80 p-0">
<Command>
<CommandInput placeholder="Search..." />
<CommandEmpty className="flex-1">No option found.</CommandEmpty>
<CommandEmpty className="flex-1">
No s&apos;han trobat resultats.
</CommandEmpty>
<CommandGroup className="flex-1 h-full overflow-y-scroll">
{options.map((option) => (
<CommandItem
Expand Down
137 changes: 16 additions & 121 deletions src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useMutation } from "@tanstack/react-query"
import axios from "axios"
import { useRouter } from "next/navigation"
import { toast } from "@/hooks/use-toast"
import { useEffect } from "react"
import { useEffect, useState } from "react"
import { Button } from "@/components/ui/Button"
import {
Form,
Expand Down Expand Up @@ -48,6 +48,7 @@ export function ProfileForm({
isAdmin: boolean
}) {
const router = useRouter()
const [assignatures, setAssignatures] = useState([])

const { mutate: createApuntsPost } = useMutation({
mutationFn: async ({
Expand Down Expand Up @@ -123,125 +124,19 @@ export function ProfileForm({

createApuntsPost(payload)
}
// ------------------------------
const assignatures = [
{
value: "alg",
label: "ALG",
},
{
value: "cal",
label: "CAL",
},
{
value: "lmd",
label: "LMD",
},
{
value: "ap1",
label: "AP1",
},
{
value: "ap2",
label: "AP2",
},
{
value: "ac2",
label: "AC2",
},
{
value: "pie1",
label: "PIE1",
},
{
value: "com",
label: "COM",
},
{
value: "sis",
label: "SIS",
},
{
value: "ap3",
label: "AP3",
},
{
value: "teoi",
label: "TEOI",
},
{
value: "pie2",
label: "PIE2",
},
{
value: "bd",
label: "BD",
},
{
value: "psd",
label: "PSD",
},
{
value: "ipa",
label: "IPA",
},
{
value: "om",
label: "OM",
},
{
value: "ad",
label: "AD",
},
{
value: "aa1",
label: "AA1",
},
{
value: "vi",
label: "VI",
},
{
value: "cai",
label: "CAI",
},
{
value: "bda",
label: "BDA",
},
{
value: "aa2",
label: "AA2",
},
{
value: "ei",
label: "EI",
},
{
value: "taed1",
label: "TAED1",
},
{
value: "poe",
label: "POE",
},
{
value: "piva",
label: "PIVA",
},
{
value: "pe",
label: "PE",
},
{
value: "taed2",
label: "TAED2",
},
{
value: "altres",
label: "Altres",
},
]

useEffect(() => {
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",
Expand All @@ -264,7 +159,7 @@ export function ProfileForm({
label: "Altres",
},
]
// ------------------------------

return (
<Form {...form}>
<form
Expand Down

0 comments on commit aed5942

Please sign in to comment.