{"size": 2208, "ext": "js", "max_stars_repo_path": "client/store/comments.js", "max_stars_repo_name": "hackerDelta/capstone", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/store/comments.js", "max_issues_repo_name": "hackerDelta/capstone", "max_issues_repo_issues_event_min_datetime": "2020-07-01T17:04:22.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-13T16:22:42.000Z", "max_forks_repo_path": "client/store/comments.js", "max_forks_repo_name": "hackerDelta/capstone", "max_forks_repo_forks_event_min_datetime": "2020-07-01T16:58:58.000Z", "max_forks_repo_forks_event_max_datetime": "2020-07-09T23:32:58.000Z"}
{"max_stars_count": null, "max_issues_count": 22, "max_forks_count": 3, "avg_line_length": 23.2421052632, "max_line_length": 89, "alphanum_fraction": 0.6503623188}
import axios from 'axios';

const GET_COMMENTS = 'GET_COMMENTS';

const CREATE_COMMENT = 'CREATE_COMMENT';

const REMOVE_COMMENT = 'REMOVE_COMMENT';

const UPDATE_COMMENT = 'UPDATE_COMMENT';

const getComments = (comments) => ({
  type: 'GET_COMMENTS',
  comments
});

const createComment = (comment) => ({
  type: CREATE_COMMENT,
  comment
});

const removeComment = (id) => ({ type: 'REMOVE_COMMENT', id });

const updateComment = (id, comment) => ({
  type: 'UPDATE_COMMENT',
  id,
  comment
});

const defaultComments = [];

export const fetchCommentsFromServer = (id) => async (dispatch) => {
  try {
    const response = await axios.get(
      `https://hackerdelta-capstone.herokuapp.com/api/businesses/${id}/comments`
    );
    dispatch(getComments(response.data));
  } catch (err) {
    console.log(err);
  }
};

export const createCommentFromServer = (businessId, comment) => async (
  dispatch
) => {
  try {
    const response = await axios.post(
      `https://hackerdelta-capstone.herokuapp.com/api/businesses/${businessId}/comments`,
      comment
    );
    dispatch(createComment(response.data));
  } catch (err) {
    console.log(err);
  }
};

export const removeCommentFromServer = (id) => async (dispatch) => {
  try {
    await axios.delete(
      `https://hackerdelta-capstone.herokuapp.com/api/comments/${id}`
    );
    dispatch(removeComment(id));
  } catch (err) {
    console.log(err);
  }
};

export const updateCommentFromServer = (id, comment) => async (dispatch) => {
  try {
    const response = await axios.put(
      `https://hackerdelta-capstone.herokuapp.com/api/comments/${id}`,
      comment
    );
    dispatch(updateComment(response.data));
  } catch (err) {
    console.log(err);
  }
};

export default function commentsReducer(state = defaultComments, action) {
  switch (action.type) {
    case GET_COMMENTS:
      return action.comments;
    case CREATE_COMMENT:
      return [action.comment, ...state];
    case UPDATE_COMMENT:
      return state.map((comment) =>
        comment.id === action.id ? action.comment : comment
      );
    case REMOVE_COMMENT:
      return state.filter((comment) => comment.id !== action.id);
    default:
      return state;
  }
}
