-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(follow and unfollow): users should be able to follow each other
Add action creators for follow Add reducers for follow [Maintains #165273540]
- Loading branch information
Showing
3 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// third party libraries | ||
import { toast } from 'react-toastify'; | ||
|
||
// import axios instance | ||
import axios from 'utils/mainAxios'; | ||
|
||
// user constants | ||
import { | ||
FOLLOW_AUTHOR_ERROR, | ||
FOLLOW_AUTHOR_SUCCESS, | ||
UNFOLLOW_AUTHOR_FAILURE, | ||
UNFOLLOW_AUTHOR_SUCCESS, | ||
GET_FOLLOWERS_ERROR, | ||
GET_FOLLOWERS_SUCCESS, | ||
GET_FOLLOWING_ERROR, | ||
GET_FOLLOWING_SUCCESS, | ||
} from 'store/actions/followTypes'; | ||
|
||
/** | ||
* follow author action function on success | ||
* @param {string} response | ||
*/ | ||
export const followAuthorSuccess = response => ({ | ||
type: FOLLOW_AUTHOR_SUCCESS, | ||
response, | ||
}); | ||
|
||
/** | ||
* follow author action function on error | ||
* @param {string} error | ||
*/ | ||
export const followAuthorError = error => ({ | ||
type: FOLLOW_AUTHOR_ERROR, | ||
error, | ||
}); | ||
|
||
/** | ||
* follow author action function on success | ||
* @param {string} response | ||
*/ | ||
export const getFollowersSuccess = response => ({ | ||
type: GET_FOLLOWERS_SUCCESS, | ||
response, | ||
}); | ||
|
||
/** | ||
* get followers action function on error | ||
* @param {string} error | ||
*/ | ||
export const getFollowersError = error => ({ | ||
type: GET_FOLLOWERS_ERROR, | ||
error, | ||
}); | ||
|
||
/** | ||
* get followers action function on success | ||
* @param {string} response | ||
*/ | ||
export const getFollowingSuccess = response => ({ | ||
type: GET_FOLLOWING_SUCCESS, | ||
response, | ||
}); | ||
|
||
/** | ||
* follow author action function on error | ||
* @param {string} error | ||
*/ | ||
export const getFollowingError = error => ({ | ||
type: GET_FOLLOWING_ERROR, | ||
error, | ||
}); | ||
|
||
/** | ||
* action creator function for following authors | ||
* username as a parameter and dispatch as a function | ||
* @param {string} username | ||
*/ | ||
export const fellowAndUnfollowAuthorActionCreator = username => dispatch => axios | ||
.post(`authors/${username}/follow/`) | ||
.then(response => dispatch(followAuthorSuccess(response.data.profile))) | ||
.catch(error => dispatch(followAuthorError(error))); | ||
|
||
|
||
/** | ||
* action creator function for getting author's followers | ||
* username as a parameter and dispatch as a function | ||
* @param {string} username | ||
*/ | ||
export const getFollowersActionCreator = () => dispatch => axios | ||
.get('authors/followers/') | ||
.then(response => dispatch(getFollowersSuccess(response.data.profile))) | ||
.catch(error => dispatch(getFollowersError(error))); | ||
|
||
/** | ||
* action creator function for getting author's followers | ||
* username as a parameter and dispatch as a function | ||
* @param {string} username | ||
*/ | ||
export const getFollowingActionCreator = () => dispatch => axios | ||
.get('authors/following/') | ||
.then(response => dispatch(getFollowingSuccess(response.data.profile))) | ||
.catch(error => dispatch(getFollowingError(error))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const FOLLOW_AUTHOR_ERROR = 'FOLLOW_AUTHOR_ERROR'; | ||
export const FOLLOW_AUTHOR_SUCCESS = 'FOLLOW_AUTHOR_SUCCESS'; | ||
export const GET_FOLLOWERS_ERROR = 'GET_FOLLOWERS_ERROR'; | ||
export const GET_FOLLOWERS_SUCCESS = 'GET_FOLLOWERS_SUCCESS'; | ||
export const GET_FOLLOWING_ERROR = 'GET_FOLLOWING_ERROR'; | ||
export const GET_FOLLOWING_SUCCESS = 'GET_FOLLOWING_SUCCESS'; | ||
export const UNFOLLOW_AUTHOR_FAILURE = 'UNFOLLOW_AUTHOR_FAILURE'; | ||
export const UNFOLLOW_AUTHOR_SUCCESS = 'UNFOLLOW_AUTHOR_SUCCESS'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// import action types | ||
import { | ||
FOLLOW_AUTHOR_ERROR, | ||
FOLLOW_AUTHOR_SUCCESS, | ||
GET_FOLLOWERS_ERROR, | ||
GET_FOLLOWERS_SUCCESS, | ||
GET_FOLLOWING_ERROR, | ||
GET_FOLLOWING_SUCCESS, | ||
} from 'store/actions/followTypes'; | ||
|
||
// initial state object | ||
export const initialState = { | ||
followers: 0, | ||
following: 0, | ||
data: [ | ||
{ | ||
author_name: '', | ||
author_stats: { | ||
followers: '', | ||
following: 0 | ||
} | ||
} | ||
], | ||
error: {}, | ||
}; | ||
|
||
/** | ||
* This is a switch function for our follow reducer. | ||
* @param {Object} - initialState* | ||
* @return {object} - action | ||
* @example | ||
* | ||
*/ | ||
|
||
const followReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case FOLLOW_AUTHOR_SUCCESS: | ||
return Object.assign({}, state, { | ||
follow: action.follow, | ||
}); | ||
case FOLLOW_AUTHOR_ERROR: | ||
case GET_FOLLOWERS_SUCCESS: | ||
return Object.assign({}, state, { | ||
follow: action.follow, | ||
}); | ||
case GET_FOLLOWERS_ERROR: | ||
case GET_FOLLOWING_SUCCESS: | ||
return Object.assign({}, state, { | ||
follow: action.follow, | ||
}); | ||
case GET_FOLLOWING_ERROR: | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default followReducer; |