-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from vishalkondle45/feature/forum
feat: my-questions & my answers
- Loading branch information
Showing
6 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use client'; | ||
|
||
import { Button, Group, Stack, Title } from '@mantine/core'; | ||
import { useDisclosure, useMediaQuery } from '@mantine/hooks'; | ||
import { ForumItem, AskQuestion } from '@/components/Forum'; | ||
import useFetchData from '@/hooks/useFetchData'; | ||
import { ForumType } from '@/components/Forum/Forum.types'; | ||
import Skelton from '@/components/Skelton/Skelton'; | ||
|
||
const ForumPage = () => { | ||
const { data: forums, loading } = useFetchData('/api/forums/my-answers'); | ||
const [opened, { open, close }] = useDisclosure(false); | ||
const isMobile = useMediaQuery('(max-width: 768px)'); | ||
|
||
return ( | ||
<> | ||
<Group mb="md" justify="space-between"> | ||
<Title>You Answered</Title> | ||
<Button onClick={open}>Ask Question</Button> | ||
</Group> | ||
<Stack> | ||
{loading ? ( | ||
<Skelton items={6} height={90} /> | ||
) : ( | ||
Array.isArray(forums) && | ||
forums?.map((forum: ForumType) => ( | ||
<ForumItem key={String(forum._id)} forum={forum} isMobile={isMobile} /> | ||
)) | ||
)} | ||
</Stack> | ||
<AskQuestion opened={opened} close={close} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default ForumPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use client'; | ||
|
||
import { Button, Group, Stack, Title } from '@mantine/core'; | ||
import { useDisclosure, useMediaQuery } from '@mantine/hooks'; | ||
import { ForumItem, AskQuestion } from '@/components/Forum'; | ||
import useFetchData from '@/hooks/useFetchData'; | ||
import { ForumType } from '@/components/Forum/Forum.types'; | ||
import Skelton from '@/components/Skelton/Skelton'; | ||
|
||
const ForumPage = () => { | ||
const { data: forums, loading } = useFetchData('/api/forums/my-questions'); | ||
const [opened, { open, close }] = useDisclosure(false); | ||
const isMobile = useMediaQuery('(max-width: 768px)'); | ||
|
||
return ( | ||
<> | ||
<Group mb="md" justify="space-between"> | ||
<Title>You Asked</Title> | ||
<Button onClick={open}>Ask Question</Button> | ||
</Group> | ||
<Stack> | ||
{loading ? ( | ||
<Skelton items={6} height={90} /> | ||
) : ( | ||
Array.isArray(forums) && | ||
forums?.map((forum: ForumType) => ( | ||
<ForumItem key={String(forum._id)} forum={forum} isMobile={isMobile} /> | ||
)) | ||
)} | ||
</Stack> | ||
<AskQuestion opened={opened} close={close} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default ForumPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { getServerSession } from 'next-auth'; | ||
import { NextResponse } from 'next/server'; | ||
import startDb from '@/lib/db'; | ||
import Forum from '@/models/Forum'; | ||
import { authOptions } from '../../auth/[...nextauth]/authOptions'; | ||
import { UserDataTypes } from '../../auth/[...nextauth]/next-auth.interfaces'; | ||
|
||
export const maxDuration = 60; | ||
export const dynamic = 'force-dynamic'; | ||
|
||
export async function GET() { | ||
try { | ||
const session: UserDataTypes | null = await getServerSession(authOptions); | ||
if (!session?.user) { | ||
return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); | ||
} | ||
await startDb(); | ||
|
||
const forums = await Forum.find({ | ||
question: { $exists: false }, | ||
description: { $exists: true }, | ||
user: session?.user._id, | ||
}); | ||
|
||
const forumsIds = forums.map((forum) => forum._id); | ||
|
||
const answers = await Forum.find({ answers: { $in: forumsIds } }).populate({ | ||
path: 'user', | ||
select: 'name', | ||
}); | ||
|
||
return NextResponse.json(answers, { status: 200 }); | ||
} catch (error: any) { | ||
return NextResponse.json({ error: error?.message }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { getServerSession } from 'next-auth'; | ||
import { NextResponse } from 'next/server'; | ||
import startDb from '@/lib/db'; | ||
import Forum from '@/models/Forum'; | ||
import { authOptions } from '../../auth/[...nextauth]/authOptions'; | ||
import { UserDataTypes } from '../../auth/[...nextauth]/next-auth.interfaces'; | ||
|
||
export const maxDuration = 60; | ||
export const dynamic = 'force-dynamic'; | ||
|
||
export async function GET() { | ||
try { | ||
const session: UserDataTypes | null = await getServerSession(authOptions); | ||
if (!session?.user) { | ||
return NextResponse.json({ error: 'You are not authorized' }, { status: 401 }); | ||
} | ||
await startDb(); | ||
|
||
const forums = await Forum.find({ | ||
question: { $exists: true }, | ||
description: { $exists: true }, | ||
user: session?.user._id, | ||
}) | ||
.populate({ path: 'user', select: 'name' }) | ||
.sort('-updatedAt'); | ||
|
||
return NextResponse.json(forums, { status: 200 }); | ||
} catch (error: any) { | ||
return NextResponse.json({ error: error?.message }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters