@@ -12,10 +12,10 @@ export const TaskProvider = ({ children }) => {
12
12
const { searchText} = usePage ( ) ;
13
13
14
14
const getTasks = async ( query = searchText ) => {
15
- const response = await fetchTasksService ( query ) ;
15
+ const { status , data } = await fetchTasksService ( query ) ;
16
16
17
- if ( response ?. status === 200 ) {
18
- setTasks ( response ?. data ) ;
17
+ if ( status === 200 ) {
18
+ setTasks ( data ) ;
19
19
} else {
20
20
toast ( {
21
21
variant : "destructive" ,
@@ -27,9 +27,9 @@ export const TaskProvider = ({ children }) => {
27
27
}
28
28
29
29
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 ] )
33
33
toast ( {
34
34
description : "Task added successfully!" ,
35
35
} ) ;
@@ -44,9 +44,13 @@ export const TaskProvider = ({ children }) => {
44
44
45
45
const editTask = async ( id , taskData ) => {
46
46
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
+ )
50
54
toast ( {
51
55
description : "Task edited Successfully!!" ,
52
56
} ) ;
@@ -60,11 +64,15 @@ export const TaskProvider = ({ children }) => {
60
64
}
61
65
62
66
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
+ )
66
74
toast ( {
67
- description : `Task ${ response ?. data ?. isPinned ? 'pinned' : 'unpinned' } Successfully!` ,
75
+ description : `Task ${ data ?. isPinned ? 'pinned' : 'unpinned' } Successfully!` ,
68
76
} ) ;
69
77
70
78
} else {
@@ -76,13 +84,17 @@ export const TaskProvider = ({ children }) => {
76
84
}
77
85
}
78
86
79
- const checkTask = async ( id , status ) => {
87
+ const checkTask = async ( task ) => {
80
88
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
+ )
84
96
toast ( {
85
- description : `Task marked as ${ response ?. data ?. status ? 'done' : 'pending' } Successfully!` ,
97
+ description : `Task marked as ${ data ?. status ? 'done' : 'pending' } Successfully!` ,
86
98
} ) ;
87
99
88
100
} else {
@@ -95,9 +107,9 @@ export const TaskProvider = ({ children }) => {
95
107
}
96
108
97
109
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 ) ) ;
101
113
toast ( {
102
114
description : "Task removed Successfully!" ,
103
115
} ) ;
0 commit comments