Skip to content

Commit

Permalink
action refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pjblitz86 committed Jan 17, 2019
1 parent 59ef5e0 commit b25124b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
28 changes: 14 additions & 14 deletions client/src/actions/postActions.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import axios from 'axios';
import { ADD_POST, GET_ERRORS, GET_POSTS, POST_LOADING, DELETE_POST, GET_POST, CLEAR_ERRORS } from './types';

export const addPost = postData => dispatch => {
dispatch(clearErrors());
axios.post('/posts', postData)
.then(res => dispatch({
type: ADD_POST,
payload: res.data
})
)
.catch(err => dispatch({
type: GET_ERRORS,
payload: err.response.data
}));
}

export const getPosts = () => dispatch => {
dispatch(setPostLoading());
axios.get('/posts')
Expand Down Expand Up @@ -43,6 +29,20 @@ export const getPost = (id) => dispatch => {
}));
}

export const addPost = postData => dispatch => {
dispatch(clearErrors());
axios.post('/posts', postData)
.then(res => dispatch({
type: ADD_POST,
payload: res.data
})
)
.catch(err => dispatch({
type: GET_ERRORS,
payload: err.response.data
}));
}

export const deletePost = id => dispatch => {
axios.delete(`/posts/${id}`)
.then(res => dispatch({
Expand Down
15 changes: 10 additions & 5 deletions client/src/actions/types.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
export const GET_ERRORS = 'GET_ERRORS';
export const SET_CURRENT_USER = 'SET_CURRENT_USER';
export const GET_PROFILE = 'GET_PROFILE';
export const GET_PROFILES = 'GET_PROFILES';

// PROFILE
export const GET_PROFILE = 'GET_PROFILE';
export const PROFILE_LOADING = 'PROFILE_LOADING';
export const PROFILE_NOT_FOUND = 'PROFILE_NOT_FOUND';
export const CLEAR_CURRENT_PROFILE = 'CLEAR_CURRENT_PROFILE';
export const POST_LOADING = 'POST_LOADING';
export const PROFILE_NOT_FOUND = 'PROFILE_NOT_FOUND';

// POST
export const GET_POSTS = 'GET_POSTS';
export const POST_LOADING = 'POST_LOADING';
export const GET_POST = 'GET_POST';
export const ADD_POST = 'ADD_POST';
export const DELETE_POST = 'DELETE_POST';
export const CLEAR_ERRORS = 'CLEAR_ERRORS';

// ERRORS
export const GET_ERRORS = 'GET_ERRORS';
export const CLEAR_ERRORS = 'CLEAR_ERRORS';

0 comments on commit b25124b

Please sign in to comment.