-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.js
65 lines (50 loc) · 1.65 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React from 'react';
import { render } from 'react-dom';
import { connect, Provider } from 'react-redux';
import createElmishStore from '../elm/createElmishStore';
import forwardTo from '../elm/forwardTo';
import mapEffects from '../elm/mapEffects';
import { View as RandomGif, update as randomGifUpdate } from '../5-random-gif-viewer/main';
// UPDATE
const INIT = 'INIT';
const LEFT = 'LEFT';
const RIGHT = 'RIGHT';
export function* update(incomingModel, action) {
const model = incomingModel || {
left: yield* randomGifUpdate(undefined, action),
right: yield* randomGifUpdate(undefined, action)
};
const { type, payload } = action;
switch (type) {
case INIT:
return {
left: yield* mapEffects(randomGifUpdate(model.left, action), LEFT),
right: yield* mapEffects(randomGifUpdate(model.right, action), RIGHT)
};
case LEFT:
return {
...incomingModel,
left: yield* mapEffects(randomGifUpdate(model.left, payload), LEFT)
};
case RIGHT:
return {
...incomingModel,
right: yield* mapEffects(randomGifUpdate(model.right, payload), RIGHT)
};
default:
return model;
}
}
// VIEW
export const View = ({dispatch, left, right}) => (
<div style={{display: 'flex'}}>
<RandomGif dispatch={forwardTo(dispatch, LEFT)} model={left} />
<RandomGif dispatch={forwardTo(dispatch, RIGHT)} model={right} />
</div>
);
// MAIN
const ConnectedView = connect(model => model)(View);
const store = createElmishStore(update);
const Application = () => <Provider store={store}><ConnectedView /></Provider>;
store.dispatch(INIT, 'funny cats');
render(<Application />, document.getElementById('app'));