|
1 | 1 | import { Action } from 'redux';
|
2 |
| -import { ListStoriesRequest, ListStoriesResponse } from '../proto/hackernews_pb'; |
| 2 | +import { Item, ListStoriesRequest, ListStoriesResponse } from '../proto/hackernews_pb'; |
3 | 3 | import { GrpcAction, grpcRequest } from '../middleware/grpc';
|
4 | 4 | import { Code, Metadata } from 'grpc-web-client';
|
5 | 5 | import { HackerNewsService } from '../proto/hackernews_pb_service';
|
6 | 6 | import { PingRequest, PingResponse } from '../proto/ping_pb';
|
7 | 7 | import { PingService } from '../proto/ping_pb_service';
|
8 | 8 |
|
9 | 9 | export const STORIES_INIT = 'STORIES_INIT';
|
| 10 | +export const ADD_STORY = 'ADD_STORY'; |
10 | 11 |
|
11 |
| -type ListStoriesInit = { |
12 |
| - type: typeof STORIES_INIT, |
13 |
| -}; |
14 | 12 |
|
15 | 13 | export const ping = () => {
|
16 | 14 | return grpcRequest<PingRequest, PingResponse>({
|
@@ -38,14 +36,28 @@ export const listStories = () => {
|
38 | 36 | host: 'http://localhost:8900',
|
39 | 37 | methodDescriptor: HackerNewsService.ListStories,
|
40 | 38 | onMessage: message => {
|
41 |
| - console.log(message); |
| 39 | + const story = message.getStory(); |
| 40 | + if (story) { |
| 41 | + return addStory(story); |
| 42 | + } |
42 | 43 | return;
|
43 | 44 | },
|
44 | 45 | });
|
45 | 46 | };
|
46 | 47 |
|
| 48 | +type ListStoriesInit = { |
| 49 | + type: typeof STORIES_INIT, |
| 50 | +}; |
47 | 51 | export const listStoriesInit = (): ListStoriesInit => ({type: STORIES_INIT});
|
48 | 52 |
|
| 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 | + |
49 | 60 | export type StoryActionTypes =
|
50 | 61 | | ListStoriesInit
|
| 62 | + | AddStory |
51 | 63 | | GrpcAction<ListStoriesRequest, ListStoriesResponse>;
|
0 commit comments