Skip to content

Commit c510d91

Browse files
author
Milan Pavlik
committed
Wire up backend to frontend
1 parent c2090c6 commit c510d91

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

app/src/Stories.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ class Stories extends React.Component<StoriesProps, {}> {
2222
}
2323

2424
render() {
25+
console.log(this.props.stories)
2526
return (
26-
<Container style={{marginTop: '3em'}}>
27+
<Container style={{padding: '1em'}} fluid={true}>
2728
<Header as="h1" dividing={true}>Hacker News with gRPC-Web</Header>
2829

2930
<Grid columns={2} stackable={true} divided={'vertically'}>
30-
<Grid.Column width={3}>
31+
<Grid.Column width={4}>
3132
<StoryList stories={this.props.stories}/>
3233
</Grid.Column>
3334

34-
<Grid.Column width={13} stretched={true}>
35+
<Grid.Column width={12} stretched={true}>
3536

3637
<Header as="h2">Example body text</Header>
3738
<StoryView/>
@@ -45,7 +46,7 @@ class Stories extends React.Component<StoriesProps, {}> {
4546
}
4647

4748
export default connect((state: RootState) => ({
48-
stories: Array.from(state.stories.stories.values()),
49+
stories: Object.keys(state.stories.stories).map(key => state.stories.stories[key]),
4950
loading: state.stories.loading,
5051
error: state.stories.error,
5152
}))(Stories);

app/src/actions/stories.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { Action } from 'redux';
2-
import { ListStoriesRequest, ListStoriesResponse } from '../proto/hackernews_pb';
2+
import { Item, ListStoriesRequest, ListStoriesResponse } from '../proto/hackernews_pb';
33
import { GrpcAction, grpcRequest } from '../middleware/grpc';
44
import { Code, Metadata } from 'grpc-web-client';
55
import { HackerNewsService } from '../proto/hackernews_pb_service';
66
import { PingRequest, PingResponse } from '../proto/ping_pb';
77
import { PingService } from '../proto/ping_pb_service';
88

99
export const STORIES_INIT = 'STORIES_INIT';
10+
export const ADD_STORY = 'ADD_STORY';
1011

11-
type ListStoriesInit = {
12-
type: typeof STORIES_INIT,
13-
};
1412

1513
export const ping = () => {
1614
return grpcRequest<PingRequest, PingResponse>({
@@ -38,14 +36,28 @@ export const listStories = () => {
3836
host: 'http://localhost:8900',
3937
methodDescriptor: HackerNewsService.ListStories,
4038
onMessage: message => {
41-
console.log(message);
39+
const story = message.getStory();
40+
if (story) {
41+
return addStory(story);
42+
}
4243
return;
4344
},
4445
});
4546
};
4647

48+
type ListStoriesInit = {
49+
type: typeof STORIES_INIT,
50+
};
4751
export const listStoriesInit = (): ListStoriesInit => ({type: STORIES_INIT});
4852

53+
type AddStory = {
54+
type: typeof ADD_STORY,
55+
payload: Item,
56+
};
57+
58+
export const addStory = (story: Item) => ({ type: ADD_STORY, payload: story });
59+
4960
export type StoryActionTypes =
5061
| ListStoriesInit
62+
| AddStory
5163
| GrpcAction<ListStoriesRequest, ListStoriesResponse>;

app/src/reducers/stories.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RootAction } from '../actions';
2-
import { STORIES_INIT } from '../actions/stories';
2+
import { ADD_STORY, STORIES_INIT } from '../actions/stories';
3+
import { Item } from '../proto/hackernews_pb';
34

45
export type StoryId = number;
56

@@ -14,23 +15,31 @@ export type Story = {
1415
};
1516

1617
export type StoryState = {
17-
readonly stories: Map<StoryId, Story>,
18+
readonly stories: { [storyId: number]: Item.AsObject },
1819
readonly error: Error | null,
1920
readonly loading: boolean,
2021
};
2122

2223
const initialState = {
23-
stories: new Map<StoryId, Story>(),
24+
stories: {},
2425
error: null,
2526
loading: false
2627
};
2728

2829
export default function (state: StoryState = initialState, action: RootAction): StoryState {
2930

3031
switch (action.type) {
32+
3133
case STORIES_INIT:
3234
return {...state, loading: true};
3335

36+
case ADD_STORY:
37+
const story: Item.AsObject = action.payload.toObject();
38+
if (story.id && story.id.id) {
39+
return {...state, loading: false, stories: {...state.stories, [story.id.id]: story}};
40+
}
41+
return state;
42+
3443
default:
3544
return state;
3645
}

0 commit comments

Comments
 (0)