Skip to content

Commit 5b6e056

Browse files
author
Manish Kumar Yadav
committed
improve api hit
1 parent e7b4a6e commit 5b6e056

File tree

9 files changed

+71
-52
lines changed

9 files changed

+71
-52
lines changed

client/src/components/Modals/AddEditModal.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const AddEditModal = ({tab, isDialogOpen, setIsDialogOpen, data, type = 'add', o
3535
if (data?._id === undefined) {
3636
await onSave(noteData);
3737
} else {
38-
await onSave(data?._id, noteData);
38+
await onSave(data, noteData);
3939
}
4040
};
4141

client/src/components/Modals/DetailModal.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ const DetailModal = ({ sidebarItem = false, data, isDialogOpen, setIsDialogOpen,
2929

3030
const handleStatusClick = () => {
3131
if (sidebarItem) {
32-
handleStatus(data?._id, data?.status)
32+
handleStatus(data)
3333
setIsDialogOpen(false)
3434
} else {
35-
handleStatus(data?._id, data?.status)
35+
handleStatus(data)
3636
}
3737
}
3838

client/src/components/notes/NoteCard.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import DetailModal from '../Modals/DetailModal';
1010
const NoteCard = ({
1111
note,
1212
}) => {
13-
13+
1414
const [isDialogOpen, setIsDialogOpen] = useState(false);
1515
const { pinNote, removeNote, editNote } = useNote();
1616

@@ -23,7 +23,7 @@ const NoteCard = ({
2323
<Card className="h-full transition-all ease-in-out hover:shadow-2xl cursor-pointer">
2424
<CardHeader>
2525
<div className="flex justify-between gap-2 mt-2 sm:mt-0">
26-
<Button variant="ghost" className="max-w-6" onClick={()=>pinNote(note?._id)}>
26+
<Button variant="ghost" className="max-w-6" onClick={() => pinNote(note?._id)}>
2727
<Pin className={`h-5 w-5 ${note?.isPinned ? 'text-indigo-700' : ''}`} />
2828
</Button>
2929
<h6 className="text-lg font-bold w-full text-center" onClick={handledetail} >{note?.title}</h6>

client/src/components/notes/NoteContainer.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { useNote } from '@/context/NoteProvider';
1111
const NoteContainer = ({ add = true }) => {
1212

1313
const { toast } = useToast();
14-
const { notes, getNotes, addNote } = useNote();
14+
const { notes, addNote } = useNote();
1515
const [isDialogOpen, setIsDialogOpen] = useState(false);
1616
const note = {
1717
id: null,
@@ -34,7 +34,6 @@ const NoteContainer = ({ add = true }) => {
3434
<div key={index}>
3535
<NoteCard
3636
note={note}
37-
callbackFunction={getNotes}
3837
toast={toast}
3938
/>
4039
</div>

client/src/components/tasks/TaskLists.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const TaskLists = ({
3434
</TableCell>
3535
<TableCell
3636
className="[&:has([role=checkbox])]:pr-2 w-[70px] select-none"
37-
onClick={() => taskOperations?.status(task?._id, task?.status)}
37+
onClick={() => taskOperations?.status(task)}
3838
>
3939
<div className={`flex items-center justify-end ${task.status ? 'text-green-500' : 'text-yellow-700'}`}>
4040
{task.status ? 'done' : 'pending'}

client/src/context/NoteProvider.jsx

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export const NoteProvider = ({ children }) => {
1212
const { searchText } = usePage();
1313

1414
const getNotes = async (query = searchText) => {
15-
const response = await fetchNotesService(query);
16-
if (response?.status === 200) {
17-
setNotes(response?.data);
15+
const {status,data} = await fetchNotesService(query);
16+
if (status === 200) {
17+
setNotes(data);
1818
} else {
1919
toast({
2020
variant: "destructive",
@@ -26,9 +26,9 @@ export const NoteProvider = ({ children }) => {
2626
}
2727

2828
const addNote = async (noteData) => {
29-
const response = await addNotesService(noteData)
30-
if (response.status === 201) {
31-
getNotes();
29+
const {status,data} = await addNotesService(noteData)
30+
if (status === 201) {
31+
setNotes((prev) => [...prev, data])
3232
toast({
3333
description: "Note added successfully!",
3434
});
@@ -45,10 +45,14 @@ export const NoteProvider = ({ children }) => {
4545
getNotes()
4646
}, [])
4747

48-
const editNote = async (id, noteData) => {
49-
const response = await updateNotesService(id, noteData);
50-
if (response?.data) {
51-
getNotes();
48+
const editNote = async (originalNote, updatedNote) => {
49+
const { status, data } = await updateNotesService(originalNote?._id, updatedNote);
50+
if (status === 200) {
51+
setNotes((prev) =>
52+
prev?.map((note) =>
53+
note?._id === data?._id ? data : note
54+
)
55+
)
5256
toast({
5357
description: "Note edited Successfully!!",
5458
});
@@ -61,11 +65,10 @@ export const NoteProvider = ({ children }) => {
6165
}
6266
}
6367

64-
6568
const removeNote = async (id) => {
66-
const response = await removeNotesService(id);
67-
if (response?.data) {
68-
getNotes();
69+
const {status} = await removeNotesService(id);
70+
if (status===200) {
71+
setNotes((prevNotes) => prevNotes.filter((note) => note._id !== id));
6972
toast({
7073
description: "Note removed Successfully!",
7174
});
@@ -80,11 +83,16 @@ export const NoteProvider = ({ children }) => {
8083

8184
const pinNote = async (id) => {
8285

83-
const response = await pinNotesService(id);
84-
if (response?.data) {
85-
getNotes();
86+
const {status,data} = await pinNotesService(id);
87+
if (status===200) {
88+
// getNotes();
89+
setNotes((prev) =>
90+
prev?.map((note) =>
91+
note?._id === data?._id ? data : note
92+
)
93+
)
8694
toast({
87-
description: `Note ${response?.data?.isPinned ? 'pinned' : 'unpinned'} Successfully!`,
95+
description: `Note ${data?.isPinned ? 'pinned' : 'unpinned'} Successfully!`,
8896
});
8997
} else {
9098
toast({

client/src/context/TaskProvider.jsx

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export const TaskProvider = ({ children }) => {
1212
const {searchText} = usePage();
1313

1414
const getTasks = async (query = searchText) => {
15-
const response = await fetchTasksService(query);
15+
const {status,data} = await fetchTasksService(query);
1616

17-
if (response?.status === 200) {
18-
setTasks(response?.data);
17+
if (status === 200) {
18+
setTasks(data);
1919
} else {
2020
toast({
2121
variant: "destructive",
@@ -27,9 +27,9 @@ export const TaskProvider = ({ children }) => {
2727
}
2828

2929
const addTask = async (noteData) => {
30-
const response = await addTaskService(noteData)
31-
if (response.status === 201) {
32-
getTasks();
30+
const {status,data} = await addTaskService(noteData)
31+
if (status === 201) {
32+
setTasks((prev) => [...prev, data])
3333
toast({
3434
description: "Task added successfully!",
3535
});
@@ -44,9 +44,13 @@ export const TaskProvider = ({ children }) => {
4444

4545
const editTask = async (id, taskData) => {
4646

47-
const response = await updateTaskService(id, taskData);
48-
if (response?.data) {
49-
getTasks();
47+
const {status,data} = await updateTaskService(id, taskData);
48+
if (status===200) {
49+
setTasks((prev) =>
50+
prev?.map((task) =>
51+
task?._id === data?._id ? data : task
52+
)
53+
)
5054
toast({
5155
description: "Task edited Successfully!!",
5256
});
@@ -60,11 +64,15 @@ export const TaskProvider = ({ children }) => {
6064
}
6165

6266
const pinTask = async (id,isPinned) => {
63-
const response = await updateTaskService(id, { isPinned: !isPinned });
64-
if (response?.data) {
65-
getTasks();
67+
const {status,data} = await updateTaskService(id, { isPinned: !isPinned });
68+
if (status===200) {
69+
setTasks((prev) =>
70+
prev?.map((task) =>
71+
task?._id === data?._id ? data : task
72+
)
73+
)
6674
toast({
67-
description: `Task ${response?.data?.isPinned ? 'pinned' : 'unpinned'} Successfully!`,
75+
description: `Task ${data?.isPinned ? 'pinned' : 'unpinned'} Successfully!`,
6876
});
6977

7078
} else {
@@ -76,13 +84,17 @@ export const TaskProvider = ({ children }) => {
7684
}
7785
}
7886

79-
const checkTask = async (id,status) => {
87+
const checkTask = async (task) => {
8088

81-
const response = await updateTaskService(id, { status: !status });
82-
if (response?.data) {
83-
getTasks();
89+
const {status,data} = await updateTaskService(task?._id, { status: !task?.status });
90+
if (status===200) {
91+
setTasks((prev) =>
92+
prev?.map((task) =>
93+
task?._id === data?._id ? data : task
94+
)
95+
)
8496
toast({
85-
description: `Task marked as ${response?.data?.status ? 'done' : 'pending'} Successfully!`,
97+
description: `Task marked as ${data?.status ? 'done' : 'pending'} Successfully!`,
8698
});
8799

88100
} else {
@@ -95,9 +107,9 @@ export const TaskProvider = ({ children }) => {
95107
}
96108

97109
const removeTask = async (id) => {
98-
const response = await removeTaskService(id);
99-
if (response?.data) {
100-
getTasks();
110+
const {status,data} = await removeTaskService(id);
111+
if (status===200) {
112+
setTasks((prevTasks) => prevTasks.filter((task) => task._id !== id));
101113
toast({
102114
description: "Task removed Successfully!",
103115
});

server/controllers/noteController.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const updateNotes = async (req, res) => {
8787
await note.save();
8888

8989
// Send the updated note as a response
90-
res.json(note);
90+
res.status(200).json(note);
9191
} catch (error) {
9292
console.error('Error updating note:', error.message);
9393
res.status(500).json({ msg: 'Internal Server error' }); // 500 Internal Server Error
@@ -117,7 +117,7 @@ const updateNotesIsPinned = async (req, res) => {
117117
await note.save();
118118

119119
// Send the updated note as a response
120-
res.json(note);
120+
res.status(200).json(note);
121121
} catch (error) {
122122
console.error('Error updating note:', error.message);
123123
res.status(500).json({ msg: 'Internal Server error' }); // 500 Internal Server Error
@@ -143,7 +143,7 @@ const deleteNotes = async (req, res) => {
143143
await Note.findByIdAndDelete(id);
144144

145145
// Send a success message after deleting the note
146-
res.json({ msg: 'Note deleted' });
146+
res.status(200).json({ msg: 'Note deleted' });
147147
} catch (error) {
148148
console.error('Error deleting note:', error.message);
149149
res.status(500).json({ msg: 'Internal Server error' }); // 500 Internal Server Error

server/controllers/taskController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const updateTask = async (req, res) => {
8080
await task.save();
8181

8282
// Send the updated task as a response
83-
res.json(task);
83+
res.status(200).json(task);
8484
} catch (error) {
8585
console.error('Error updating task:', error.message);
8686
res.status(500).json({ msg: 'Internal Server error' }); // 500 Internal Server Error
@@ -106,7 +106,7 @@ const deleteTask = async (req, res) => {
106106
await Task.findByIdAndDelete(id);
107107

108108
// Send a success message after deleting the task
109-
res.json({ msg: 'Task deleted' });
109+
res.status(200).json({ msg: 'Task deleted' });
110110
} catch (error) {
111111
console.error('Error deleting task:', error.message);
112112
res.status(500).json({ msg: 'Internal Server error' }); // 500 Internal Server Error

0 commit comments

Comments
 (0)