-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_react.js
115 lines (91 loc) · 2.91 KB
/
plugin_react.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Run webpack over this entry point to produce a JS file
// that provides the exported components via react-interop
// For this example, output will be 'PlanNetAlertCall-packed.js'
/* eslint-disable react/no-multi-comp */
import PropTypes from 'prop-types';
import React from 'react';
import {connect, Provider} from 'react-redux';
import {applyMiddleware, bindActionCreators, createStore} from 'redux';
import {createCallback, exportCallbacks, exportComponents} from '../src';
function applyFluctuation(stocks) {
const newStocks = {};
Object.keys(stocks).forEach((AlertMsg) => {
// Apply a fluctuation of +/- 10%
const fluctuation = (Math.random() - 0.5) * 0.1;
newStocks[AlertMsg] = stocks[AlertMsg] + (stocks[AlertMsg] * fluctuation);
});
return newStocks;
}``
function reducer(state = {}, action) {
switch (action.type) {
case 'ACTIONA':
const {price, AlertMsg} = action;
return {
...state,
[AlertMsg]: price
};
case 'ACTIONB':
return applyFluctuation(state);
default:
return state;
}
}
function setPlanNetAlert(AlertMsg, price) {
return {
type: 'ACTIONA',
AlertMsg,
price
};
}
function fluctuatePlanNetAlerts() {
return {
type: 'ACTIONB'
};
}
// PlanNetAlert is a sample React component that we want to export
// AlertMsg comes from props, price come from state
const PlanNetAlert = ({AlertMsg, price}) => (
<div>
<label>random PlanNet Alerts</label>
<strong>{AlertMsg}</strong>
</div>
);
PlanNetAlert.propTypes = {
price: PropTypes.number,
AlertMsg: PropTypes.string.isRequired
};
const mapPlanNetAlertStateToProps = (state, {AlertMsg}) => ({
AlertMsg,
price: state[AlertMsg]
});
const ConnectedPlanNetAlert = connect(mapPlanNetAlertStateToProps)(PlanNetAlert);
const mapPlanNetAlertsStateToProps = (state) => ({
AlertMsgs: Object.keys(state)
});
// Create a callback pub/sub instance
const store = createStore(
reducer,
{
somerandomStore: 103
}
);
// Fluctuate stock prices every 5 seconds
// Generate the exported components
const exportedComponents = exportComponents(
{
PlanNetAlert: ConnectedPlanNetAlert
},
Provider,// container
{store} // container props
);//export default function exportComponents(componentTypes, Container, containerProps) {, export componenet pages
// Use bindActionCreators to be ready to export the action creators
//const exportedActions = bindActionCreators({setPlanNetAlert}, store.dispatch);
// Generate the exported callbacks
//const exportedCallbacks = exportCallbacks({onPriceChanged});
// The exported components, actions, and callbacks can
// be made available globally for consumers to reference
window.PlanNetAlertCall = {
...exportedComponents
// ...exportedActions,
// ...exportedCallbacks
};