Skip to content

Commit

Permalink
Merge branch 'aux-branch' into 74-any-en-funcio-de-lassignatura
Browse files Browse the repository at this point in the history
  • Loading branch information
PauMatas committed Mar 18, 2024
2 parents dca480c + b483d67 commit be04246
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 42 deletions.
8 changes: 5 additions & 3 deletions src/app/[slug]/submit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ProfileForm } from "@/components/Form"
import { db } from "@/lib/db"
import { getAuthSession } from "@/lib/auth"
import { notFound } from "next/navigation"
import { GCED_START } from "@/config"
import { GCED_START, LAST_SEMESTER } from "@/config"

interface PageProps {
params: {
Expand Down Expand Up @@ -33,11 +33,13 @@ const page = async ({ params }: PageProps) => {
{subject.name}
</h1>
<ProfileForm
PreselectedSubject={slug}
PreselectedSubject={slug.toLowerCase()}
isAdmin={isAdmin}
generacio={session ? Number(session.user.generacio) : GCED_START}
semester={
subject.semester[0] === "Q" ? parseInt(subject.semester[1]) : 8
subject.semester[0] === "Q"
? parseInt(subject.semester[1])
: LAST_SEMESTER
}
/>
</>
Expand Down
21 changes: 21 additions & 0 deletions src/app/api/semester/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { LAST_SEMESTER } from "@/config"
import { db } from "@/lib/db"

export async function GET(req: Request) {
const url = new URL(req.url)
const acronym = url.searchParams.get("acronym")

if (!acronym) return new Response("Invalid query", { status: 400 })

const semester = (
await db.subject.findFirst({
where: { acronym },
select: { semester: true },
})
)?.semester
if (!semester) return new Response("Subject not found", { status: 404 })

const results = semester[0] === "Q" ? parseInt(semester[1]) : LAST_SEMESTER

return new Response(JSON.stringify(results))
}
10 changes: 8 additions & 2 deletions src/components/Combobox.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react"
import { Check, ChevronsUpDown } from "lucide-react"
import { Check, ChevronsUpDown, Loader2 } from "lucide-react"

import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/Button"
Expand All @@ -24,10 +24,12 @@ export function Combobox({
options,
value,
setValue,
isLoading = false,
}: {
options: Option[]
value: string
setValue: Function
isLoading?: boolean
}) {
const [open, setOpen] = React.useState(false)
return (
Expand All @@ -43,7 +45,11 @@ export function Combobox({
{value
? options.find((option) => option.value === value)?.label
: "Selecciona..."}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
{isLoading ? (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
) : (
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
)}
</Button>
</PopoverTrigger>
<PopoverContent className="max-h-80 flex flex-col w-80 p-0">
Expand Down
95 changes: 58 additions & 37 deletions src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Checkbox } from "@/components/ui/checkbox"
import { ApuntsPostCreationRequest } from "@/lib/validators/post"
import { uploadFiles } from "@/lib/uploadthing"
import Fireworks from "react-canvas-confetti/dist/presets/fireworks"
import useSemesterHook from "@/hooks/use-semester-hook"

const formSchema = z.object({
pdf: z.any(),
Expand Down Expand Up @@ -113,6 +114,7 @@ export function ProfileForm({
const form = useForm({
resolver: zodResolver(formSchema),
})
const { data, isLoading } = useSemesterHook(form.watch("assignatura"))
useEffect(() => {
if (PreselectedSubject !== "AllSubjects") {
form.setValue("assignatura", PreselectedSubject)
Expand Down Expand Up @@ -193,6 +195,24 @@ export function ProfileForm({
? (generacio + Math.floor((semester - 1) / 2)).toString()
: undefined

useEffect(() => {
if (!isLoading) {
if (
data === -1 ||
data === undefined ||
data === null ||
!form.watch("assignatura")
) {
form.setValue("year", "")
} else {
form.setValue(
"year",
(generacio + Math.floor((data - 1) / 2)).toString(),
)
}
}
}, [data, isLoading])

return (
<Form {...form}>
<form
Expand All @@ -201,29 +221,17 @@ export function ProfileForm({
>
<FormField
control={form.control}
name="year"
render={({ field }) => {
if (!field.value && default_year) {
field.value = default_year
field.onChange(default_year)
}

return (
<FormItem>
<FormLabel>Any</FormLabel>
<FormControl>
<Combobox
options={tipus_any}
value={field.value}
setValue={field.onChange}
/>
</FormControl>
<FormDescription>
Any que has cursat l&apos;assignatura.
</FormDescription>
</FormItem>
)
}}
name="title"
render={({ field }) => (
<FormItem>
<FormLabel>Nom dels Apunts</FormLabel>
<FormControl>
<Input placeholder="WhoIsGraf?" {...field} />
</FormControl>
<FormDescription>El nom dels teus apunts.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
Expand Down Expand Up @@ -251,20 +259,6 @@ export function ProfileForm({
</FormItem>
)}
/>
<FormField
control={form.control}
name="title"
render={({ field }) => (
<FormItem>
<FormLabel>Nom dels Apunts</FormLabel>
<FormControl>
<Input placeholder="WhoIsGraf?" {...field} />
</FormControl>
<FormDescription>El nom dels teus apunts.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
{PreselectedSubject === "AllSubjects" && (
<FormField
control={form.control}
Expand All @@ -285,6 +279,33 @@ export function ProfileForm({
/>
)}

<FormField
control={form.control}
name="year"
render={({ field }) => {
if (!field.value && default_year) {
field.value = default_year
field.onChange(default_year)
}

return (
<FormItem>
<FormLabel>Any</FormLabel>
<FormControl>
<Combobox
options={tipus_any}
value={field.value}
setValue={field.onChange}
isLoading={isLoading}
/>
</FormControl>
<FormDescription>
Any que has cursat l&apos;assignatura.
</FormDescription>
</FormItem>
)
}}
/>
<FormField
control={form.control}
name="tipus"
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const INFINITE_SCROLL_PAGINATION_RESULTS = 4
export const GCED_START = 2017
export const LAST_SEMESTER = 8
32 changes: 32 additions & 0 deletions src/hooks/use-semester-hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useState, useEffect } from "react"
import axios from "axios" // You might need to install axios
import { LAST_SEMESTER } from "@/config"

const useSemesterHook = (subjectAcronym: string) => {
const [data, setData] = useState(LAST_SEMESTER)
const [isLoading, setIsLoading] = useState(false)

useEffect(() => {
const fetchData = async () => {
setIsLoading(true)
if (subjectAcronym === "") {
setData(-1)
} else {
const response = await axios.get(
`/api/semester?acronym=${subjectAcronym}`,
)
setData(response.data)
}
setIsLoading(false)
}

if (subjectAcronym !== null && subjectAcronym !== undefined) {
// Assuming subjectAcronym can be null or undefined initially
fetchData()
}
}, [subjectAcronym])

return { data, isLoading, setIsLoading }
}

export default useSemesterHook
2 changes: 2 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
import { formatDistanceToNowStrict } from "date-fns"
import locale from "date-fns/locale/en-US"
import { db } from "./db"
import { LAST_SEMESTER } from "@/config"

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
Expand Down

0 comments on commit be04246

Please sign in to comment.