-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.android.js
166 lines (147 loc) · 4.18 KB
/
index.android.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import React, { Component } from 'react';
import {
View,
AppRegistry,
StyleSheet,
ActivityIndicator,
BackAndroid,
Alert
} from 'react-native';
import { Router, Scene } from 'react-native-router-flux';
import DeviceInfo from 'react-native-device-info';
import NetworkInfo from 'react-native-network-info';
import HomePage from './HomePage';
import BarcodePage from './BarcodePage';
import ProductPage from './ProductPage';
import InputCodePage from './InputCodePage';
import baseStyle from './style';
class ThongJing extends Component {
constructor(props) {
super(props);
this.state = {
role: null,
isLoaded: false,
macAddress: DeviceInfo.getMacAddress(),
ip: null,
};
NetworkInfo.getIPAddress((ip) => {
this.setState({
ip: ip,
});
});
}
componentWillMount() {
this.loadRoles();
}
render() {
if (this.state.isLoaded) {
return (
<Router
navigationBarStyle={style.navigationBarStyle}
titleStyle={style.titleStyle}
barButtonIconStyle={style.barButtonIconStyle} >
<Scene
key="homePage"
component={HomePage}
title="ថុង ជីង" initial
role={this.state.role}
type="reset" />
<Scene
key="barcodePage"
component={BarcodePage}
title="ស្គែនលេខកូដផលិតផល"
type="reset" />
<Scene
key="inputCodePage"
component={InputCodePage}
title="បញ្ចូលលេខកូដផលិតផល"
type="reset" />
<Scene
key="productPage"
component={ProductPage}
title="ព័ត៌មានលំអិតពីផលិតផល"
type="reset" />
</Router>
);
} else {
return (
<View style={baseStyle.containerCentering}>
<ActivityIndicator
animating={true}
style={{ padding: 8, height: 80 }}
size="large"
/>
</View>
);
}
}
loadRoles() {
let apiKey = 'AIzaSyD1-wLf4jdSWdYLRHnIcIh6TxQFIKROOA0';
let id = '1at-LgWYPYycbdhTvCpyqWL2sTlgGvxK5h2q4lqUopto';
let sheetUrl = 'https://sheets.googleapis.com/v4/spreadsheets/';
let url = `${sheetUrl}${id}/values/Sheet1?key=${apiKey}`;
fetch(url, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
.then((response) => response.json())
.then((responseJson) => {
let rowMac = responseJson.values.find(row => row[1] === this.state.macAddress);
let rowIp = null;
let userRole = '';
if(rowMac != undefined) {
rowIp = rowMac[0] === this.state.ip;
userRole = rowMac[2];
}else {
rowIp = responseJson.values.find(row => row[0] === this.state.ip);
if(rowIp != undefined) {
userRole = rowIp[2];
}
}
if (rowMac && userRole == 'Admin') {
this.setState({ role: 'Admin', isLoaded: true });
return;
}
if (rowMac && rowIp && userRole == 'Seller') {
this.setState({ role: 'Seller', isLoaded: true });
return;
}
if (!rowMac && rowIp && userRole == 'Client') {
this.setState({ role: 'Client', isLoaded: true });
return;
}
showRoleNotFound();
})
.catch((error) => {
alert('មិនអាចទាញទិន្នន័យបាន');
});
}
}
function showRoleNotFound() {
const title = 'គ្មានការអនុញ្ញាត';
const message = 'សូមសាកសួរក្រុមហ៊ុន ថុង ជីង';
Alert.alert(title, message, [
{
text: 'ចាកចេញ',
onPress: () => BackAndroid.exitApp(),
},
],
{ cancelable: false });
}
const style = StyleSheet.create({
navigationBarStyle: {
backgroundColor: 'rgb(0, 150, 136)',
elevation: 4,
},
titleStyle: {
color: 'rgb(255, 255, 255)',
fontWeight: '400',
},
barButtonIconStyle: {
tintColor: 'rgb(255, 255, 255)',
},
});
AppRegistry.registerComponent('ThongJing', () => ThongJing);