|
1 | 1 | import * as types from '../types/articles';
|
2 | 2 | import axios from '../utils/axios';
|
3 | 3 |
|
4 |
| -export const fetchArticles = () => { |
5 |
| - return (dispatch) => { |
6 |
| - dispatch({type: types.GET_ARTICLES}); |
7 |
| - axios.get('/articles') |
8 |
| - .then(response => { |
9 |
| - dispatch({ type: types.GET_ARTICLES_SUCCESS, payload: response.data }); |
10 |
| - }) |
11 |
| - .catch(error => { |
12 |
| - dispatch({ type: types.GET_ARTICLES_ERROR, payload: error }); |
13 |
| - }); |
| 4 | +export const fetchArticles = () => async dispatch => { |
| 5 | + try { |
| 6 | + dispatch(getArticles()); |
| 7 | + const response = await axios.get('/articles'); |
| 8 | + dispatch(getArticlesSuccess(response.data)); |
| 9 | + } catch (error) { |
| 10 | + dispatch(getArticlesError(error)); |
14 | 11 | }
|
15 | 12 | }
|
16 | 13 |
|
17 |
| -export const addArticle = (payload, history) => { |
18 |
| - return (dispatch) => { |
19 |
| - dispatch({type: types.ADD_ARTICLE}); |
20 |
| - axios.post('/articles', payload) |
21 |
| - .then(response => { |
22 |
| - dispatch({ type: types.ADD_ARTICLE_SUCCESS, payload: response.data }); |
23 |
| - history.push(`/article/${response.data.id}`); |
24 |
| - }) |
25 |
| - .catch(error => { |
26 |
| - dispatch({ type: types.ADD_ARTICLE_ERROR, payload: error }); |
27 |
| - }); |
| 14 | +export const createArticle = (payload, history) => async dispatch => { |
| 15 | + try { |
| 16 | + dispatch(addArticle()); |
| 17 | + const response = await axios.post('/articles', payload); |
| 18 | + dispatch(addArticleSuccess(response.data)); |
| 19 | + history.push(`/article/${response.data.id}`); |
| 20 | + } catch (error) { |
| 21 | + dispatch(addArticleError(error)); |
28 | 22 | }
|
29 | 23 | }
|
| 24 | + |
| 25 | +export const getArticles = () => ({ type: types.GET_ARTICLES }); |
| 26 | +export const getArticlesSuccess = payload => ({ type: types.GET_ARTICLES_SUCCESS, payload }); |
| 27 | +export const getArticlesError = payload => ({ type: types.GET_ARTICLES_ERROR, payload }); |
| 28 | + |
| 29 | +export const addArticle = () => ({ type: types.ADD_ARTICLE }); |
| 30 | +export const addArticleSuccess = payload => ({ type: types.ADD_ARTICLE_SUCCESS, payload }); |
| 31 | +export const addArticleError = payload => ({ type: types.ADD_ARTICLE_ERROR, payload }); |
0 commit comments