8
8
getSingleBlogSuccess ,
9
9
getSingleCategorySuccess ,
10
10
} from "../features/blogSlice" ;
11
- import { updateUserBlogsSuccess } from "../features/authSlice" ;
11
+
12
12
import { RootState } from "../app/store" ;
13
13
import { toastError , toastSuccess } from "../helpers/ToastNotify" ;
14
14
import { useNavigate } from "react-router-dom" ;
@@ -20,11 +20,25 @@ const useBlogCall = () => {
20
20
const { token } = useSelector ( ( state : RootState ) => state . auth ) ;
21
21
const dispatch = useDispatch ( ) ;
22
22
23
- const getBlogData = async ( endpoint : string ) : Promise < void > => {
23
+ const getBlogData = async (
24
+ endpoint : string ,
25
+ query ?: string
26
+ ) : Promise < void > => {
24
27
dispatch ( fetchStart ( ) ) ;
25
28
try {
26
- const { data } = await axios ( `${ BASE_URL } ${ endpoint } ` ) ;
27
- dispatch ( getBlogSuccess ( { endpoint, data } ) ) ;
29
+ const url = query
30
+ ? `${ BASE_URL } ${ endpoint } ?${ query } `
31
+ : `${ BASE_URL } ${ endpoint } ` ;
32
+ const { data } = await axios ( url , {
33
+ headers : {
34
+ Authorization : `Token ${ token } ` ,
35
+ } ,
36
+ } ) ;
37
+ if ( query ?. includes ( "author=" ) ) {
38
+ dispatch ( getBlogByUserIdSuccess ( { endpoint : "userBlogs" , data } ) ) ;
39
+ } else {
40
+ dispatch ( getBlogSuccess ( { endpoint, data } ) ) ;
41
+ }
28
42
} catch ( error ) {
29
43
dispatch ( fetchFail ( ) ) ;
30
44
console . error ( error ) ;
@@ -67,8 +81,7 @@ const useBlogCall = () => {
67
81
Authorization : `Token ${ token } ` ,
68
82
} ,
69
83
} ) ;
70
- dispatch ( getBlogByUserIdSuccess ( data ) ) ;
71
- dispatch ( updateUserBlogsSuccess ( data ) ) ;
84
+ dispatch ( getBlogByUserIdSuccess ( { endpoint : "userBlogs" , data } ) ) ;
72
85
} catch ( error ) {
73
86
dispatch ( fetchFail ( ) ) ;
74
87
console . error ( error ) ;
0 commit comments