Skip to content

Commit 9995981

Browse files
author
Jickson P
committed
Sync data to firebase
1 parent e7339e0 commit 9995981

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

src/Application.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import Navigate from './utils/Navigate';
1414
import { Toolbar } from './components';
1515
import Navigation from './scenes/Navigation';
1616
import store from './stores/ReactEvalStore';
17+
import * as GLOBAL from './utils/Globals';
18+
19+
const Firebase = require('firebase');
1720

1821
class 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}

src/scenes/BarcodeList.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class BarcodeList extends Component {
4646
.state
4747
.dataSource
4848
.cloneWithRows(nextProps.barcodes);
49+
50+
console.log('componentWillReceiveProps ',nextProps);
51+
4952
// Below comment is used to remove the lint warning.
5053
// Do this only when you are sure that you are calling set state using redux store.
5154
this.setState({

src/stores/ReactEvalStore.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ function todoStore(state = defaultStore, action) {
1818
// Copy the element in front of array.
1919
console.log('State update called', action);
2020
const newArray = [{
21-
code: action.qrcode,
22-
time: new Date().getTime(),
21+
code: action.qrcode.code,
22+
time: action.qrcode.time,
2323
}];
2424
return Object.assign({}, state, {
2525
barcodes: newArray.concat(state.barcodes),
2626
isLoaded: true,
2727
});
28+
case 'UPDATE_ALL_QR_CODES':
29+
console.log('State update called UPDATE_ALL_QR_CODES ', action);
30+
return Object.assign({}, state, {
31+
barcodes: action.qrcodes,
32+
isLoaded: true,
33+
});
2834
default:
2935
return state;
3036
}

src/utils/Globals.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module.exports = {
2+
FIREBASE_URL: 'https://reactevalapp.firebaseio.com/',
3+
FIREBASE_KEY: 'qrcodes',
24
SCREEN_TITLE: {
35
QR_CODE_SCANNER: 'QR Code Scanner',
46
SCAN_QR_CODE: 'Scan QR Code',

0 commit comments

Comments
 (0)