Skip to content

Commit

Permalink
Afegir selector any al pujar apunts
Browse files Browse the repository at this point in the history
  • Loading branch information
marcfranquesa committed Mar 15, 2024
1 parent dd3e1b0 commit 41092f9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/app/[slug]/submit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ const page = async ({ params }: PageProps) => {
Penja apunts {subjectNameArticle}
{subject.name}
</h1>
<ProfileForm PreselectedSubject={slug} isAdmin={isAdmin} />
<ProfileForm
PreselectedSubject={slug}
isAdmin={isAdmin}
session={session}
semester={
subject.semester[0] === "Q" ? parseInt(subject.semester[1]) : 8
}
/>
</>
)
}
Expand Down
10 changes: 1 addition & 9 deletions src/app/api/subject/post/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/components/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ export function Combobox({
options,
value,
setValue,
defaultValue,
}: {
options: Option[]
value: string
setValue: Function
defaultValue?: string
}) {
const [open, setOpen] = React.useState(false)
return (
Expand Down
51 changes: 51 additions & 0 deletions src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
}),
Expand All @@ -43,16 +47,21 @@ const formSchema = z.object({
export function ProfileForm({
PreselectedSubject,
isAdmin,
session,
semester,
}: {
PreselectedSubject: string
isAdmin: boolean
session: Session | null
semester: number
}) {
const router = useRouter()

const { mutate: createApuntsPost } = useMutation({
mutationFn: async ({
pdf,
title,
year,
assignatura,
tipus,
anonim,
Expand All @@ -61,6 +70,7 @@ export function ProfileForm({
const payload: ApuntsPostCreationRequest = {
pdf,
title,
year,
assignatura,
tipus,
anonim,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -264,13 +275,53 @@ 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 (
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit as SubmitHandler<FieldValues>)}
className="space-y-8"
>
<FormField
control={form.control}
name="year"
render={({ field }) => {
if (!field.value) {
field.value = default_year.toString()
}

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>
)
}}
/>
<FormField
control={form.control}
name="pdf"
Expand Down
1 change: 1 addition & 0 deletions src/lib/validators/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const CommentValidator = z.object({
export const ApuntsPostValidator = z.object({
pdf: z.any(),
title: z.string(),
year: z.number(),
assignatura: z.string().min(2).max(5),
tipus: z.string(),
anonim: z.boolean(),
Expand Down

0 comments on commit 41092f9

Please sign in to comment.