Skip to content

Commit d72cd8a

Browse files
committed
refactor: example pages
1 parent 39bbd91 commit d72cd8a

File tree

18 files changed

+212
-230
lines changed

18 files changed

+212
-230
lines changed

client/modules/App/AppReducer.js

+6
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ const AppReducer = (state = initialState, action) => {
1818
}
1919
};
2020

21+
/* Selectors */
22+
23+
// Get showAddPost
24+
export const getShowAddPost = state => state.app.showAddPost;
25+
26+
// Export Reducer
2127
export default AppReducer;
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava';
22
import { reducerTest } from 'redux-ava';
3-
import appReducer from '../AppReducer';
3+
import appReducer, { getShowAddPost } from '../AppReducer';
44
import { toggleAddPost } from '../AppActions';
55

66
test('action for TOGGLE_ADD_POST is working', reducerTest(
@@ -9,3 +9,9 @@ test('action for TOGGLE_ADD_POST is working', reducerTest(
99
toggleAddPost(),
1010
{ showAddPost: true },
1111
));
12+
13+
test('getShowAddPost selector', t => {
14+
t.is(getShowAddPost({
15+
app: { showAddPost: false },
16+
}), false);
17+
});

client/modules/Post/PostActions.js

+24-59
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,60 @@
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';
52

63
// Export Constants
74
export const ADD_POST = 'ADD_POST';
85
export const ADD_POSTS = 'ADD_POSTS';
9-
export const ADD_SELECTED_POST = 'ADD_SELECTED_POST';
106
export const DELETE_POST = 'DELETE_POST';
117

128
// Export Actions
139
export function addPost(post) {
1410
return {
1511
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,
2213
};
2314
}
2415

2516
export function addPostRequest(post) {
2617
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)));
4025
};
4126
}
4227

43-
export function addSelectedPost(post) {
28+
export function addPosts(posts) {
4429
return {
45-
type: ADD_SELECTED_POST,
46-
post,
30+
type: ADD_POSTS,
31+
posts,
4732
};
4833
}
4934

50-
export function getPostRequest(post) {
35+
export function fetchPosts() {
5136
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+
});
5840
};
5941
}
6042

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)));
6546
};
6647
}
6748

68-
export function addPosts(posts) {
49+
export function deletePost(cuid) {
6950
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,
8053
};
8154
}
8255

83-
export function deletePostRequest(post) {
56+
export function deletePostRequest(cuid) {
8457
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)));
9459
};
9560
}

client/modules/Post/PostReducer.js

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
1-
import { ADD_POST, ADD_POSTS, ADD_SELECTED_POST, DELETE_POST } from './PostActions';
1+
import { ADD_POST, ADD_POSTS, DELETE_POST } from './PostActions';
22

3-
const initialState = { posts: [], post: null };
3+
// Initial State
4+
const initialState = { data: [] };
45

56
const PostReducer = (state = initialState, action) => {
67
switch (action.type) {
78
case ADD_POST :
89
return {
9-
posts: [{
10-
name: action.name,
11-
title: action.title,
12-
content: action.content,
13-
slug: action.slug,
14-
cuid: action.cuid,
15-
_id: action._id,
16-
}, ...state.posts],
17-
post: state.post };
18-
19-
case ADD_POSTS :
20-
return {
21-
posts: action.posts,
22-
post: state.post,
10+
data: [action.post, ...state.data],
2311
};
2412

25-
case ADD_SELECTED_POST :
13+
case ADD_POSTS :
2614
return {
27-
post: action.post,
28-
posts: state.posts,
15+
data: action.posts,
2916
};
3017

3118
case DELETE_POST :
3219
return {
33-
posts: state.posts.filter((post) => post._id !== action.post._id),
20+
data: state.data.filter(post => post.cuid !== action.cuid),
3421
};
3522

3623
default:
3724
return state;
3825
}
3926
};
4027

28+
/* Selectors */
29+
30+
// Get all posts
31+
export const getPosts = state => state.posts.data;
32+
33+
// Get post by cuid
34+
export const getPost = (state, cuid) => state.posts.data.filter(post => post.cuid === cuid)[0];
35+
36+
// Export Reducer
4137
export default PostReducer;
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
import test from 'ava';
22
import { actionTest } from 'redux-ava';
3+
34
import {
45
ADD_POST,
5-
ADD_SELECTED_POST,
66
DELETE_POST,
77
ADD_POSTS,
88
addPost,
9-
addSelectedPost,
109
deletePost,
1110
addPosts,
1211
} from '../PostActions';
1312

1413
const post = { name: 'Prashant', title: 'Hello Mern', cuid: 'f34gb2bh24b24b2', content: "All cats meow 'mern!'", slug: 'hello-mern', _id: 1 };
1514

16-
test('should return the correct type for addPost', actionTest(addPost, post, {
17-
type: ADD_POST,
18-
name: post.name,
19-
title: post.title,
20-
content: post.content,
21-
slug: post.slug,
22-
cuid: post.cuid,
23-
_id: post._id,
24-
}));
25-
26-
test('should return the correct type for addSelectedPost', actionTest(deletePost, post, { type: DELETE_POST, post }));
15+
test('should return the correct type for addPost', actionTest(
16+
addPost,
17+
post,
18+
{ type: ADD_POST, post },
19+
));
2720

28-
test('should return the correct type for deletePost', actionTest(addSelectedPost, post, { type: ADD_SELECTED_POST, post }));
21+
test('should return the correct type for deletePost', actionTest(
22+
deletePost,
23+
post.cuid,
24+
{ type: DELETE_POST, cuid: post.cuid },
25+
));
2926

30-
test('should return the correct type for addPosts', actionTest(addPosts, [post], {
31-
type: ADD_POSTS,
32-
posts: [post],
33-
}));
27+
test('should return the correct type for addPosts', actionTest(
28+
addPosts,
29+
[post],
30+
{ type: ADD_POSTS, posts: [post] },
31+
));

0 commit comments

Comments
 (0)