Skip to content

Commit

Permalink
Merge pull request #45 from vishalkondle45/feature/forum
Browse files Browse the repository at this point in the history
feat: my-questions & my answers
  • Loading branch information
vishalkondle-dev authored Jul 11, 2024
2 parents d8e4ef1 + 23569ba commit ede7a33
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 5 deletions.
36 changes: 36 additions & 0 deletions app/(private)/forum/my-answers/page.tsx
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;
36 changes: 36 additions & 0 deletions app/(private)/forum/my-questions/page.tsx
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;
36 changes: 36 additions & 0 deletions app/api/forums/my-answers/route.ts
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 });
}
}
31 changes: 31 additions & 0 deletions app/api/forums/my-questions/route.ts
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 });
}
}
3 changes: 2 additions & 1 deletion app/api/forums/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ export async function DELETE(req: NextRequest) {
return NextResponse.json({ error: 'You are not authorized' }, { status: 401 });
}
await startDb();
const fourm = await Forum.findById(req.nextUrl.searchParams.get('_id'));
await Forum.findByIdAndDelete(req.nextUrl.searchParams.get('_id'));
await Forum.deleteMany({ parent: req.nextUrl.searchParams.get('_id') });
await Forum.deleteMany({ _id: { $in: fourm?.answers } });
return NextResponse.json(null, { status: 200 });
} catch (error: any) {
return NextResponse.json({ error: error?.message }, { status: 500 });
Expand Down
12 changes: 8 additions & 4 deletions lib/constants.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HarmBlockThreshold, HarmCategory } from '@google/generative-ai';
import {
IconArchive,
IconBookmarkFilled,
IconBookmark,
IconCalendar,
IconCalendarDown,
IconCalendarUp,
Expand All @@ -11,6 +11,7 @@ import {
IconCode,
IconLock,
IconMessageQuestion,
IconMessageReply,
IconMessages,
IconNote,
IconNumber0Small,
Expand Down Expand Up @@ -40,9 +41,10 @@ import {
IconNumber9Small,
IconPlus,
IconPrompt,
IconQuestionMark,
IconRobot,
IconStar,
IconTagsFilled,
IconTags,
IconTrash,
IconWallet,
} from '@tabler/icons-react';
Expand Down Expand Up @@ -139,8 +141,10 @@ export const APPS: AppType[] = [
color: 'indigo',
sidebar: [
{ label: 'Home', path: '/forum', icon: <IconMessageQuestion /> },
{ label: 'Tags', path: '/forum/tags', icon: <IconTagsFilled /> },
{ label: 'Saved', path: '/forum/saved', icon: <IconBookmarkFilled /> },
{ label: 'Tags', path: '/forum/tags', icon: <IconTags /> },
{ label: 'Saved', path: '/forum/saved', icon: <IconBookmark /> },
{ label: 'You Asked', path: '/forum/my-questions', icon: <IconQuestionMark /> },
{ label: 'You Answered', path: '/forum/my-answers', icon: <IconMessageReply /> },
],
},
{ label: 'Passwords', path: '/passwords', icon: <IconLock />, color: 'red', sidebar: [] },
Expand Down

0 comments on commit ede7a33

Please sign in to comment.