@@ -14,6 +14,9 @@ import Navigate from './utils/Navigate';
1414import { Toolbar } from './components' ;
1515import Navigation from './scenes/Navigation' ;
1616import store from './stores/ReactEvalStore' ;
17+ import * as GLOBAL from './utils/Globals' ;
18+
19+ const Firebase = require ( 'firebase' ) ;
1720
1821class Application extends Component {
1922
@@ -24,6 +27,7 @@ class Application extends Component {
2427
2528 constructor ( props ) {
2629 super ( props ) ;
30+ this . itemsRef = this . getRef ( ) . child ( GLOBAL . FIREBASE_KEY ) ;
2731 this . state = {
2832 drawer : null ,
2933 navigator : null ,
@@ -34,10 +38,15 @@ class Application extends Component {
3438 // Do this only when you are sure that you are calling set state using redux store.
3539 this . setState ( {
3640 barcodes : store . getState ( ) . barcodes ,
41+ isLoaded : store . getState ( ) . isLoaded ,
3742 } ) ; // eslint-disable-line react/no-set-state
3843 } ) ;
3944 }
4045
46+ getRef ( ) {
47+ return new Firebase ( GLOBAL . FIREBASE_URL ) ;
48+ }
49+
4150 getChildContext = ( ) => {
4251 return {
4352 drawer : this . state . drawer ,
@@ -58,12 +67,41 @@ class Application extends Component {
5867 } ;
5968
6069
61- onQrCodeRead ( qrcode ) {
62- this . state . navigator . back ( ) ;
63- store . dispatch ( {
64- type : 'ADD_QR_CODE' ,
65- qrcode,
66- } ) ;
70+ componentDidMount ( ) {
71+ this . listenForItems ( this . itemsRef ) ;
72+ }
73+
74+ listenForItems ( itemsRef ) {
75+ itemsRef . on ( 'value' , ( snap ) => {
76+ // get children as an array
77+ var qrcodes = [ ] ;
78+ snap . forEach ( ( child ) => {
79+ qrcodes . push ( {
80+ code : child . val ( ) . code ,
81+ time : child . val ( ) . time ,
82+ _key : child . key ( ) ,
83+ } ) ;
84+ } ) ;
85+ qrcodes . reverse ( ) ;
86+ console . log ( 'Data recieved listenForItems' , qrcodes ) ;
87+ store . dispatch ( {
88+ type : 'UPDATE_ALL_QR_CODES' ,
89+ qrcodes,
90+ } ) ;
91+ } ) ;
92+ }
93+
94+
95+ onQrCodeRead ( code ) {
96+
97+ this . state . navigator . back ( ) ;
98+ const qrcode = {
99+ code : code ,
100+ time : new Date ( ) . getTime ( ) ,
101+ } ;
102+
103+ //TODO check if this is right
104+ this . itemsRef . push ( qrcode ) ;
67105 }
68106
69107 onScanQrCodePressed ( ) {
@@ -107,6 +145,7 @@ class Application extends Component {
107145 title = { route . title }
108146 path = { route . path }
109147 barcodes = { this . state . barcodes }
148+ isLoaded = { this . state . isLoaded }
110149 onScanQrCodePressed = { this . onScanQrCodePressed . bind ( this ) }
111150 onQrCodeRead = { this . onQrCodeRead . bind ( this ) }
112151 { ...route . props }
0 commit comments