diff --git a/app/(private)/layout.tsx b/app/(private)/layout.tsx index f2e611b..f32f268 100644 --- a/app/(private)/layout.tsx +++ b/app/(private)/layout.tsx @@ -10,5 +10,8 @@ interface Props { export default async function RootLayout({ children }: Props) { const session: UserDataTypes | null = await getServerSession(authOptions); if (!session?.user) redirect('/auth/login'); - return <>{session?.user && children}; + + if (session.user) { + return <>{children}; + } } diff --git a/app/(private)/notes/archive/page.tsx b/app/(private)/notes/archive/page.tsx index 71f89e2..4b2f426 100644 --- a/app/(private)/notes/archive/page.tsx +++ b/app/(private)/notes/archive/page.tsx @@ -5,7 +5,7 @@ import { useForm } from '@mantine/form'; import { useDisclosure } from '@mantine/hooks'; import Note, { NewNote, NoteModal } from '@/components/Note'; import useFetchData from '@/hooks/useFetchData'; -import { apiCall, failure } from '@/lib/client_functions'; +import { apiCall } from '@/lib/client_functions'; import { NoteDocument } from '@/models/Note'; export default function NotesPage() { @@ -46,24 +46,16 @@ export default function NotesPage() { const createNote = async (note: any) => { if (form.values.title || form.values.note) { const { _id, ...remainingNote } = note; - await apiCall('/api/notes', remainingNote, 'POST') - .then(() => { - refetch(); - }) - .catch((err) => { - failure(err.response.data.error); - }); + await apiCall('/api/notes', remainingNote, 'POST').then(() => { + refetch(); + }); } }; const updateNote = async (note: any) => { - await apiCall('/api/notes', note, 'POST') - .then(() => { - refetch(); - }) - .catch((err) => { - failure(err.response.data.error); - }); + await apiCall('/api/notes', note, 'POST').then(() => { + refetch(); + }); }; const onSave = async (note: any) => { diff --git a/app/(private)/notes/page.tsx b/app/(private)/notes/page.tsx index e3d0c56..87c5b57 100644 --- a/app/(private)/notes/page.tsx +++ b/app/(private)/notes/page.tsx @@ -5,7 +5,7 @@ import { Container, SimpleGrid, Stack, Text } from '@mantine/core'; import { useDisclosure } from '@mantine/hooks'; import Note, { NewNote, NoteModal } from '@/components/Note'; import useFetchData from '@/hooks/useFetchData'; -import { apiCall, failure } from '@/lib/client_functions'; +import { apiCall } from '@/lib/client_functions'; import { NoteDocument } from '@/models/Note'; import Skelton from '@/components/Skelton/Skelton'; @@ -46,24 +46,16 @@ export default function NotesPage() { const createNote = async (note: any) => { if (note.title || note.note) { const { _id, ...remainingNote } = note; - await apiCall('/api/notes', remainingNote, 'POST') - .then(() => { - refetch(); - }) - .catch((err) => { - failure(err.response.data.error); - }); + await apiCall('/api/notes', remainingNote, 'POST').then(() => { + refetch(); + }); } }; const updateNote = async (note: any) => { - await apiCall('/api/notes', note, 'PUT') - .then(() => { - refetch(); - }) - .catch((err) => { - failure(err.response.data.error); - }); + await apiCall('/api/notes', note, 'PUT').then(() => { + refetch(); + }); }; const onSave = async (note: any) => { diff --git a/app/(private)/notes/trashed/page.tsx b/app/(private)/notes/trashed/page.tsx index 888a984..408279d 100644 --- a/app/(private)/notes/trashed/page.tsx +++ b/app/(private)/notes/trashed/page.tsx @@ -5,7 +5,7 @@ import { useForm } from '@mantine/form'; import { useDisclosure } from '@mantine/hooks'; import Note, { NewNote, NoteModal } from '@/components/Note'; import useFetchData from '@/hooks/useFetchData'; -import { apiCall, failure } from '@/lib/client_functions'; +import { apiCall } from '@/lib/client_functions'; import { NoteDocument } from '@/models/Note'; export default function NotesPage() { @@ -46,24 +46,16 @@ export default function NotesPage() { const createNote = async (note: any) => { if (form.values.title || form.values.note) { const { _id, ...remainingNote } = note; - await apiCall('/api/notes', remainingNote, 'POST') - .then(() => { - refetch(); - }) - .catch((err) => { - failure(err.response.data.error); - }); + await apiCall('/api/notes', remainingNote, 'POST').then(() => { + refetch(); + }); } }; const updateNote = async (note: any) => { - await apiCall('/api/notes', note, 'PUT') - .then(() => { - refetch(); - }) - .catch((err) => { - failure(err.response.data.error); - }); + await apiCall('/api/notes', note, 'PUT').then(() => { + refetch(); + }); }; const onSave = async (note: any) => { @@ -100,13 +92,9 @@ export default function NotesPage() { }; const onDelete = async (_id: string) => { - await apiCall(`/api/notes?_id=${_id}`, {}, 'DELETE') - .then(() => { - refetch(); - }) - .catch((err) => { - failure(err.response.data.error); - }); + await apiCall(`/api/notes?_id=${_id}`, {}, 'DELETE').then(() => { + refetch(); + }); form.reset(); close(); }; diff --git a/app/(private)/todos/[_id]/page.tsx b/app/(private)/todos/[_id]/page.tsx index 086009b..d533057 100644 --- a/app/(private)/todos/[_id]/page.tsx +++ b/app/(private)/todos/[_id]/page.tsx @@ -28,7 +28,7 @@ import useFetchData from '@/hooks/useFetchData'; import { TodoType } from '@/models/Todo'; import Todo from '@/components/Todo/Todo'; import { COLORS, STYLES } from '@/lib/constants'; -import { apiCall, failure, openModal } from '@/lib/client_functions'; +import { apiCall, openModal } from '@/lib/client_functions'; const TodosPage = ({ params }: { params: { _id: string } }) => { const { data, refetch, loading } = useFetchData(`/api/todos?type=list&list=${params._id}`); @@ -66,24 +66,16 @@ const TodosPage = ({ params }: { params: { _id: string } }) => { }; const getTodoLists = async () => { - try { - const res = await apiCall('/api/todos/todo-list'); - setTodoList(res?.data); - } catch (error) { - failure('Something went wrong'); - } + const res = await apiCall('/api/todos/todo-list'); + setTodoList(res?.data); }; const onDelete = () => { - openModal(() => { - apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE') - .then(() => { - form.reset(); - refetch(); - }) - .catch((err) => { - failure(err.response.data.error || 'Something went wrong'); - }); + openModal('This todo will be deleted permanently', () => { + apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE').then(() => { + form.reset(); + refetch(); + }); }); }; diff --git a/app/(private)/todos/important/page.tsx b/app/(private)/todos/important/page.tsx index 48c494a..2df6335 100644 --- a/app/(private)/todos/important/page.tsx +++ b/app/(private)/todos/important/page.tsx @@ -28,7 +28,7 @@ import useFetchData from '@/hooks/useFetchData'; import { TodoType } from '@/models/Todo'; import Todo from '@/components/Todo/Todo'; import { COLORS, STYLES } from '@/lib/constants'; -import { apiCall, failure, openModal } from '@/lib/client_functions'; +import { apiCall, openModal } from '@/lib/client_functions'; const TodosPage = () => { const { data, refetch, loading } = useFetchData('/api/todos?type=important'); @@ -66,24 +66,16 @@ const TodosPage = () => { }; const getTodoLists = async () => { - try { - const res = await apiCall('/api/todos/todo-list'); - setTodoList(res?.data); - } catch (error) { - failure('Something went wrong'); - } + const res = await apiCall('/api/todos/todo-list'); + setTodoList(res?.data); }; const onDelete = () => { - openModal(() => { - apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE') - .then(() => { - form.reset(); - refetch(); - }) - .catch((err) => { - failure(err.response.data.error || 'Something went wrong'); - }); + openModal('This todo will be deleted permanently', () => { + apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE').then(() => { + form.reset(); + refetch(); + }); }); }; diff --git a/app/(private)/todos/page.tsx b/app/(private)/todos/page.tsx index 2e0c299..c22c7e6 100644 --- a/app/(private)/todos/page.tsx +++ b/app/(private)/todos/page.tsx @@ -28,7 +28,7 @@ import useFetchData from '@/hooks/useFetchData'; import { TodoType } from '@/models/Todo'; import Todo from '@/components/Todo/Todo'; import { COLORS, STYLES } from '@/lib/constants'; -import { apiCall, failure, openModal } from '@/lib/client_functions'; +import { apiCall, openModal } from '@/lib/client_functions'; const TodosPage = () => { const { data, refetch, loading } = useFetchData('/api/todos'); @@ -66,25 +66,16 @@ const TodosPage = () => { }; const getTodoLists = async () => { - try { - await apiCall('/api/todos/todo-list').then((res) => { - setTodoList(res?.data); - }); - } catch (error) { - failure('Something went wrong'); - } + const res = await apiCall('/api/todos/todo-list'); + setTodoList(res?.data); }; const onDelete = () => { - openModal(() => { - apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE') - .then(() => { - form.reset(); - refetch(); - }) - .catch((err) => { - failure(err.response.data.error || 'Something went wrong'); - }); + openModal('This todo will be deleted permanently', () => { + apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE').then(() => { + form.reset(); + refetch(); + }); }); }; diff --git a/app/(private)/todos/recent/page.tsx b/app/(private)/todos/recent/page.tsx index e055137..26ce4fa 100644 --- a/app/(private)/todos/recent/page.tsx +++ b/app/(private)/todos/recent/page.tsx @@ -28,7 +28,7 @@ import useFetchData from '@/hooks/useFetchData'; import { TodoType } from '@/models/Todo'; import Todo from '@/components/Todo/Todo'; import { COLORS, STYLES } from '@/lib/constants'; -import { apiCall, failure, openModal } from '@/lib/client_functions'; +import { apiCall, openModal } from '@/lib/client_functions'; const TodosPage = () => { const { data, refetch, loading } = useFetchData('/api/todos?type=recent'); @@ -66,24 +66,16 @@ const TodosPage = () => { }; const getTodoLists = async () => { - try { - const res = await apiCall('/api/todos/todo-list'); - setTodoList(res?.data); - } catch (error) { - failure('Something went wrong'); - } + const res = await apiCall('/api/todos/todo-list'); + setTodoList(res?.data); }; const onDelete = () => { - openModal(() => { - apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE') - .then(() => { - form.reset(); - refetch(); - }) - .catch((err) => { - failure(err.response.data.error || 'Something went wrong'); - }); + openModal('This todo will be deleted permanently', () => { + apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE').then(() => { + form.reset(); + refetch(); + }); }); }; diff --git a/app/(private)/todos/today/page.tsx b/app/(private)/todos/today/page.tsx index 6369223..3842573 100644 --- a/app/(private)/todos/today/page.tsx +++ b/app/(private)/todos/today/page.tsx @@ -28,7 +28,7 @@ import useFetchData from '@/hooks/useFetchData'; import { TodoType } from '@/models/Todo'; import Todo from '@/components/Todo/Todo'; import { COLORS, STYLES } from '@/lib/constants'; -import { apiCall, failure, openModal } from '@/lib/client_functions'; +import { apiCall, openModal } from '@/lib/client_functions'; const TodosPage = () => { const { data, refetch, loading } = useFetchData('/api/todos?type=today'); @@ -66,24 +66,16 @@ const TodosPage = () => { }; const getTodoLists = async () => { - try { - const res = await apiCall('/api/todos/todo-list'); - setTodoList(res?.data); - } catch (error) { - failure('Something went wrong'); - } + const res = await apiCall('/api/todos/todo-list'); + setTodoList(res?.data); }; const onDelete = () => { - openModal(() => { - apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE') - .then(() => { - form.reset(); - refetch(); - }) - .catch((err) => { - failure(err.response.data.error || 'Something went wrong'); - }); + openModal('This todo will be deleted permanently', () => { + apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE').then(() => { + form.reset(); + refetch(); + }); }); }; diff --git a/app/(private)/todos/upcoming/page.tsx b/app/(private)/todos/upcoming/page.tsx index 4bb7d61..10e598c 100644 --- a/app/(private)/todos/upcoming/page.tsx +++ b/app/(private)/todos/upcoming/page.tsx @@ -28,7 +28,7 @@ import useFetchData from '@/hooks/useFetchData'; import { TodoType } from '@/models/Todo'; import Todo from '@/components/Todo/Todo'; import { COLORS, STYLES } from '@/lib/constants'; -import { apiCall, failure, openModal } from '@/lib/client_functions'; +import { apiCall, openModal } from '@/lib/client_functions'; const TodosPage = () => { const { data, refetch, loading } = useFetchData('/api/todos?type=upcoming'); @@ -66,24 +66,16 @@ const TodosPage = () => { }; const getTodoLists = async () => { - try { - const res = await apiCall('/api/todos/todo-list'); - setTodoList(res?.data); - } catch (error) { - failure('Something went wrong'); - } + const res = await apiCall('/api/todos/todo-list'); + setTodoList(res?.data); }; const onDelete = () => { - openModal(() => { - apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE') - .then(() => { - form.reset(); - refetch(); - }) - .catch((err) => { - failure(err.response.data.error || 'Something went wrong'); - }); + openModal('This todo will be deleted permanently', () => { + apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE').then(() => { + form.reset(); + refetch(); + }); }); }; diff --git a/app/api/list/route.ts b/app/api/list/route.ts index 2d7cf70..0672ee7 100644 --- a/app/api/list/route.ts +++ b/app/api/list/route.ts @@ -22,7 +22,7 @@ export async function GET(req: NextRequest) { break; } return NextResponse.json( - list.map((i) => ({ path: `/${schema}/${i?._id}`, label: i?.title, color: i?.color })), + list?.map((i) => ({ path: `/${schema}/${i?._id}`, label: i?.title, color: i?.color })), { status: 200 } ); } catch (error: any) { diff --git a/app/api/todos/todo-list/route.ts b/app/api/todos/todo-list/route.ts index 187765b..eccd007 100644 --- a/app/api/todos/todo-list/route.ts +++ b/app/api/todos/todo-list/route.ts @@ -4,6 +4,7 @@ import startDb from '@/lib/db'; import TodoList from '@/models/TodoList'; import { authOptions } from '../../auth/[...nextauth]/authOptions'; import { UserDataTypes } from '../../auth/[...nextauth]/next-auth.interfaces'; +import Todo from '@/models/Todo'; export async function GET() { try { @@ -48,6 +49,7 @@ export async function DELETE(req: NextRequest) { } await startDb(); await TodoList.findByIdAndDelete(req.nextUrl.searchParams.get('_id')); + await Todo.deleteMany({ list: req.nextUrl.searchParams.get('_id') }); return NextResponse.json(null, { status: 200 }); } catch (error: any) { return NextResponse.json({ error: error?.message }, { status: 500 }); diff --git a/app/auth/register/page.tsx b/app/auth/register/page.tsx index 288bd71..5522538 100644 --- a/app/auth/register/page.tsx +++ b/app/auth/register/page.tsx @@ -13,7 +13,7 @@ import { useForm } from '@mantine/form'; import { useSession } from 'next-auth/react'; import { useRouter } from 'next/navigation'; import FormButtons from '@/components/FormButtons'; -import { apiCall, failure, success } from '@/lib/client_functions'; +import { apiCall, success } from '@/lib/client_functions'; const Register = () => { const router = useRouter(); @@ -36,12 +36,10 @@ const Register = () => { }); const sumbitRegister = async () => { - await apiCall('/api/users/register', form.values, 'POST') - .then(() => { - success('User registered successfully'); - router.push('/auth/login'); - }) - .catch((error) => failure(error.response.data.error)); + await apiCall('/api/users/register', form.values, 'POST').then(() => { + success('User registered successfully'); + router.push('/auth/login'); + }); }; if (session.status === 'authenticated') { diff --git a/components/Layout/Layout.tsx b/components/Layout/Layout.tsx index 4f486b3..c80eb2c 100644 --- a/components/Layout/Layout.tsx +++ b/components/Layout/Layout.tsx @@ -3,39 +3,40 @@ import { ActionIcon, AppShell, + Avatar, Burger, Button, - Container, Group, Indicator, Popover, rem, SimpleGrid, Stack, + Text, } from '@mantine/core'; import { useDisclosure, useNetwork } from '@mantine/hooks'; import { notifications } from '@mantine/notifications'; -import { IconGridDots, IconList } from '@tabler/icons-react'; +import { IconGridDots, IconList, IconLogout, IconUser } from '@tabler/icons-react'; import { signOut, useSession } from 'next-auth/react'; import { usePathname, useRouter } from 'next/navigation'; import React, { useCallback, useEffect, useState } from 'react'; import { App } from '../App'; import { APPS } from '@/lib/constants'; -import { apiCall, failure } from '@/lib/client_functions'; +import { apiCall, getInitials } from '@/lib/client_functions'; export default function Layout({ children }: { children: React.ReactNode }) { + const session = useSession(); + const isLoggedIn = session?.status === 'authenticated'; + const router = useRouter(); + const network = useNetwork(); const [mobileOpened, { toggle: toggleMobile, close }] = useDisclosure(); const [desktopOpened, { toggle: toggleDesktop }] = useDisclosure(true); const [opened, setOpened] = useState(false); - const session = useSession(); - const loading = session?.status === 'loading'; - const isLoggedIn = session?.status === 'authenticated'; - const isLoggedOff = session?.status === 'unauthenticated'; - const router = useRouter(); + const [opened1, setOpened1] = useState(false); const pathname = usePathname(); const rootpath = pathname.split('/')[1]; - const [APP, setAPP] = useState(APPS.find((app) => `/${rootpath}` === app?.path)); + const [APP, setAPP] = useState(APPS?.find((app) => `/${rootpath}` === app?.path)); const navigateTo = useCallback( (path?: string) => { @@ -50,21 +51,16 @@ export default function Layout({ children }: { children: React.ReactNode }) { ); const getList = async () => { - try { - setAPP(APPS.find((app) => `/${rootpath}` === app?.path)); - const res = await apiCall(`/api/list?schema=${rootpath}`); - if (res?.data) { - setAPP((old = { sidebar: [] }) => ({ ...old, sidebar: [...old.sidebar, ...res.data] })); - } - } catch (error) { - failure('Something went wrong'); + if (session.status === 'unauthenticated') { + return; + } + setAPP(APPS?.find((app) => `/${rootpath}` === app?.path)); + const res = await apiCall(`/api/list?schema=${rootpath}`); + if (res?.data) { + setAPP((old = { sidebar: [] }) => ({ ...old, sidebar: [...old.sidebar, ...res.data] })); } }; - if (isLoggedOff) { - router.push('/auth/login'); - } - useEffect(() => { getList(); }, [pathname]); @@ -85,15 +81,11 @@ export default function Layout({ children }: { children: React.ReactNode }) { } }, [network.online]); - if (loading) { - return <>; - } - return ( {isLoggedIn && ( - <> + - - setOpened((o) => !o)} - > - - - + setOpened((o) => !o)} + > + + - {APPS.map((app) => ( + {APPS?.map((app) => ( - - + + + setOpened1((o) => !o)} + style={{ cursor: 'pointer' }} + > + {getInitials(session?.data?.user?.name)} + + + + + + + {session?.data?.user?.email} + + + + + {getInitials(session?.data?.user?.name)} + + + + {network.online ? 'Online' : 'Offline'} + + + + Hi {session?.data?.user?.name}, + + + + + + + + + )} - {isLoggedOff && ( + {!isLoggedIn && ( <>