-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
104 lines (98 loc) · 3.12 KB
/
index.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
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import './index.css';
import App from './App';
import * as types from './actions/types'
import registerServiceWorker from './registerServiceWorker';
import reducers from './reducers';
import thunk from 'redux-thunk'
import firebase from './firebase'
import { Route, BrowserRouter } from 'react-router-dom'
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const store = createStoreWithMiddleware(reducers)
/**
*/
firebase.auth().onAuthStateChanged((user) => {
if (user) {
let firestore = firebase.firestore();
let gamesCollection = firestore
.collection("games")
.where('owners.' user.uid, '==', 'ADMIN')
.onSnapshot((querySnapshot) => {
querySnapshot.docChanges.forEach((change)=>{
if (change.type === "added") {
let charactersCollection = firestore
.collection(`games/${change.doc.id}/characters`)
.onSnapshot((characterSnapshots) => {
characterSnapshots.docChanges.forEach(function (characterChange) {
store.dispatch({
type: types.SAVE_CHARACTER,
payload: {
...characterChange.doc.data(),
gameId: change.doc.id,
id: characterChange.doc.id
}
});
});
},
(error) => {
console.log("on snapshot error: ", error)
});
store.dispatch({
type: types.ADD_LISTENER,
payload: charactersCollection
})
let itemsCollection = firestore
.collection(`games/${change.doc.id}/items`)
.onSnapshot((itemSnapshots) => {
itemSnapshots.docChanges.forEach(function (itemChange) {
store.dispatch({
type: types.SAVE_ITEM,
payload: {
...itemChange.doc.data(),
gameId: change.doc.id,
id: itemChange.doc.id
}
});
});
},
(error) => {
console.log("on snapshot error: ", error)
});
store.dispatch({
type: types.ADD_LISTENER,
payload: itemsCollection
})
}
store.dispatch({
type: types.SAVE_GAME,
payload: {
...change.doc.data(),
id: change.doc.id
}
});
});
}, (error) => {
console.log("on snapshot error: ", error)
});
store.dispatch({
type: types.ADD_LISTENER,
payload: gamesCollection
})
} else {
store.dispatch({
type: types.REMOVE_LISTENERS
});
console.log("No logged in user");
}
})
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<Route path="/" component={App} />
</BrowserRouter>
</Provider>
, document.querySelector('#root'));
registerServiceWorker();