Skip to content

Commit

Permalink
Merge branch 'main' into 1-scrolling-combobox
Browse files Browse the repository at this point in the history
  • Loading branch information
PauMatas committed Mar 17, 2024
2 parents aed5942 + a5a7fbd commit ec68d1c
Show file tree
Hide file tree
Showing 23 changed files with 332 additions and 248 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"papaparse": "^5.4.1",
"postcss": "8.4.23",
"react": "^18.2.0",
"react-canvas-confetti": "^2.0.7",
"react-dom": "^18.2.0",
"react-dropzone": "^14.2.3",
"react-hook-form": "^7.50.1",
Expand Down
39 changes: 9 additions & 30 deletions src/app/[slug]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import SubscribeLeaveToggle from "@/components/SubscribeLeaveToggle"
import { buttonVariants } from "@/components/ui/Button"
import { getAuthSession } from "@/lib/auth"
import { db } from "@/lib/db"
Expand All @@ -8,14 +7,14 @@ import {
FileTextIcon,
InfoIcon,
} from "lucide-react"
import Link from "next/link"
import { notFound } from "next/navigation"
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip"
import SubscribeHeartToggle from "@/components/SubscribeHeartToggle"

const Layout = async ({
children,
Expand Down Expand Up @@ -168,40 +167,20 @@ const Layout = async ({
</p>
</div>
) : null}

{subject.creatorId !== session?.user?.id ? (
<SubscribeLeaveToggle
subjectId={subject.id}
subjectName={subject.name}
isSubscribed={isSubscribed}
/>
) : null}

<Link
href={"/submit"}
className={buttonVariants({
variant: "outline",
className: "w-full mb-2",
})}
>
Penja Apunts
</Link>

<Link
href={`/${slug}/q`}
className={buttonVariants({
variant: "outline",
className: "w-full mb-6",
})}
>
Llança una pregunta
</Link>
</dl>
</div>
</TooltipContent>
</Tooltip>
</TooltipProvider>

{subject.creatorId !== session?.user?.id ? (
<SubscribeHeartToggle
subjectId={subject.id}
subjectName={subject.name}
isSubscribed={isSubscribed}
/>
) : null}

<div className="grid grid-cols-1 md:grid-cols-3 gap-y-4 md:gap-x-4 py-6">
<div className="flex flex-col col-span-3 space-y-6">{children}</div>
</div>
Expand Down
10 changes: 7 additions & 3 deletions src/app/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MiniCreatePost from "@/components/MiniCreatePost"
import CreatePost from "@/components/CreatePost"
import PostFeed from "@/components/PostFeed"
import { INFINITE_SCROLL_PAGINATION_RESULTS } from "@/config"
import { getAuthSession } from "@/lib/auth"
Expand All @@ -23,7 +23,11 @@ const page = async ({ params }: PageProps) => {
include: {
author: true,
votes: true,
comments: true,
comments: {
include: {
_count: true,
},
},
subject: true,
},
orderBy: {
Expand All @@ -44,7 +48,7 @@ const page = async ({ params }: PageProps) => {
{subject.name}
</h1>

<MiniCreatePost session={session} />
<CreatePost session={session} />

<PostFeed initialPosts={subject.posts} subjectAcronym={subject.acronym} />
</>
Expand Down
4 changes: 2 additions & 2 deletions src/app/[slug]/q/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { INFINITE_SCROLL_PAGINATION_RESULTS } from "@/config"
import { getAuthSession } from "@/lib/auth"
import { db } from "@/lib/db"
import MiniCreateQuestion from "@/components/MiniCreateQuestion"
import CreateQuestion from "@/components/CreateQuestion"
import QuestionFeed from "@/components/QuestionFeed"
import { notFound } from "next/navigation"

Expand Down Expand Up @@ -41,7 +41,7 @@ const page = async ({ params }: PageProps) => {
{subject.name} - Preguntes
</h1>

<MiniCreateQuestion session={session} subjectId={subject.id} />
<CreateQuestion session={session} subjectId={subject.id} />

<QuestionFeed
initialQuestions={subject.questions}
Expand Down
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
2 changes: 1 addition & 1 deletion src/app/api/subject/answer/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function POST(req: Request) {
authorId: session.user.id,
},
})
return new Response("Answer created", { status: 201 })
return new Response(JSON.stringify(questionId), { status: 201 })
} catch (error) {
if (error instanceof z.ZodError) {
return new Response(error.message, { status: 422 })
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
141 changes: 95 additions & 46 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// import CustomFeed from "@/components/CustomFeed";
import { buttonVariants } from "@/components/ui/Button"
// import { getAuthSession } from "@/lib/auth";
import { getAuthSession } from "@/lib/auth"
import { FileTextIcon, HelpCircleIcon, HomeIcon, BookIcon } from "lucide-react"
import Link from "next/link"

import { db } from "@/lib/db"
// import { HeartIcon, HeartPulseIcon } from "lucide-react";
import { Badge } from "@/components/ui/Badge"
import { cn } from "@/lib/utils"
import SubscribeHeartToggle from "@/components/SubscribeHeartToggle"

export default async function Home() {
// const session = await getAuthSession();
const session = await getAuthSession()

const subjects = await db.subject.findMany({
select: {
Expand All @@ -31,17 +30,41 @@ export default async function Home() {
},
})

// const subscription = !session?.user
// ? undefined
// : await db.subscription.findFirst({
// where: {
// userId: session.user.id,
// subjectId: subjects.id,
// },
// });
const subscribedSubjects = await db.subscription.findMany({
where: {
userId: session?.user.id,
},
select: {
subject: {
select: {
id: true,
acronym: true,
name: true,
semester: true,
posts: {
select: {
_count: true,
},
},
questions: {
select: {
_count: true,
},
},
},
},
},
orderBy: {
subject: {
semester: "asc",
},
},
})

// const isSubscribed = !!subscription;
// const ColorClass = isSubscribed ? "text-red-500" : "text-black";
const notSubscribedSubjects = subjects.filter(
(subject) =>
!subscribedSubjects.some((sub) => sub.subject.id === subject.id),
)

function semesterColor(semester: string) {
switch (semester) {
Expand All @@ -66,35 +89,6 @@ export default async function Home() {
<>
<h1 className="font-bold text-3xl md:text-4xl">El teu espai</h1>
<div className="grid grid-cols-1 md:grid-cols-3 gap-y-4 md:gap-x-4 py-6">
{/* Feed
{session ? <CustomFeed /> : null} */}

{/* subjects info */}
{/* <div className="overflow-hidden h-fit rounded-lg border border-gray-200 order-first md:order-last mb-4">
<div className="bg-emerald-100 px-6 py-4">
<p className="font-semibold py-3 flex items-center gap-1.5">
<HomeIcon className="w-4 h-4" />
Home
</p>
</div>
<div className="-my-3 divide-y divide-gray-100 px-6 py-4 text-sm leading-6">
<div className="flex justify-between gap-x-4 py-3">
<p className="text-zinc-500">
La teva pàgina d Apunts de Dades. Accedeix aquí per a veure els apunts de les assignatures que
t interessen.
</p>
</div>
<Link
className={buttonVariants({
className: "w-full mt-4 mb-6",
})}
href="/create">
Crea una assignatura
</Link>
</div>
</div> */}
<div className="overflow-hidden h-fit rounded-lg border border-gray-200 order-first mb-4">
<div className="bg-pink-100 px-6 py-4">
<p className="font-semibold py-3 flex items-center gap-1.5">
Expand Down Expand Up @@ -137,7 +131,55 @@ export default async function Home() {
</div>

<div className="grid grid-cols-1 md:grid-cols-3 gap-y-4 md:gap-x-4 py-6">
{subjects.map((subject, index) => {
{subscribedSubjects.map((subscription, index) => {
return (
<Link
key={index}
className="w-full mt-4 mb-6"
href={`/${subscription.subject.acronym}`}
>
<div className="overflow-hidden h-fit rounded-lg border border-gray-200 order-first md:order-last">
<div
className={cn(
"px-6 py-2 flex justify-between items-center",
semesterColor(subscription.subject.semester),
)}
>
<p className="py-1 flex items-center gap-1.5">
<BookIcon className="w-4 h-4" />
{subscription.subject.name}
</p>
<SubscribeHeartToggle
subjectId={subscription.subject.id}
subjectName={subscription.subject.name}
isSubscribed={true}
/>
</div>

<div className="-my-3 divide-y divide-gray-100 px-6 py-4 text-sm leading-6 space-x-2">
<Badge variant="outline">
{subscription.subject.semester}
</Badge>
<Badge variant="secondary">
{subscription.subject.acronym}
</Badge>
<Badge variant="outline">
{subscription.subject.posts.length}
<FileTextIcon className="w-3 h-3 ml-2" />
</Badge>
<Badge variant="outline">
{subscription.subject.questions.length}
<HelpCircleIcon className="w-3 h-3 ml-2" />
</Badge>
</div>
</div>
</Link>
)
})}
</div>

<div className="grid grid-cols-1 md:grid-cols-3 gap-y-4 md:gap-x-4 py-6">
{notSubscribedSubjects.map((subject, index) => {
return (
<Link
key={index}
Expand All @@ -146,13 +188,20 @@ export default async function Home() {
>
<div className="overflow-hidden h-fit rounded-lg border border-gray-200 order-first md:order-last">
<div
className={cn("px-6 py-2", semesterColor(subject.semester))}
className={cn(
"px-6 py-2 flex justify-between items-center",
semesterColor(subject.semester),
)}
>
<p className="py-1 flex items-center gap-1.5">
<BookIcon className="w-4 h-4" />
{subject.name}
{/* <HeartIcon className={cn("h-5 w-5", ColorClass)} /> */}
</p>
<SubscribeHeartToggle
subjectId={subject.id}
subjectName={subject.name}
isSubscribed={false}
/>
</div>

<div className="-my-3 divide-y divide-gray-100 px-6 py-4 text-sm leading-6 space-x-2">
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
Loading

0 comments on commit ec68d1c

Please sign in to comment.