-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (54 loc) · 1.33 KB
/
app.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
import _ from 'lodash';
import React from 'react';
import {render, unmountComponentAtNode} from 'react-dom';
import {Provider} from 'react-redux';
import {createStore} from 'redux';
import reducer from './reducers';
import {updateAvailability} from './actions';
import BoundView from './views/container';
class Fulfillment {
constructor (options) {
this.store = createStore(reducer);
this.skusHash = {};
this.updateAvailability(options);
}
_addReactComponent (sku) {
render(
<Provider store={ this.store }>
<BoundView idSelector={ sku.idSelector }/>
</Provider>,
document.getElementById(sku.idSelector)
);
}
updateAvailability (updates) {
this.store.dispatch(updateAvailability(updates));
this.addNewSkus(updates.skus);
}
/**
* Add sku to skuHash.
* @param skus
* @returns {{}|*}
*/
addNewSkus (skus) {
_.each(skus, function (sku) {
if (typeof this.skusHash[sku.idSelector] === 'undefined') {
this._addReactComponent(sku);
this.skusHash[sku.idSelector] = true;
}
}, this);
return this.skusHash;
}
removeSkuById (idSelector) {
try {
if (this.skusHash[idSelector]) {
unmountComponentAtNode(document.getElementById(idSelector));
delete this.skusHash[idSelector];
return true;
}
} catch (e) {
window.console.error(e);
}
return false;
}
}
export default Fulfillment;