Skip to content

Commit

Permalink
chore: generalized error notification | dynamic message for openModal…
Browse files Browse the repository at this point in the history
… on delete
  • Loading branch information
vishalkondle-dev committed Jun 24, 2024
1 parent 4f69d6d commit a6228cd
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 262 deletions.
5 changes: 4 additions & 1 deletion app/(private)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}</>;
}
}
22 changes: 7 additions & 15 deletions app/(private)/notes/archive/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) => {
Expand Down
22 changes: 7 additions & 15 deletions app/(private)/notes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) => {
Expand Down
32 changes: 10 additions & 22 deletions app/(private)/notes/trashed/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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();
};
Expand Down
24 changes: 8 additions & 16 deletions app/(private)/todos/[_id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down Expand Up @@ -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();
});
});
};

Expand Down
24 changes: 8 additions & 16 deletions app/(private)/todos/important/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
});
});
};

Expand Down
25 changes: 8 additions & 17 deletions app/(private)/todos/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
});
});
};

Expand Down
24 changes: 8 additions & 16 deletions app/(private)/todos/recent/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
});
});
};

Expand Down
24 changes: 8 additions & 16 deletions app/(private)/todos/today/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
});
});
};

Expand Down
24 changes: 8 additions & 16 deletions app/(private)/todos/upcoming/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
});
});
};

Expand Down
Loading

0 comments on commit a6228cd

Please sign in to comment.