{"size": 2341, "ext": "js", "max_stars_repo_path": "frontend/src/actions/posts.js", "max_stars_repo_name": "HenryBrown0/udacity-react-readable", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "frontend/src/actions/posts.js", "max_issues_repo_name": "HenryBrown0/udacity-react-readable", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "frontend/src/actions/posts.js", "max_forks_repo_name": "HenryBrown0/udacity-react-readable", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.7281553398, "max_line_length": 72, "alphanum_fraction": 0.6488680051}
import axios from 'axios';
export const FETCH_CATEGORY_POSTS = 'FETCH_CATEGORY_POSTS';
export const FETCH_POST = 'FETCH_POST';
export const ADD_POST = 'ADD_POST';
export const DELETE_POST = 'DELETE_POST';
export const UPDATE_POST = 'UPDATE_POST';

const URL = 'http://localhost:3001/';
const headers = { 'Authorization': 'whatever-you-want' };

export function fetchCategoryPosts(category){
	const request = axios.get(`${URL}${category}/posts`,{headers});
	return dispatch => {
		request.then(({data}) => {
			console.log("Fetch category posts successful")
			dispatch({
				type: FETCH_CATEGORY_POSTS,
				categoryPosts: { category: category, posts: data}
			})
		})
	}
}

export function fetchPost(postID){
	const request = axios.get(`${URL}posts/${postID}`,{headers});
	return dispatch => {
		request.then(({data}) => {
			console.log("Fetch post successful")
			dispatch({
				type: FETCH_POST,
				post: data
			})
		})
	}
}

export function addPost(newPost){
	const request = axios.post(`${URL}posts`,newPost,{headers});
	return dispatch => {
		request.then((response) => {
			console.log("Add post successful")
			dispatch({
				type: ADD_POST,
				newPost: response.data
			})
		})
		.catch(function (error) {
    	console.error(error);
  	});
	}
}

export function deletePost(deletePost){
	const request = axios.delete(`${URL}posts/${deletePost.id}`,{headers});
	return dispatch => {
		request.then((response) => {
			console.log("Delete post successful")
			dispatch({
				type: DELETE_POST,
				deletePost
			})
		})
		.catch(function (error) {
    	console.error(error);
  	});
	}
}

export function votePost(vote){
	const request = axios
		.post(`${URL}posts/${vote.post.id}`,{ option: vote.type },{headers});
	return dispatch => {
		request.then((response) => {
			console.log("Vote post successful")
			dispatch({
				type: UPDATE_POST,
				updatePost: response.data
			})
		})
		.catch(function (error) {
    	console.error(error);
  	});
	}
}

export function editPost(edit){
	const request = axios
		.put(`${URL}posts/${edit.id}`,
			{ title: edit.title, body: edit.body  },{headers});
	return dispatch => {
		request.then((response) => {
			console.log("Edit post successful")
			dispatch({
				type: UPDATE_POST,
				updatePost: response.data
			})
		})
		.catch(function (error) {
    	console.error(error);
  	});
	}
}
