-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdminOrders.js
675 lines (617 loc) · 17.2 KB
/
AdminOrders.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
import React from 'react';
import {
Dimensions,
Modal,
KeyboardAvoidingView,
AsyncStorage,
StyleSheet,
TextInput,
View,
Text,
Image,
Platform,
DeviceEventEmitter,
ScrollView,
TouchableOpacity,
Share,
Alert,
FlatList
} from 'react-native';
import { Button } from 'react-native-elements';
import { Table, Row, Rows } from 'react-native-table-component';
import { Ionicons } from '@expo/vector-icons';
import { NavigationActions } from 'react-navigation';
import Colors from '../../constants/Colors';
import LoadingIndicator from '../../components/LoadingIndicator';
import OrderDetailBox from '../../components/OrderDetailBox';
import Server from '../../constants/server';
import {Select, Option} from "react-native-chooser";
const selectLabels = [
'قيد القبول',
'جاري التوصيل',
'تم التوصيل'
]
export default class Signin extends React.Component {
constructor(props) {
super(props);
this.state = {
detailed_order:[],
owner_login: '0',
storeOrdersFetched: false,
passname: '',
password: '',
errorMsg: '',
orders: [],
note:'...',
orderId:0,
details:'...',
delivery_cost:0,
clientName:'...',
phone:'',
orderStates: [],
modalVisible: false,
pickerData:[
{
label:'قيد القبول',
value:0
},
{
label: 'التوصيل',
value: 1
},
{
label:'تم التوصيل',
value:2
}
]
};
}
static navigationOptions = ({ navigation }) => {
return {
header: null,
tabBarOnPress: ({ previousScene, scene, jumpToIndex }) => {
// Inject event
DeviceEventEmitter.emit('ReloadStoreOrders', { empty: 0 });
// Keep original behaviour
jumpToIndex(scene.index);
}
};
};
componentDidMount () {
AsyncStorage.getItem('storeid', (err, storeid) => {
if(storeid) this.fetchStoreOrders(parseInt(storeid));
})
}
listeners = {
update: DeviceEventEmitter.addListener('ReloadStoreOrders', ({ empty }) => {
AsyncStorage.getItem('owner_login').then(logged => {
if (logged === null) return;
if (logged == '1') {
AsyncStorage.getItem('storeid').then(val => {
if (val === null) return;
this.setState({ storeOrdersFetched: false });
this.fetchStoreOrders(parseInt(val));
});
}
});
})
};
componentWillUnmount() {
// cleaning up listeners
// I am using lodash
_.each(this.listeners, listener => {
listener.remove();
});
}
openModal() {
this.setState({ modalVisible: true });
}
closeModal() {
this.setState({ modalVisible: false });
}
setOwnerLoginStatus = value => {
AsyncStorage.setItem('owner_login', value);
this.setState({ owner_login: value });
};
fetchStoreOrders = storeid => {
fetch(`${Server.dest}/api/get-store-orders?store_id=${storeid}`, {
headers: { 'Cache-Control': 'no-cache' }
})
.then(res => res.json())
.then(resJson => {
if (resJson.response == 1) {
var data = [];
var rowNum = 0;
var orderStates = [];
resJson.orders.map((order)=>{
data.push([order[0],this.location(order[1]),order[2],this.SeeMore(order[3],order[6],order[7],order[8],order[5],order[2],order[9],order[10]),this.order_status_changer(order[4],order[5], rowNum++)]);
//console.log('pushed', order[4], 'at', rowNum-1);
orderStates.push(order[4])
})
this.setState({ orders: data, orderStates, storeOrdersFetched: true }, () => this.setOwnerLoginStatus('1'));
}
});
};
showData = (details,note,name,discounted,id,price,delivery_cost,phone) =>{
this.setState({
details:details,note:note,clientName:name,phone,discounted,price,delivery_cost,orderId:id,modalVisible:true
})
fetch(Server.dest + '/api/order-data?id=' + id)
.then(res => res.json())
.then(data => {
this.setState({detailed_order:data.meals})
});
}
SeeMore = (details,note,name,discounted,id,price,delivery_cost,phone) =>(
<TouchableOpacity style={{
backgroundColor:'gray',
borderRadius:30,
justifyContent:'center',
alignItems:'center'
}}
onPress={()=>{
this.showData(details,note,name,discounted,id,price,delivery_cost,phone)
}}
>
<Text style={{
textAlign:'center',
justifyContent:'center'
}}
>المزيد </Text>
</TouchableOpacity>
);
loginOwner = () => {
if (this.state.password.length == 0) {
this.setState({ errorMsg: 'ادخل كلمة المرور' });
return;
}
if (this.state.passname.length == 0) {
this.setState({ errorMsg: 'ادخل اسم الدخول للمطعم' });
return;
}
if (/\s/g.test(this.state.passname)) {
this.setState({ errorMsg: 'غير مسوح بالمسافات فى اسم الدخول' });
return;
}
this.setState({ errorMsg: '' });
fetch(
Server.dest +
'/api/store-owner-login?passname=' +
this.state.passname +
'&password=' +
this.state.password,
{ headers: { 'Cache-Control': 'no-cache' } }
)
.then(res => res.json())
.then(resJson => {
if (resJson.response == 0)
this.setState({
errorMsg: 'هذا المحل التجاري غير مُسجل عندنا او كلمة مرور غير صحيحة'
});
else {
AsyncStorage.setItem('storeid', resJson.response);
this.fetchStoreOrders(parseInt(resJson.response));
AsyncStorage.getItem('token', (err, result) => {
if(result)
{
fetch(
Server.dest +
'/api/store-push-tokens?store_id=' +
resJson.response +
'&token=' +
result,
{ headers: { 'Cache-Control': 'no-cache' } }
);
}
});
}
});
};
shouldRenderErrorMessage = () => {
if (this.state.errorMsg != '') {
return (
<View
style={{
paddingVertical: 3,
flexDirection: 'row',
justifyContent: 'center'
}}
>
<Text style={{ fontFamily: 'myfont', color: '#E85151' }}>
{this.state.errorMsg}
</Text>
</View>
);
}
};
share_order = () =>{
Share.share({
message: 'Order here http://talbatk.net:90/share-order-data?id='+this.state.orderId,
title: 'Order Share'
}, {
// Android only:
dialogTitle: 'Share Order',
// iOS only:
excludedActivityTypes: [
'com.apple.UIKit.activity.PostToTwitter'
]
})
}
location = (value) => (
<TouchableOpacity onPress={() =>{
Share.share({
message: ''+value.replace("- null",""),
title: 'Location for an order'
}, {
// Android only:
dialogTitle: 'Share Location',
// iOS only:
excludedActivityTypes: [
'com.apple.UIKit.activity.PostToTwitter'
]
})
}}>
<Text >{value.replace("- null","")}</Text>
</TouchableOpacity>
);
change_order_status = (value,id)=>{
if(value == 1){
fetch(`${Server.dest}/api/delivering-order?id=${id}`, {
headers: { 'Cache-Control': 'no-cache' }
})
.then(res => res.json())
.then(resJson => {
});
}
else {
AsyncStorage.getItem('storeid').then((store_id) => {
fetch(`${Server.dest}/api/delivered-order?id=${id}&store_id=${store_id}&user_id=${store_id}`, {
headers: { 'Cache-Control': 'no-cache' }
})
.then(res => res.json())
.then(resJson => {
});
})
}
}
order_status_changer = (value,id, rowNum) => {
let TheSelect = null;
function resetText (oldOrderState) {
setTimeout(() => {
TheSelect.setSelectedText(selectLabels[oldOrderState])
}, 0)
}
return (
<Select
ref={(c) => TheSelect = c}
defaultText={selectLabels[value]}
style = {{borderWidth:0}}
textStyle = {{}}
backdropStyle = {{backgroundColor : "white"}}
optionListStyle = {{backgroundColor : "white"}}
onSelect={(itemValue, label) => {
let oldOrderState = this.state.orderStates[rowNum];
//console.log(this.state.orderStates);
//console.log('old', oldOrderState, 'new', itemValue);
if (itemValue == oldOrderState)
Alert.alert('لا يمكن تغيير الحالة', 'هذا الطلب بالفعل فى هذه الحالة')
else if (itemValue < oldOrderState) {
Alert.alert('لا يمكن تغيير الحالة', 'لا يمكنك ارجاع حالة الطلب.')
resetText(oldOrderState)
}
else {
//console.log('success')
// Make a copy of the order states array
let copy_orderStates = [...this.state.orderStates];
// Make a copy of the target order state
let orderState = copy_orderStates[rowNum];
/// Change order state
orderState = itemValue;
// Update our copy of order states array
copy_orderStates[rowNum] = orderState;
// Update component's state
this.setState({ orderStates: copy_orderStates });
// Send to server
this.change_order_status(itemValue,id)
}}} >
<Option value='0'>تم القبول</Option>
<Option value='1'>جاري التوصيل</Option>
<Option value='2'>تم التوصيل</Option>
</Select>
)
};
render() {
const tableHead = ['النوع','الوصف'];
const tableData = [
['' + this.state.note, 'الملاحظات'],
['' + this.state.details, 'التفاصيل'],
['' + this.state.clientName, 'اسم العميل'],
['' + this.state.phone, 'هاتف العميل'],
['' + this.state.price, ' المبلغ الاجمالى'],
['' + this.state.discounted, 'المبلغ المخصوم'],
['' + this.state.delivery_cost, 'سعر التوصيل'],
['' + this.state.price-this.state.discounted, ' السعر الاجمالى بعد الخصم'],
];
if (this.state.owner_login == '0') {
return (
<View
style={{
backgroundColor: '#FFFFFF',
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
alignItems: 'center',
height: Dimensions.get('window').height,
width: Dimensions.get('window').width
}}
>
<Image
style={{
flex: 1,
height: '35%',
width: Dimensions.get('window').width,
marginTop:20
}}
resizeMode="cover"
source={require('../../assets/images/splash.jpg')}
/>
<KeyboardAvoidingView
behavior="padding"
keyboardVerticalOffset={140}
style={{ flex: 1 }}
contentContainerStyle={{
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
alignItems: 'center',
width: Dimensions.get('window').width
}}
>
<View style={styles.inputsContainer}>
{this.shouldRenderErrorMessage()}
<View style={styles.singleInputContainer}>
<TextInput
underlineColorAndroid="transparent"
placeholder="اسم الدخول للمحل التجاري"
placeholderTextColor="#CCCCCC"
autoGrow={false}
multiline={false}
autoFocus={false}
style={styles.textInput}
defaultValue={this.state.passname}
onChangeText={text => this.setState({ passname: text })}
onSubmitEditing={event => this.loginOwner()}
/>
<Ionicons
name={Platform.OS === 'ios' ? 'ios-people' : 'md-people'}
size={26}
color={Colors.fadedMainColor}
style={styles.inputIcon}
/>
</View>
<View style={styles.singleInputContainer}>
<TextInput
underlineColorAndroid="transparent"
placeholder="كلمة المرور"
placeholderTextColor="#CCCCCC"
autoGrow={false}
multiline={false}
autoFocus={false}
secureTextEntry={true}
style={styles.textInput}
onChangeText={text => this.setState({ password: text })}
onSubmitEditing={event => this.loginOwner()}
/>
<Ionicons
name={Platform.OS === 'ios' ? 'ios-lock' : 'md-lock'}
size={26}
color={Colors.fadedMainColor}
style={styles.inputIcon}
/>
</View>
</View>
</KeyboardAvoidingView>
<View style={styles.signupButtonContainer}>
<View style={{ flex: 1, marginBottom: 13, width: '100%' }}>
<Button
onPress={() => {
this.loginOwner();
}}
color="white"
backgroundColor={Colors.mainColor}
borderRadius={15}
buttonStyle={{ padding: 10 }}
containerViewStyle={{
marginLeft: 0,
width: '100%',
borderRadius: 15
}}
textStyle={{ fontFamily: 'myfont' }}
title="تسجيل دخول التاجر"
/>
</View>
</View>
</View>
);
} else if (!this.state.storeOrdersFetched) {
return <LoadingIndicator size="large" />;
} else {
return (
<View style={{ height: '100%',marginTop:20 }}>
<ScrollView>
<View style={{ flex: 1, marginBottom: 15 }}>
<Button
onPress={() => {
this.setOwnerLoginStatus('0');
}}
color="white"
backgroundColor={Colors.mainColor}
borderRadius={15}
buttonStyle={{ padding: 10 }}
containerViewStyle={{
marginTop: 15,
marginHorizontal: 7,
borderRadius: 15
}}
textStyle={{ fontFamily: 'myfont' }}
title="تسجيل خروج من المتجر"
/>
</View>
<Modal
visible={this.state.modalVisible}
animationType={'slide'}
onRequestClose={() => this.closeModal()}
>
<View style={styles.modalContainer}>
<View style={styles.innerContainer}>
<ScrollView>
<View style={{ paddingRight: 10, paddingLeft: 10 }}>
<TouchableOpacity onPress={()=>{
this.closeModal()
}} style={{width:350,backgroundColor:Colors.mainColor,justifyContent:'center',alignItems:'center',color:'white',padding:10,marginRight:20,marginBottom:20,marginTop:20}} >
<Text style={{textAlign:'center',justifyContent:'center',alignItems:'center',color:'white'}}>اغلاق</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{
this.share_order()
}} style={{width:350,backgroundColor:Colors.mainColor,justifyContent:'center',alignItems:'center',color:'white',padding:10,marginRight:20,marginBottom:20,marginTop:20}} >
<Text style={{textAlign:'center',justifyContent:'center',alignItems:'center',color:'white'}}>مشاركه الطلب</Text>
</TouchableOpacity>
<Table
borderStyle={{
borderWidth: 0.5,
borderColor: Colors.fadedMainColor,
width:'100%',
paddingTop:10
}}
>
<Row
data={tableHead}
style={styles.head}
textStyle={styles.text}
/>
<Rows
data={tableData}
style={styles.row}
textStyle={styles.text2}
/>
</Table>
<FlatList
automaticallyAdjustContentInsets={false}
style={{ backgroundColor: 'white' }}
removeClippedSubviews={false}
ItemSeparatorComponent={() => (
<View style={{ height: 5, backgroundColor: Colors.smoothGray }} />
)}
data={this.state.detailed_order}
renderItem={({ item }) => (
<TouchableOpacity
>
<OrderDetailBox
style={styles.restaurant}
name={item.name}
desc={item.desc}
image={item.img}
price={item.cost}
count={item.count}
/>
</TouchableOpacity>
)}
/>
</View>
</ScrollView>
</View>
</View>
</Modal>
<Table
style={styles.table}
borderStyle={{ borderWidth: 0.5, borderColor: Colors.mainColor }}
>
<Row
data={['بيانات الزبون', 'الموقع', 'السعر', 'التفاصيل', 'الحالة']}
style={styles.head}
textStyle={styles.headText}
flexArr={[2, 2, 1, 2, 2]}
/>
<Rows
data={this.state.orders}
style={styles.row}
textStyle={styles.text}
flexArr={[2, 2, 1, 1, 2]}
/>
</Table>
</ScrollView>
</View>
);
}
}
}
const styles = StyleSheet.create({
table: { flex: 1, marginTop: 20 },
head: { height: 70, backgroundColor: Colors.mainColor },
headText: { fontFamily: 'myfont', textAlign: 'center', color: 'white' },
text: {
paddingVertical: 10,
textAlign: 'center',
color: Colors.secondaryColor
},
row: { height: 100, backgroundColor: 'white' },
textInput: {
flex: 1,
color: Colors.mainColor,
textAlign: 'right',
fontFamily: 'myfont',
padding: 9,
borderRadius: 4,
backgroundColor: 'transparent',
borderBottomColor: Colors.fadedMainColor,
borderBottomWidth: 0.7
},
inputIcon: {
backgroundColor: 'transparent',
marginLeft: 9
},
inputsContainer: {
flex: 0.8,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
width: '90%'
},
singleInputContainer: {
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center'
},
signupButtonContainer: {
flex: 0.3,
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
width: '92%'
},
restaurant: {
backgroundColor: 'white',
flex: 1,
padding: 100
},
text2: {
fontFamily: 'myfont',
fontSize: 13,
color: Colors.secondaryColor,
textAlign: 'center'
},
container: {
flex: 1,
justifyContent: 'center'
},
modalContainer: {
flex: 1,
justifyContent: 'center',
backgroundColor: 'grey'
},
innerContainer: {
marginTop:20,
flex:1
},
});