-
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 #2 from vishalkondle45/feature/todos
todos page with skelton
- Loading branch information
Showing
16 changed files
with
537 additions
and
22 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,14 @@ | ||
import { Container } from '@mantine/core'; | ||
import { ReactNode } from 'react'; | ||
|
||
interface Props { | ||
children: ReactNode; | ||
} | ||
|
||
export default async function RootLayout({ children }: Props) { | ||
return ( | ||
<Container px={0} size="sm"> | ||
{children} | ||
</Container> | ||
); | ||
} |
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,18 @@ | ||
'use client'; | ||
|
||
import { TodoPageActions } from '@/components/Todo'; | ||
import Todos from '@/components/Todo/Todo'; | ||
import TodoSkelton from '@/components/Todo/TodoSkelton'; | ||
import useFetchData from '@/hooks/useFetchData'; | ||
|
||
const TodosPage = () => { | ||
const { data, refetch, loading } = useFetchData('/api/todos'); | ||
return ( | ||
<> | ||
<TodoPageActions refetch={refetch} /> | ||
{loading ? <TodoSkelton /> : <Todos todos={data} />} | ||
</> | ||
); | ||
}; | ||
|
||
export default TodosPage; |
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 { NextRequest, NextResponse } from 'next/server'; | ||
import startDb from '@/lib/db'; | ||
import TodoList from '@/models/TodoList'; | ||
import { authOptions } from '../auth/[...nextauth]/authOptions'; | ||
import { UserDataTypes } from '../auth/[...nextauth]/next-auth.interfaces'; | ||
|
||
export async function GET(req: NextRequest) { | ||
try { | ||
const session: UserDataTypes | null = await getServerSession(authOptions); | ||
if (!session?.user) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); | ||
} | ||
await startDb(); | ||
let list: any[] = []; | ||
const schema = req.nextUrl.searchParams.get('schema')?.toString(); | ||
switch (schema) { | ||
case 'todos': | ||
list = await TodoList.find({ user: session?.user._id }).sort('-updatedAt'); | ||
break; | ||
default: | ||
break; | ||
} | ||
return NextResponse.json( | ||
list.map((i) => ({ path: `/${schema}/${i?._id}`, label: i?.title, color: i?.color })), | ||
{ 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,45 @@ | ||
import { getServerSession } from 'next-auth'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import mongoose from 'mongoose'; | ||
import startDb from '@/lib/db'; | ||
import Todo from '@/models/Todo'; | ||
import { authOptions } from '../auth/[...nextauth]/authOptions'; | ||
import { UserDataTypes } from '../auth/[...nextauth]/next-auth.interfaces'; | ||
|
||
export async function GET() { | ||
try { | ||
const session: UserDataTypes | null = await getServerSession(authOptions); | ||
if (!session?.user) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); | ||
} | ||
await startDb(); | ||
const todoList = await Todo.find({ user: session?.user._id }) | ||
.populate({ | ||
path: 'list', | ||
select: 'title color -_id', | ||
}) | ||
.sort('-updatedAt'); | ||
return NextResponse.json(todoList, { status: 200 }); | ||
} catch (error: any) { | ||
return NextResponse.json({ error: error?.message }, { status: 500 }); | ||
} | ||
} | ||
export async function POST(req: NextRequest) { | ||
try { | ||
const session: UserDataTypes | null = await getServerSession(authOptions); | ||
if (!session?.user) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); | ||
} | ||
const body = await req.json(); | ||
await startDb(); | ||
if (body.list) { | ||
body.list = new mongoose.Types.ObjectId(String(body.list)); | ||
} else { | ||
body.list = null; | ||
} | ||
const todoList = await Todo.create({ ...body, user: session?.user._id }); | ||
return NextResponse.json(todoList, { 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,34 @@ | ||
import { getServerSession } from 'next-auth'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import startDb from '@/lib/db'; | ||
import TodoList from '@/models/TodoList'; | ||
import { authOptions } from '../../auth/[...nextauth]/authOptions'; | ||
import { UserDataTypes } from '../../auth/[...nextauth]/next-auth.interfaces'; | ||
|
||
export async function GET() { | ||
try { | ||
const session: UserDataTypes | null = await getServerSession(authOptions); | ||
if (!session?.user) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); | ||
} | ||
await startDb(); | ||
const todoList = await TodoList.find({ user: session?.user._id }).sort('-updatedAt'); | ||
return NextResponse.json(todoList, { status: 200 }); | ||
} catch (error: any) { | ||
return NextResponse.json({ error: error?.message }, { status: 500 }); | ||
} | ||
} | ||
export async function POST(req: NextRequest) { | ||
try { | ||
const session: UserDataTypes | null = await getServerSession(authOptions); | ||
if (!session?.user) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); | ||
} | ||
const body = await req.json(); | ||
await startDb(); | ||
const todoList = await TodoList.create({ ...body, user: session?.user._id }); | ||
return NextResponse.json(todoList, { 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React from 'react'; | ||
import { ActionIcon, Badge, Group, Paper, Stack, Text } from '@mantine/core'; | ||
import { IconCalendar, IconCircleCheckFilled, IconList, IconStarFilled } from '@tabler/icons-react'; | ||
import dayjs from 'dayjs'; | ||
import { TodoType } from '@/models/Todo'; | ||
|
||
interface Props { | ||
todos: TodoType[]; | ||
} | ||
|
||
const Todos = ({ todos }: Props) => ( | ||
<Stack> | ||
{todos?.map((i: TodoType) => ( | ||
<Paper bg={`${i.color}.3`} p="md" key={String(i._id)}> | ||
<Stack gap="xs"> | ||
<Group gap="xs" wrap="nowrap" justify="space-between"> | ||
<Group gap="xs" wrap="nowrap"> | ||
<ActionIcon color="gray.0" variant="transparent"> | ||
<IconCircleCheckFilled /> | ||
</ActionIcon> | ||
<Text fw={700} c="gray.0" lineClamp={2}> | ||
{i.todo} | ||
</Text> | ||
</Group> | ||
<ActionIcon color="gray.0" variant="transparent"> | ||
<IconStarFilled /> | ||
</ActionIcon> | ||
</Group> | ||
<Group display={i?.list || i?.date ? 'flex' : 'none'}> | ||
<Badge | ||
c={i?.list?.color || 'dark'} | ||
leftSection={<IconList size={14} />} | ||
radius="xs" | ||
variant="white" | ||
display={i?.list ? 'block' : 'none'} | ||
> | ||
{i?.list?.title} | ||
</Badge> | ||
<Badge | ||
c={i?.list?.color || 'dark'} | ||
leftSection={<IconCalendar size={14} />} | ||
radius="xs" | ||
variant="white" | ||
display={i?.date ? 'block' : 'none'} | ||
> | ||
{dayjs(i?.date).format('DD MMM YYYY')} | ||
</Badge> | ||
</Group> | ||
</Stack> | ||
</Paper> | ||
))} | ||
</Stack> | ||
); | ||
|
||
export default Todos; |
Oops, something went wrong.