Skip to content

Commit

Permalink
chore: hide sidebar (0 items) | axios call function
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalkondle-dev committed Jun 24, 2024
1 parent c32f4d5 commit cbba101
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 142 deletions.
23 changes: 4 additions & 19 deletions app/(private)/notes/archive/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import { Container, SimpleGrid } from '@mantine/core';
import { useForm } from '@mantine/form';
import { useDisclosure } from '@mantine/hooks';
import { nprogress } from '@mantine/nprogress';
import axios from 'axios';
import { useEffect } from 'react';
import Note, { NewNote, NoteModal } from '@/components/Note';
import useFetchData from '@/hooks/useFetchData';
import { failure } from '@/lib/client_functions';
import { apiCall, failure } from '@/lib/client_functions';
import { NoteDocument } from '@/models/Note';

export default function NotesPage() {
const { data, loading, refetch } = useFetchData('/api/notes/archived');
const { data, refetch } = useFetchData('/api/notes/archived');
const [opened, { close, open }] = useDisclosure(false);

const form = useForm({
Expand All @@ -27,14 +24,6 @@ export default function NotesPage() {
},
});

useEffect(() => {
if (loading) {
nprogress.start();
} else {
nprogress.complete();
}
}, [loading]);

const handleClick = (note: NoteDocument) => {
open();
form.setValues({
Expand All @@ -57,8 +46,7 @@ export default function NotesPage() {
const createNote = async (note: any) => {
if (form.values.title || form.values.note) {
const { _id, ...remainingNote } = note;
await axios
.post('/api/notes', { ...remainingNote })
await apiCall('/api/notes', remainingNote, 'POST')
.then(() => {
refetch();
})
Expand All @@ -69,8 +57,7 @@ export default function NotesPage() {
};

const updateNote = async (note: any) => {
await axios
.put('/api/notes', { ...note })
await apiCall('/api/notes', note, 'POST')
.then(() => {
refetch();
})
Expand All @@ -80,7 +67,6 @@ export default function NotesPage() {
};

const onSave = async (note: any) => {
nprogress.start();
if (note.title || note.note) {
if (note._id) {
const _note = data?.find((n: any) => n._id === note._id);
Expand Down Expand Up @@ -109,7 +95,6 @@ export default function NotesPage() {
await createNote(note);
}
}
nprogress.complete();
form.reset();
close();
};
Expand Down
25 changes: 5 additions & 20 deletions app/(private)/notes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import { useForm } from '@mantine/form';
import { Container, SimpleGrid, Stack, Text } from '@mantine/core';
import { useDisclosure } from '@mantine/hooks';
import { nprogress } from '@mantine/nprogress';
import axios from 'axios';
import { useEffect } from 'react';
import Note, { NewNote, NoteModal } from '@/components/Note';
import useFetchData from '@/hooks/useFetchData';
import { failure } from '@/lib/client_functions';
import { apiCall, failure } from '@/lib/client_functions';
import { NoteDocument } from '@/models/Note';
import Skelton from '@/components/Skelton/Skelton';

Expand All @@ -27,15 +24,6 @@ export default function NotesPage() {
isTrashed: false,
},
});

useEffect(() => {
if (loading) {
nprogress.start();
} else {
nprogress.complete();
}
}, [loading]);

const handleClick = (note: NoteDocument) => {
open();
form.setValues({
Expand All @@ -56,10 +44,9 @@ export default function NotesPage() {
};

const createNote = async (note: any) => {
if (form.values.title || form.values.note) {
if (note.title || note.note) {
const { _id, ...remainingNote } = note;
await axios
.post('/api/notes', { ...remainingNote })
await apiCall('/api/notes', remainingNote, 'POST')
.then(() => {
refetch();
})
Expand All @@ -70,8 +57,7 @@ export default function NotesPage() {
};

const updateNote = async (note: any) => {
await axios
.put('/api/notes', { ...note })
await apiCall('/api/notes', note, 'PUT')
.then(() => {
refetch();
})
Expand All @@ -81,7 +67,6 @@ export default function NotesPage() {
};

const onSave = async (note: any) => {
nprogress.start();
if (note.title || note.note) {
if (note._id) {
const _note = data?.find((n: any) => n._id === note._id);
Expand All @@ -104,13 +89,13 @@ export default function NotesPage() {
isPinned: _note.isPinned,
});
if (formValuesString !== noteString) {
console.log(formValuesString, noteString);
await updateNote(note);
}
} else {
await createNote(note);
}
}
nprogress.complete();
form.reset();
close();
};
Expand Down
28 changes: 5 additions & 23 deletions app/(private)/notes/trashed/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import { Container, SimpleGrid } from '@mantine/core';
import { useForm } from '@mantine/form';
import { useDisclosure } from '@mantine/hooks';
import { nprogress } from '@mantine/nprogress';
import axios from 'axios';
import { useEffect } from 'react';
import Note, { NewNote, NoteModal } from '@/components/Note';
import useFetchData from '@/hooks/useFetchData';
import { failure } from '@/lib/client_functions';
import { apiCall, failure } from '@/lib/client_functions';
import { NoteDocument } from '@/models/Note';

export default function NotesPage() {
const { data, loading, refetch } = useFetchData('/api/notes/trashed');
const { data, refetch } = useFetchData('/api/notes/trashed');
const [opened, { close, open }] = useDisclosure(false);

const form = useForm({
Expand All @@ -27,14 +24,6 @@ export default function NotesPage() {
},
});

useEffect(() => {
if (loading) {
nprogress.start();
} else {
nprogress.complete();
}
}, [loading]);

const handleClick = (note: NoteDocument) => {
open();
form.setValues({
Expand All @@ -57,8 +46,7 @@ export default function NotesPage() {
const createNote = async (note: any) => {
if (form.values.title || form.values.note) {
const { _id, ...remainingNote } = note;
await axios
.post('/api/notes', { ...remainingNote })
await apiCall('/api/notes', remainingNote, 'POST')
.then(() => {
refetch();
})
Expand All @@ -69,8 +57,7 @@ export default function NotesPage() {
};

const updateNote = async (note: any) => {
await axios
.put('/api/notes', { ...note })
await apiCall('/api/notes', note, 'PUT')
.then(() => {
refetch();
})
Expand All @@ -80,7 +67,6 @@ export default function NotesPage() {
};

const onSave = async (note: any) => {
nprogress.start();
if (note.title || note.note) {
if (note._id) {
const _note = data?.find((n: any) => n._id === note._id);
Expand Down Expand Up @@ -109,22 +95,18 @@ export default function NotesPage() {
await createNote(note);
}
}
nprogress.complete();
form.reset();
close();
};

const onDelete = async (_id: string) => {
nprogress.start();
await axios
.delete(`/api/notes?_id=${_id}`)
await apiCall(`/api/notes?_id=${_id}`, {}, 'DELETE')
.then(() => {
refetch();
})
.catch((err) => {
failure(err.response.data.error);
});
nprogress.complete();
form.reset();
close();
};
Expand Down
10 changes: 4 additions & 6 deletions app/(private)/todos/[_id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ import {
IconCircleCheck,
IconTrash,
} from '@tabler/icons-react';
import axios from 'axios';
import { useEffect, useState } from 'react';
import { TodoPageActions } from '@/components/Todo';
import TodoSkelton from '@/components/Todo/TodoSkelton';
import useFetchData from '@/hooks/useFetchData';
import { TodoType } from '@/models/Todo';
import Todo from '@/components/Todo/Todo';
import { COLORS, STYLES } from '@/lib/constants';
import { failure, openModal } from '@/lib/client_functions';
import { apiCall, failure, 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 @@ -60,15 +59,15 @@ const TodosPage = ({ params }: { params: { _id: string } }) => {

const onSubmit = async () => {
const { _id, todo, list, date, color } = form.values;
await axios.put('/api/todos', { _id, todo, list: list || null, date, color }).then(() => {
await apiCall('/api/todos', { _id, todo, list: list || null, date, color }, 'PUT').then(() => {
form.reset();
refetch();
});
};

const getTodoLists = async () => {
try {
const res = await axios.get('/api/todos/todo-list');
const res = await apiCall('/api/todos/todo-list');
setTodoList(res?.data);
} catch (error) {
failure('Something went wrong');
Expand All @@ -77,8 +76,7 @@ const TodosPage = ({ params }: { params: { _id: string } }) => {

const onDelete = () => {
openModal(() => {
axios
.delete(`/api/todos?_id=${form.values._id}`)
apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE')
.then(() => {
form.reset();
refetch();
Expand Down
10 changes: 4 additions & 6 deletions app/(private)/todos/important/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ import {
IconCircleCheck,
IconTrash,
} from '@tabler/icons-react';
import axios from 'axios';
import { useEffect, useState } from 'react';
import { TodoPageActions } from '@/components/Todo';
import TodoSkelton from '@/components/Todo/TodoSkelton';
import useFetchData from '@/hooks/useFetchData';
import { TodoType } from '@/models/Todo';
import Todo from '@/components/Todo/Todo';
import { COLORS, STYLES } from '@/lib/constants';
import { failure, openModal } from '@/lib/client_functions';
import { apiCall, failure, openModal } from '@/lib/client_functions';

const TodosPage = () => {
const { data, refetch, loading } = useFetchData('/api/todos?type=important');
Expand Down Expand Up @@ -60,15 +59,15 @@ const TodosPage = () => {

const onSubmit = async () => {
const { _id, todo, list, date, color } = form.values;
await axios.put('/api/todos', { _id, todo, list: list || null, date, color }).then(() => {
await apiCall('/api/todos', { _id, todo, list: list || null, date, color }, 'PUT').then(() => {
form.reset();
refetch();
});
};

const getTodoLists = async () => {
try {
const res = await axios.get('/api/todos/todo-list');
const res = await apiCall('/api/todos/todo-list');
setTodoList(res?.data);
} catch (error) {
failure('Something went wrong');
Expand All @@ -77,8 +76,7 @@ const TodosPage = () => {

const onDelete = () => {
openModal(() => {
axios
.delete(`/api/todos?_id=${form.values._id}`)
apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE')
.then(() => {
form.reset();
refetch();
Expand Down
13 changes: 6 additions & 7 deletions app/(private)/todos/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ import {
IconCircleCheck,
IconTrash,
} from '@tabler/icons-react';
import axios from 'axios';
import { useEffect, useState } from 'react';
import { TodoPageActions } from '@/components/Todo';
import TodoSkelton from '@/components/Todo/TodoSkelton';
import useFetchData from '@/hooks/useFetchData';
import { TodoType } from '@/models/Todo';
import Todo from '@/components/Todo/Todo';
import { COLORS, STYLES } from '@/lib/constants';
import { failure, openModal } from '@/lib/client_functions';
import { apiCall, failure, openModal } from '@/lib/client_functions';

const TodosPage = () => {
const { data, refetch, loading } = useFetchData('/api/todos');
Expand Down Expand Up @@ -60,25 +59,25 @@ const TodosPage = () => {

const onSubmit = async () => {
const { _id, todo, list, date, color } = form.values;
await axios.put('/api/todos', { _id, todo, list: list || null, date, color }).then(() => {
await apiCall('/api/todos', { _id, todo, list: list || null, date, color }, 'PUT').then(() => {
form.reset();
refetch();
});
};

const getTodoLists = async () => {
try {
const res = await axios.get('/api/todos/todo-list');
setTodoList(res?.data);
await apiCall('/api/todos/todo-list').then((res) => {
setTodoList(res?.data);
});
} catch (error) {
failure('Something went wrong');
}
};

const onDelete = () => {
openModal(() => {
axios
.delete(`/api/todos?_id=${form.values._id}`)
apiCall(`/api/todos?_id=${form.values._id}`, {}, 'DELETE')
.then(() => {
form.reset();
refetch();
Expand Down
Loading

0 comments on commit cbba101

Please sign in to comment.