Skip to content

Commit

Permalink
feat(follow and unfollow): users should be able to follow each other
Browse files Browse the repository at this point in the history
Add action creators for follow
Add reducers for follow

[Maintains #165273540]
  • Loading branch information
nadralia committed Jun 20, 2019
1 parent afe7b2c commit 513f30f
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/store/actions/followActions/index.js
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)));
8 changes: 8 additions & 0 deletions src/store/actions/followTypes.js
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';
57 changes: 57 additions & 0 deletions src/store/reducers/followReducer/index.js
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;

0 comments on commit 513f30f

Please sign in to comment.