Skip to content

Commit e2737df

Browse files
committed
Initialize post redux module
1 parent 59cbbd3 commit e2737df

File tree

9 files changed

+114
-24
lines changed

9 files changed

+114
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
"tslint-config-airbnb": "^5.11.1",
125125
"tslint-config-prettier": "^1.18.0",
126126
"turndown": "^5.0.3",
127-
"typesafe-actions": "^3.2.1",
127+
"typesafe-actions": "^4.4.1",
128128
"typescript": "^3.5.1",
129129
"unist-util-visit": "^1.4.0",
130130
"url-loader": "1.1.2",

src/components/post/PostReplies.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import styled from 'styled-components';
3+
import { Comment } from '../../lib/graphql/post';
4+
5+
const PostRepliesBlock = styled.div``;
6+
7+
export interface PostRepliesProps {
8+
comments: Comment[];
9+
}
10+
11+
const PostReplies: React.FC<PostRepliesProps> = props => {
12+
return <PostRepliesBlock />;
13+
};
14+
15+
export default PostReplies;

src/containers/post/PostComments.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import PostCommentsTemplate from '../../components/post/PostCommentsTemplate';
33
import PostCommentsWriteContainer from './PostCommentsWriteContainer';
4-
import { Comment, WRITE_COMMENT, RELOAD_REPLIES } from '../../lib/graphql/post';
4+
import { Comment, WRITE_COMMENT } from '../../lib/graphql/post';
55
import PostCommentsList from '../../components/post/PostCommentsList';
66
import { MutationResult, Mutation } from 'react-apollo';
77

@@ -21,13 +21,13 @@ const PostComments: React.FC<PostCommentsProps> = ({ comments, postId }) => {
2121
text: string;
2222
};
2323
const onLoadReplies = (id: string) => {
24-
return client.query({
25-
query: RELOAD_REPLIES,
26-
variables: {
27-
id: id,
28-
},
29-
fetchPolicy: 'network-only',
30-
});
24+
// return client.query({
25+
// query: RELOAD_REPLIES,
26+
// variables: {
27+
// id: id,
28+
// },
29+
// fetchPolicy: 'network-only',
30+
// });
3131
};
3232
const onReply = async ({ commentId, text }: ReplyOptions) => {
3333
try {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import { Query, QueryResult } from 'react-apollo';
3+
import { GET_REPLIES, CommentWithReplies } from '../../lib/graphql/post';
4+
import PostReplies from '../../components/post/PostReplies';
5+
6+
export interface PostRepliesProps {
7+
postId: string;
8+
commentId: string;
9+
}
10+
11+
const PostRepliesContainer: React.FC<PostRepliesProps> = ({
12+
postId,
13+
commentId,
14+
}) => {
15+
return (
16+
<Query query={GET_REPLIES} variables={{ id: commentId }}>
17+
{({
18+
loading,
19+
error,
20+
data,
21+
}: QueryResult<{ comment: CommentWithReplies }>) => {
22+
if (loading || !data) return null;
23+
return <PostReplies comments={data.comment.replies} />;
24+
}}
25+
</Query>
26+
);
27+
};
28+
29+
export default PostRepliesContainer;

src/lib/graphql/post.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ export interface SinglePost {
8080
comments: Comment[];
8181
}
8282

83+
export interface CommentWithReplies {
84+
id: string;
85+
replies: Comment[];
86+
}
87+
8388
export const GET_POST_LIST = gql`
8489
query Posts($cursor: ID) {
8590
posts(cursor: $cursor) {
@@ -184,20 +189,10 @@ export const GET_COMMENT = gql`
184189
}
185190
`;
186191

187-
export const RELOAD_REPLIES = gql`
192+
export const GET_REPLIES = gql`
188193
query ReloadReplies($id: ID!) {
189194
comment(comment_id: $id) {
190195
id
191-
user {
192-
id
193-
username
194-
profile {
195-
thumbnail
196-
}
197-
}
198-
text
199-
replies_count
200-
created_at
201196
replies {
202197
id
203198
user {

src/modules/__tests__/post.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import post, { postActions, PostState } from '../post';
2+
3+
describe('post redux module', () => {
4+
const initialState: PostState = {
5+
id: null,
6+
};
7+
it('has initialState', () => {
8+
const state = post(undefined, { type: 'INIT' } as any);
9+
expect(state).toEqual(initialState);
10+
});
11+
describe('reducer', () => {
12+
it('handles SET_ID', () => {
13+
const state = post(initialState, postActions.setPostId('aaaa'));
14+
expect(state.id).toBe('aaaa');
15+
});
16+
});
17+
});

src/modules/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import { combineReducers } from 'redux';
22
import core, { CoreState } from './core';
33
import write, { WriteState } from './write';
44
import header, { HeaderState } from './header';
5+
import post, { PostState } from './post';
56

67
export type RootState = {
78
core: CoreState;
89
write: WriteState;
910
header: HeaderState;
11+
post: PostState;
1012
};
1113

1214
const rootReducer = combineReducers({
1315
core,
1416
write,
1517
header,
18+
post,
1619
});
1720

1821
export default rootReducer;

src/modules/post.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {
2+
createReducer,
3+
createStandardAction,
4+
ActionType,
5+
} from 'typesafe-actions';
6+
import { updateKey } from '../lib/utils';
7+
8+
const SET_POST_ID = 'post/SET_POST_ID';
9+
10+
export interface PostState {
11+
id: string | null;
12+
}
13+
14+
const initialState: PostState = {
15+
id: null,
16+
};
17+
18+
const actions = {
19+
setPostId: createStandardAction(SET_POST_ID)<string>(),
20+
};
21+
22+
export const postActions = actions;
23+
24+
type RootAction = ActionType<typeof actions>;
25+
26+
const post = createReducer<PostState, RootAction>(initialState).handleAction(
27+
actions.setPostId,
28+
(state, action) => updateKey(state, 'id', action.payload),
29+
);
30+
31+
export default post;

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11461,10 +11461,10 @@ typedarray@^0.0.6:
1146111461
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
1146211462
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
1146311463

11464-
typesafe-actions@^3.2.1:
11465-
version "3.2.1"
11466-
resolved "https://registry.yarnpkg.com/typesafe-actions/-/typesafe-actions-3.2.1.tgz#833623a4079eda5dbcb775113f6dcf02b71c03f8"
11467-
integrity sha512-hvhuNqsai6LzXQ+Hl6vajC9bGd/kHHL1hJvI4GQ0Rs6Fax1cjbNEsuFGy/RbfLPhBAL10n6DECEHUa+Q9E+MxQ==
11464+
typesafe-actions@^4.4.1:
11465+
version "4.4.1"
11466+
resolved "https://registry.yarnpkg.com/typesafe-actions/-/typesafe-actions-4.4.1.tgz#92e4348289d932592f1f18c162f5c094aba8fe9a"
11467+
integrity sha512-uV74N8uDU2lVaNnYUGmg/Th/mPi9JQJJ5A8+YcHBE5GI+bdgFyl79zCZw4LLU331GCdHWDo6F2J7LVgBHRvaGQ==
1146811468

1146911469
typescript@^3.5.1:
1147011470
version "3.5.1"

0 commit comments

Comments
 (0)