1- import { GET_USER_PROFILE } from './types'
1+ import { GET_USER_PROFILE , GET_ALL_MEMBERS , UPDATE_USER_PROFILE , GET_USER_EVENTS , GET_USER_PROJECTS , GET_USER_POSTS } from './types'
22import { errorHandler } from '../utils/errorHandler'
33import axios from 'axios'
44import { setRequestStatus } from '../utils/setRequestStatus'
55
6+ // GET USER PROFILE
67export const getProfile = ( ) => async ( dispatch ) => {
78 try {
89 const res = await axios . get ( '/user/me' )
@@ -20,6 +21,7 @@ export const getProfile = () => async (dispatch)=> {
2021 }
2122}
2223
24+ // FOLLOW USER
2325export const followUser = ( userId ) => async ( dispatch ) => {
2426 try {
2527 let followObj = {
@@ -31,16 +33,17 @@ export const followUser = (userId) => async (dispatch) => {
3133 if ( res . status === 200 ) {
3234 dispatch ( setRequestStatus ( true ) )
3335 console . log ( 'started following ' , followObj )
34- // dispatch({
35- // type: GET_USER_PROFILE,
36- // payload: res.data.user,
37- // });
36+ dispatch ( {
37+ type : GET_USER_PROFILE ,
38+ payload : res . data . user ,
39+ } ) ;
3840 }
3941 } catch ( error ) {
4042 dispatch ( errorHandler ( error ) )
4143 }
4244}
4345
46+ // UnFOLLOW USER
4447export const unFollowUser = ( userId ) => async ( dispatch ) => {
4548 try {
4649 let unFollowObj = {
@@ -52,25 +55,106 @@ export const unFollowUser = (userId) => async (dispatch) => {
5255 if ( res . status === 200 ) {
5356 dispatch ( setRequestStatus ( true ) )
5457 console . log ( 'unfollowed ' , unFollowObj )
55- // dispatch({
56- // type: GET_USER_PROFILE,
57- // payload: res.data.user,
58- // });
58+ dispatch ( {
59+ type : GET_USER_PROFILE ,
60+ payload : res . data . user ,
61+ } ) ;
5962 }
6063 } catch ( error ) {
6164 dispatch ( errorHandler ( error ) )
6265 }
6366}
6467
68+ // REMOVE USER
6569export const removeUser = ( userId ) => async ( dispatch ) => {
6670 try {
6771 const res = await axios . patch ( `/user/remove/${ userId } ` )
6872 dispatch ( setRequestStatus ( false ) )
6973 if ( res . status === 200 ) {
7074 dispatch ( setRequestStatus ( true ) )
7175 console . log ( 'user removed ' , userId )
76+ const response = await axios . get ( '/org/members/all' )
77+ if ( response . status === 200 ) {
78+ dispatch ( {
79+ type : GET_ALL_MEMBERS ,
80+ payload : response . data . members
81+ } ) ;
82+ }
7283 }
7384 } catch ( error ) {
7485 dispatch ( errorHandler ( error ) )
7586 }
76- }
87+ }
88+
89+ // UPDATE USER PROFILE
90+ export const updateProfile = ( updatedInfo ) => async ( dispatch ) => {
91+ try {
92+ console . log ( 'updating ' , updatedInfo )
93+ const res = await axios . patch ( '/user/me' , updatedInfo )
94+ dispatch ( setRequestStatus ( false ) )
95+ if ( res . status === 200 ) {
96+ dispatch ( setRequestStatus ( true ) )
97+ console . log ( 'user profile updated ' , res . data )
98+ dispatch ( {
99+ type : UPDATE_USER_PROFILE ,
100+ payload : res . data . data
101+ } )
102+ }
103+ } catch ( error ) {
104+ dispatch ( errorHandler ( error ) )
105+ }
106+ }
107+
108+ // GET EVENTS CREATED BY USER
109+ export const getEventsCreatedByUser = ( pagination = 10 , page = 1 ) => async ( dispatch ) => {
110+ try {
111+ const res = await axios . get ( `event/me/all?pagination=${ pagination } &page=${ page } ` ) ;
112+ dispatch ( setRequestStatus ( false ) )
113+ if ( res . status === 200 ) {
114+ dispatch ( setRequestStatus ( true ) )
115+ console . log ( 'fetching all events created by user ' , res . data . events )
116+ dispatch ( {
117+ type : GET_USER_EVENTS ,
118+ payload : res . data . events
119+ } )
120+ }
121+ } catch ( error ) {
122+ dispatch ( errorHandler ( error ) )
123+ }
124+ }
125+
126+ // GET ALL PROJECT CREATED BY A USER
127+ export const getProjectCreatedByUser = ( pagination = 10 , page = 1 ) => async ( dispatch ) => {
128+ try {
129+ const res = await axios . get ( `/project/me/all?pagination=${ pagination } &page=${ page } ` ) ;
130+ dispatch ( setRequestStatus ( false ) )
131+ if ( res . status === 200 ) {
132+ dispatch ( setRequestStatus ( true ) )
133+ console . log ( 'fetching all projects created by user ' , res . data . projects )
134+ dispatch ( {
135+ type : GET_USER_PROJECTS ,
136+ payload : res . data . projects
137+ } ) ;
138+ }
139+ } catch ( error ) {
140+ dispatch ( errorHandler ( error ) )
141+ }
142+ }
143+
144+ // GET POSTS CREATED BY USER
145+ export const getPostsCreatedByUser = ( pagination = 10 , page = 1 ) => async ( dispatch ) => {
146+ try {
147+ const res = await axios . get ( `/post/me/all?pagination=${ pagination } &page=${ page } ` ) ;
148+ dispatch ( setRequestStatus ( false ) )
149+ if ( res . status === 200 ) {
150+ dispatch ( setRequestStatus ( true ) )
151+ console . log ( 'fetching all posts created by user ' , res . data . posts )
152+ dispatch ( {
153+ type : GET_USER_POSTS ,
154+ payload : res . data . posts
155+ } )
156+ }
157+ } catch ( error ) {
158+ dispatch ( errorHandler ( error ) )
159+ }
160+ }
0 commit comments