text: import axios from 'axios';
  import { setAlert } from './alert';
  import {
    GET_POSTS,
    POST_ERROR,
    UPDATE_LIKES,
    DELETE_POST,
    ADD_POST,
    GET_POST,
    ADD_COMMENT,
    REMOVE_COMMENT,
  } from './types';
  
  // Get posts
  export const getPosts = () => async (dispatch) => {
    try {
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'x-auth-token': localStorage.token,
        },
      };
      const res = await axios.get('http://localhost:5000/api/posts', config);
  
      dispatch({
        type: GET_POSTS,
        payload: res.data,
      });
    } catch (err) {
      dispatch({
        type: POST_ERROR,
        payload: { msg: err.response.statusText, status: err.response.status },
      });
    }
  };
  
  // Add like
  export const addLike = (id) => async (dispatch) => {
    try {
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'x-auth-token': localStorage.token,
        },
      };
      const res = await axios.put(
        `http://localhost:5000/api/posts/like/${id}`,
        config
      );
  
      dispatch({
        type: UPDATE_LIKES,
        payload: { id, likes: res.data },
      });
    } catch (err) {
      dispatch({
        type: POST_ERROR,
        payload: { msg: err.response.statusText, status: err.response.status },
      });
    }
  };
  
  // Remove like
  export const removeLike = (id) => async (dispatch) => {
    try {
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'x-auth-token': localStorage.token,
        },
      };
      const res = await axios.put(
        `http://localhost:5000/api/posts/unlike/${id}`,
        config
      );
  
      dispatch({
        type: UPDATE_LIKES,
        payload: { id, likes: res.data },
      });
    } catch (err) {
      dispatch({
        type: POST_ERROR,
        payload: { msg: err.response.statusText, status: err.response.status },
      });
    }
  };
  
  // Delete post
  export const deletePost = (id) => async (dispatch) => {
    try {
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'x-auth-token': localStorage.token,
        },
      };
      await axios.delete(`http://localhost:5000/api/posts/${id}`, config);
  
      dispatch({
        type: DELETE_POST,
        payload: id,
      });
  
      dispatch(setAlert('Post Removed', 'danger'));
    } catch (err) {
      dispatch({
        type: POST_ERROR,
        payload: { msg: err.response.statusText, status: err.response.status },
      });
    }
  };
  
  // Add post
  export const addPost = (formData) => async (dispatch) => {
    try {
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'x-auth-token': localStorage.token,
        },
      };
      const res = await axios.post(
        'http://localhost:5000/api/posts',
        formData,
        config
      );
  
      dispatch({
        type: ADD_POST,
        payload: res.data,
      });
  
      dispatch(setAlert('Post Created', 'success'));
    } catch (err) {
      console.log(err.response.data);
      dispatch({
        type: POST_ERROR,
        payload: { msg: err.response.statusText, status: err.response.status },
      });
    }
  };
  
  // Get post
  export const getPost = (id) => async (dispatch) => {
    try {
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'x-auth-token': localStorage.token,
        },
      };
      const res = await axios.get(
        `http://localhost:5000/api/posts/${id}`,
        config
      );
  
      dispatch({
        type: GET_POST,
        payload: res.data,
      });
    } catch (err) {
      dispatch({
        type: POST_ERROR,
        payload: { msg: err.response.statusText, status: err.response.status },
      });
    }
  };
  
  // Add comment
  export const addComment = (postId, formData) => async (dispatch) => {
    try {
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'x-auth-token': localStorage.token,
        },
      };
      const res = await axios.post(
        `http://localhost:5000/api/posts/comment/${postId}`,
        formData,
        config
      );
  
      dispatch({
        type: ADD_COMMENT,
        payload: res.data,
      });
  
      dispatch(setAlert('Comment Added', 'success'));
    } catch (err) {
      dispatch({
        type: POST_ERROR,
        payload: { msg: err.response.statusText, status: err.response.status },
      });
    }
  };
  
  // Delete comment
  export const deleteComment = (postId, commentId) => async (dispatch) => {
    try {
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'x-auth-token': localStorage.token,
        },
      };
      await axios.delete(
        `http://localhost:5000/api/posts/comment/${postId}/${commentId}`,
        config
      );
  
      dispatch({
        type: REMOVE_COMMENT,
        payload: commentId,
      });
  
      dispatch(setAlert('Comment Removed', 'danger'));
    } catch (err) {
      dispatch({
        type: POST_ERROR,
        payload: { msg: err.response.statusText, status: err.response.status },
      });
    }
  };
  