|
1 |
| -import Config from '../../../server/config'; |
2 |
| -import fetch from 'isomorphic-fetch'; |
3 |
| - |
4 |
| -const baseURL = typeof window === 'undefined' ? process.env.BASE_URL || (`http://localhost:${Config.port}`) : ''; |
| 1 | +import callApi from '../../util/apiCaller'; |
5 | 2 |
|
6 | 3 | // Export Constants
|
7 | 4 | export const ADD_POST = 'ADD_POST';
|
8 | 5 | export const ADD_POSTS = 'ADD_POSTS';
|
9 |
| -export const ADD_SELECTED_POST = 'ADD_SELECTED_POST'; |
10 | 6 | export const DELETE_POST = 'DELETE_POST';
|
11 | 7 |
|
12 | 8 | // Export Actions
|
13 | 9 | export function addPost(post) {
|
14 | 10 | return {
|
15 | 11 | type: ADD_POST,
|
16 |
| - name: post.name, |
17 |
| - title: post.title, |
18 |
| - content: post.content, |
19 |
| - slug: post.slug, |
20 |
| - cuid: post.cuid, |
21 |
| - _id: post._id, |
| 12 | + post, |
22 | 13 | };
|
23 | 14 | }
|
24 | 15 |
|
25 | 16 | export function addPostRequest(post) {
|
26 | 17 | return (dispatch) => {
|
27 |
| - fetch(`${baseURL}/api/posts`, { |
28 |
| - method: 'post', |
29 |
| - body: JSON.stringify({ |
30 |
| - post: { |
31 |
| - name: post.name, |
32 |
| - title: post.title, |
33 |
| - content: post.content, |
34 |
| - }, |
35 |
| - }), |
36 |
| - headers: new Headers({ |
37 |
| - 'Content-Type': 'application/json', |
38 |
| - }), |
39 |
| - }).then((res) => res.json()).then(res => dispatch(addPost(res.post))); |
| 18 | + return callApi('posts', 'post', { |
| 19 | + post: { |
| 20 | + name: post.name, |
| 21 | + title: post.title, |
| 22 | + content: post.content, |
| 23 | + }, |
| 24 | + }).then(res => dispatch(addPost(res.post))); |
40 | 25 | };
|
41 | 26 | }
|
42 | 27 |
|
43 |
| -export function addSelectedPost(post) { |
| 28 | +export function addPosts(posts) { |
44 | 29 | return {
|
45 |
| - type: ADD_SELECTED_POST, |
46 |
| - post, |
| 30 | + type: ADD_POSTS, |
| 31 | + posts, |
47 | 32 | };
|
48 | 33 | }
|
49 | 34 |
|
50 |
| -export function getPostRequest(post) { |
| 35 | +export function fetchPosts() { |
51 | 36 | return (dispatch) => {
|
52 |
| - return fetch(`${baseURL}/api/posts/${post}`, { |
53 |
| - method: 'get', |
54 |
| - headers: new Headers({ |
55 |
| - 'Content-Type': 'application/json', |
56 |
| - }), |
57 |
| - }).then((response) => response.json()).then(res => dispatch(addSelectedPost(res.post))); |
| 37 | + return callApi('posts').then(res => { |
| 38 | + dispatch(addPosts(res.posts)); |
| 39 | + }); |
58 | 40 | };
|
59 | 41 | }
|
60 | 42 |
|
61 |
| -export function deletePost(post) { |
62 |
| - return { |
63 |
| - type: DELETE_POST, |
64 |
| - post, |
| 43 | +export function fetchPost(cuid) { |
| 44 | + return (dispatch) => { |
| 45 | + return callApi(`posts/${cuid}`).then(res => dispatch(addPost(res.post))); |
65 | 46 | };
|
66 | 47 | }
|
67 | 48 |
|
68 |
| -export function addPosts(posts) { |
| 49 | +export function deletePost(cuid) { |
69 | 50 | return {
|
70 |
| - type: ADD_POSTS, |
71 |
| - posts, |
72 |
| - }; |
73 |
| -} |
74 |
| - |
75 |
| -export function fetchPosts() { |
76 |
| - return (dispatch) => { |
77 |
| - return fetch(`${baseURL}/api/posts`). |
78 |
| - then((response) => response.json()). |
79 |
| - then((response) => dispatch(addPosts(response.posts))); |
| 51 | + type: DELETE_POST, |
| 52 | + cuid, |
80 | 53 | };
|
81 | 54 | }
|
82 | 55 |
|
83 |
| -export function deletePostRequest(post) { |
| 56 | +export function deletePostRequest(cuid) { |
84 | 57 | return (dispatch) => {
|
85 |
| - fetch(`${baseURL}/api/posts`, { |
86 |
| - method: 'delete', |
87 |
| - body: JSON.stringify({ |
88 |
| - id: post._id, |
89 |
| - }), |
90 |
| - headers: new Headers({ |
91 |
| - 'Content-Type': 'application/json', |
92 |
| - }), |
93 |
| - }).then(() => dispatch(deletePost(post))); |
| 58 | + return callApi(`posts/${cuid}`, 'delete').then(() => dispatch(deletePost(cuid))); |
94 | 59 | };
|
95 | 60 | }
|
0 commit comments