From e63418fb88d7a770863073acf5fe68f98e0365c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Franquesa=20Mon=C3=A9s?= Date: Fri, 15 Mar 2024 19:51:11 +0100 Subject: [PATCH 1/4] Afegir selector any al pujar apunts --- src/app/[slug]/submit/page.tsx | 9 ++++- src/app/api/subject/post/create/route.ts | 10 +---- src/components/Form.tsx | 51 ++++++++++++++++++++++++ src/lib/validators/post.ts | 1 + 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/app/[slug]/submit/page.tsx b/src/app/[slug]/submit/page.tsx index dbf59c4..85585b1 100644 --- a/src/app/[slug]/submit/page.tsx +++ b/src/app/[slug]/submit/page.tsx @@ -31,7 +31,14 @@ const page = async ({ params }: PageProps) => { Penja apunts {subjectNameArticle} {subject.name} - + ) } diff --git a/src/app/api/subject/post/create/route.ts b/src/app/api/subject/post/create/route.ts index fffec0b..ab5350d 100644 --- a/src/app/api/subject/post/create/route.ts +++ b/src/app/api/subject/post/create/route.ts @@ -14,7 +14,7 @@ export async function POST(req: Request) { const body = await req.json() - const { pdf, title, assignatura, tipus, anonim, authorEmail } = + const { pdf, title, year, assignatura, tipus, anonim, authorEmail } = ApuntsPostValidator.parse(body) const subject = await db.subject.findFirst({ @@ -27,14 +27,6 @@ export async function POST(req: Request) { return new Response("Subject not found", { status: 404 }) } - const semester = subject.semester - const semesterNumber = semester[0] === "Q" ? parseInt(semester[1]) : 8 - if (typeof session.user.generacio !== "number") { - return new Response("Invalid generacio", { status: 409 }) - } - const year: number = - session.user.generacio + Math.floor((semesterNumber - 1) / 2) - if ( !["apunts", "examens", "exercicis", "diapositives", "altres"].includes( tipus, diff --git a/src/components/Form.tsx b/src/components/Form.tsx index d6c89de..91546ab 100644 --- a/src/components/Form.tsx +++ b/src/components/Form.tsx @@ -22,12 +22,16 @@ import { Combobox } from "@/components/Combobox" import { Checkbox } from "@/components/ui/checkbox" import { ApuntsPostCreationRequest } from "@/lib/validators/post" import { uploadFiles } from "@/lib/uploadthing" +import { Session } from "next-auth" const formSchema = z.object({ pdf: z.any(), title: z.string({ required_error: "Selecciona un títol", }), + year: z.string({ + required_error: "Selecciona un any", + }), assignatura: z.string({ required_error: "Selecciona una assignatura.", }), @@ -43,9 +47,13 @@ const formSchema = z.object({ export function ProfileForm({ PreselectedSubject, isAdmin, + session, + semester, }: { PreselectedSubject: string isAdmin: boolean + session: Session + semester: number }) { const router = useRouter() @@ -53,6 +61,7 @@ export function ProfileForm({ mutationFn: async ({ pdf, title, + year, assignatura, tipus, anonim, @@ -61,6 +70,7 @@ export function ProfileForm({ const payload: ApuntsPostCreationRequest = { pdf, title, + year, assignatura, tipus, anonim, @@ -115,6 +125,7 @@ export function ProfileForm({ const payload: ApuntsPostCreationRequest = { pdf: res.fileUrl, title: data.title, + year: Number(data.year), assignatura: data.assignatura, tipus: data.tipus, anonim: data.anonim, @@ -264,6 +275,21 @@ export function ProfileForm({ label: "Altres", }, ] + + const date = new Date() + const end = date.getFullYear() + const begin = session ? Number(session.user.generacio) : 2017 // primera promoció + let tipus_any = [] + for (let i = begin; i <= end; i++) { + tipus_any.push({ + value: i.toString(), + label: i.toString(), + }) + } + const default_year = session + ? session.user.generacio + Math.floor((semester - 1) / 2) + : end + // ------------------------------ return (
@@ -271,6 +297,31 @@ export function ProfileForm({ onSubmit={form.handleSubmit(onSubmit as SubmitHandler)} className="space-y-8" > + { + if (!field.value) { + field.value = default_year.toString() + } + + return ( + + Any + + + + + Any que has cursat l'assignatura. + + + ) + }} + /> Date: Fri, 15 Mar 2024 20:02:17 +0100 Subject: [PATCH 2/4] Afegir set year --- src/components/Form.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/Form.tsx b/src/components/Form.tsx index 91546ab..e9712b9 100644 --- a/src/components/Form.tsx +++ b/src/components/Form.tsx @@ -303,6 +303,7 @@ export function ProfileForm({ render={({ field }) => { if (!field.value) { field.value = default_year.toString() + field.onChange(default_year.toString()) } return ( From 67a21fa6b5abd8c3a37a3047ee5c289f4ce9aff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Franquesa=20Mon=C3=A9s?= Date: Sat, 16 Mar 2024 20:14:48 +0100 Subject: [PATCH 3/4] https://www.youtube.com/watch?v=My2FRPA3Gf8 --- src/app/[slug]/submit/page.tsx | 2 +- src/components/Form.tsx | 28 +++++++++++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/app/[slug]/submit/page.tsx b/src/app/[slug]/submit/page.tsx index 85585b1..eea0260 100644 --- a/src/app/[slug]/submit/page.tsx +++ b/src/app/[slug]/submit/page.tsx @@ -34,10 +34,10 @@ const page = async ({ params }: PageProps) => { ) diff --git a/src/components/Form.tsx b/src/components/Form.tsx index e9712b9..064b78c 100644 --- a/src/components/Form.tsx +++ b/src/components/Form.tsx @@ -22,7 +22,6 @@ import { Combobox } from "@/components/Combobox" import { Checkbox } from "@/components/ui/checkbox" import { ApuntsPostCreationRequest } from "@/lib/validators/post" import { uploadFiles } from "@/lib/uploadthing" -import { Session } from "next-auth" const formSchema = z.object({ pdf: z.any(), @@ -47,13 +46,13 @@ const formSchema = z.object({ export function ProfileForm({ PreselectedSubject, isAdmin, - session, semester, + generacio = 2017, }: { PreselectedSubject: string isAdmin: boolean - session: Session - semester: number + semester?: number + generacio?: number }) { const router = useRouter() @@ -278,17 +277,20 @@ export function ProfileForm({ const date = new Date() const end = date.getFullYear() - const begin = session ? Number(session.user.generacio) : 2017 // primera promoció - let tipus_any = [] - for (let i = begin; i <= end; i++) { + interface Year { + value: string + label: string + } + let tipus_any: Year[] = [] + for (let i = generacio; i < end; i++) { tipus_any.push({ value: i.toString(), label: i.toString(), }) } - const default_year = session - ? session.user.generacio + Math.floor((semester - 1) / 2) - : end + const default_year = semester + ? (generacio + Math.floor((semester - 1) / 2)).toString() + : undefined // ------------------------------ return ( @@ -301,9 +303,9 @@ export function ProfileForm({ control={form.control} name="year" render={({ field }) => { - if (!field.value) { - field.value = default_year.toString() - field.onChange(default_year.toString()) + if (!field.value && default_year) { + field.value = default_year + field.onChange(default_year) } return ( From ab4deaa0688d500167e3b44602e46da36e4f2abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Franquesa=20Mon=C3=A9s?= Date: Sat, 16 Mar 2024 20:36:34 +0100 Subject: [PATCH 4/4] BREW UNINSTALL HARDCODED --- src/app/[slug]/submit/page.tsx | 3 ++- src/app/submit/page.tsx | 9 ++++++++- src/components/Form.tsx | 5 +++-- src/config.ts | 1 + 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/app/[slug]/submit/page.tsx b/src/app/[slug]/submit/page.tsx index eea0260..03d297b 100644 --- a/src/app/[slug]/submit/page.tsx +++ b/src/app/[slug]/submit/page.tsx @@ -2,6 +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" interface PageProps { params: { @@ -34,10 +35,10 @@ const page = async ({ params }: PageProps) => { ) diff --git a/src/app/submit/page.tsx b/src/app/submit/page.tsx index 4100ad5..b3b0893 100644 --- a/src/app/submit/page.tsx +++ b/src/app/submit/page.tsx @@ -1,12 +1,19 @@ import { getAuthSession } from "@/lib/auth" import { ProfileForm } from "@/components/Form" import { notFound } from "next/navigation" +import { GCED_START } from "@/config" const page = async ({}) => { const session = await getAuthSession() const isAdmin = session?.user?.isAdmin if (isAdmin === undefined) return notFound() - return + return ( + + ) } export default page diff --git a/src/components/Form.tsx b/src/components/Form.tsx index 064b78c..298c911 100644 --- a/src/components/Form.tsx +++ b/src/components/Form.tsx @@ -22,6 +22,7 @@ import { Combobox } from "@/components/Combobox" import { Checkbox } from "@/components/ui/checkbox" import { ApuntsPostCreationRequest } from "@/lib/validators/post" import { uploadFiles } from "@/lib/uploadthing" +import { GCED_START } from "@/config" const formSchema = z.object({ pdf: z.any(), @@ -46,13 +47,13 @@ const formSchema = z.object({ export function ProfileForm({ PreselectedSubject, isAdmin, + generacio, semester, - generacio = 2017, }: { PreselectedSubject: string isAdmin: boolean + generacio: number semester?: number - generacio?: number }) { const router = useRouter() diff --git a/src/config.ts b/src/config.ts index 53cbb5a..cd179dc 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1 +1,2 @@ export const INFINITE_SCROLL_PAGINATION_RESULTS = 4 +export const GCED_START = 2017