-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.tsx
96 lines (87 loc) · 2.71 KB
/
index.tsx
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
import { h, Component, render } from 'preact';
import { HomePage } from './pages/Home';
import { from, combineLatest, merge, of } from 'rxjs';
import { map, mergeMap, scan, tap } from 'rxjs/operators';
export interface AppState { restaurants: any[]; }
export interface AppProps { seed: any[]; }
const CONFIG = {
apiKey: "AIzaSyC1pXdWIiJZRcJUYtoIi-MmrRdnTcUISgk",
authDomain: "ticket-fire.firebaseapp.com",
databaseURL: "https://ticket-fire.firebaseio.com",
projectId: "ticket-fire",
storageBucket: "ticket-fire.appspot.com",
messagingSenderId: "1090774042344"
};
function lazyLoad(config: any, enablePersistence = true) {
const app$ = from(import('firebase/app'));
// NOTE: The lazy load for Firestore does not work with TypeScipt
// w/out hacking the node_modules/firebase/firestore/package.json file
// to use "typings": "../index.d.ts".
const firestore$ = from(import('firebase/firestore'));
const rxfire$ = from(import('rxfire/firestore'));
return combineLatest(app$, firestore$, rxfire$)
.pipe(
map(([firebase, firestore, rxfire]) => {
const app = firebase.apps[0] || firebase.initializeApp(config);
const settings = { timestampsInSnapshots: true};
app.firestore().settings(settings);
if(enablePersistence) { app.firestore().enablePersistence(); }
return { app, rxfire };
})
);
}
/**
* Lazy load a list of todos from RxFire
* @param config
* @param collectionName
*/
function lazyData(config: any, collectionName: string) {
return lazyLoad(config)
.pipe(
mergeMap(load => {
const { app, rxfire } = load;
const ref = app.firestore().collection(collectionName);
const obs$ = rxfire.collectionData(ref, 'id')
return obs$;
})
);
}
class App extends Component<AppProps, AppState> {
constructor() {
super();
this.state = { restaurants: [] };
}
componentDidMount() {
const seed$ = merge(of(this.props.seed));
const data$ = lazyData(CONFIG, 'restaurants');
merge(seed$, data$).pipe(
scan((restaurants: any[], updatedRestaurants: any[], index) => {
return restaurants.map((r, i) => {
const updated = updatedRestaurants[i];
updated.previousOccupants = r.occupants;
return updated;
});
}, this.props.seed)
).subscribe(restaurants => {
this.setState({ restaurants });
});
}
render() {
return (
<HomePage restaurants={this.state.restaurants} />
);
}
}
if ((window as any)['__data__']) {
const data = (window as any)['__data__'];
render(
<App seed={data} />,
document.body,
document.querySelector('#root')!
);
} else {
render(
<App seed={[]} />,
document.querySelector('#root')!
);
}