Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Afegir selector any al pujar apunts #71

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/app/[slug]/submit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -31,7 +32,14 @@ const page = async ({ params }: PageProps) => {
Penja apunts {subjectNameArticle}
{subject.name}
</h1>
<ProfileForm PreselectedSubject={slug} isAdmin={isAdmin} />
<ProfileForm
PreselectedSubject={slug}
isAdmin={isAdmin}
generacio={session ? Number(session.user.generacio) : GCED_START}
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
9 changes: 8 additions & 1 deletion src/app/submit/page.tsx
Original file line number Diff line number Diff line change
@@ -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 <ProfileForm PreselectedSubject={"AllSubjects"} isAdmin={isAdmin} />
return (
<ProfileForm
PreselectedSubject={"AllSubjects"}
isAdmin={isAdmin}
generacio={session ? Number(session.user.generacio) : GCED_START}
/>
)
}

export default page
55 changes: 55 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 { GCED_START } from "@/config"

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,
generacio,
semester,
}: {
PreselectedSubject: string
isAdmin: boolean
generacio: number
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,57 @@ export function ProfileForm({
label: "Altres",
},
]

const date = new Date()
const end = date.getFullYear()
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 = semester
? (generacio + Math.floor((semester - 1) / 2)).toString()
: undefined

// ------------------------------
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 && 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>
)
}}
/>
<FormField
control={form.control}
name="pdf"
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const INFINITE_SCROLL_PAGINATION_RESULTS = 4
export const GCED_START = 2017
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