-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
executable file
·826 lines (792 loc) · 22.7 KB
/
Copy pathApp.js
File metadata and controls
executable file
·826 lines (792 loc) · 22.7 KB
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
Button,
Picker,
TextInput,
Alert,
Platform,
ActionSheetIOS,
Image,
ImageBackground,
SafeAreaView,
} from 'react-native';
import {
WLJSONStore,
JSONStoreCollection,
JSONStoreQueryPart,
JSONStoreInitOptions,
JSONStoreSyncPolicy,
JSONStoreAddOptions,
} from 'react-native-ibm-mobilefirst-jsonstore';
import AwesomeButton from 'react-native-really-awesome-button';
import AwesomeButtonRick from 'react-native-really-awesome-button/src/themes/rick';
const SUCCESS = 'SUCCESS';
const FAILURE = 'FAILURE';
const BUTTONS_IOS_ACTIONSHEET_LABELS = [
'Initialize Collection',
'Add Document',
'Remove Document',
'Find Document',
'Count Document',
'Clear Collection',
'Remove Collection',
];
const BUTTONS_IOS_ACTIONSHEET_OPERATIONS = [
'initCollection',
'add',
'remove',
'find',
'count',
'clearCollection',
'removeCollection',
];
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
operation: BUTTONS_IOS_ACTIONSHEET_OPERATIONS[0],
addDataModel: {
name: '',
age: '',
},
removeDataModel: {
id: '',
},
findQueryModel: {
query: '',
},
countQueryModel: {
query: '',
},
result: '',
iosButtonTitle: BUTTONS_IOS_ACTIONSHEET_LABELS[0],
};
// views
this.returnPickerView = this.returnPickerView.bind(this);
this.returnOperationDetailView = this.returnOperationDetailView.bind(this);
this.changePickerItem = this.changePickerItem.bind(this);
// operations
this.initialize = this.initialize.bind(this);
this.addDocument = this.addDocument.bind(this);
this.removeDocument = this.removeDocument.bind(this);
this.findDocuments = this.findDocuments.bind(this);
this.findDocumentsByAge = this.findDocumentsByAge.bind(this);
this.findDocumentsByName = this.findDocumentsByName.bind(this);
this.findDocumentById = this.findDocumentById.bind(this);
this.countDocuments = this.countDocuments.bind(this);
this.countDocumentsByAge = this.countDocumentsByAge.bind(this);
this.countDocumentsByName = this.countDocumentsByName.bind(this);
this.clearCollection = this.clearCollection.bind(this);
this.removeCollection = this.removeCollection.bind(this);
this.showActionSheet = this.showActionSheet.bind(this);
}
initialize() {
var collection = new JSONStoreCollection('people');
collection.setSearchField('name', 'string');
collection.setSearchField('age', 'number');
var options = new JSONStoreInitOptions();
options.setSyncOptions(
JSONStoreSyncPolicy.SYNC_UPSTREAM,
'JSONStoreCloudantSync',
);
WLJSONStore.openCollections(['people'], options)
// WLJSONStore.openCollections(["people"])
.then(res => {
this.setState({result: SUCCESS + '\n' + res});
})
.catch(err => {
this.setState({result: FAILURE + '\n' + err});
});
}
addDocument() {
var name = this.state.addDataModel.name.trim();
var age = this.state.addDataModel.age.trim();
if (name.length > 0 && age.length > 0) {
if (isNaN(Number(age))) {
Alert.alert('Please enter a valid Age.');
}
var data = {
name: this.state.addDataModel.name.trim(),
age: Number(this.state.addDataModel.age.trim()),
};
var options = new JSONStoreAddOptions(true);
var collection = new JSONStoreCollection('people');
collection
.addData(data, options)
.then(res => {
this.setState({
result:
SUCCESS +
'\nDocument ' +
JSON.stringify(data) +
' added successfully.',
addDataModel: {name: '', age: ''},
});
})
.catch(err => {
this.setState({
result: FAILURE + '\n' + err,
addDataModel: {name: '', age: ''},
});
});
}
}
removeDocument() {
if (this.state.removeDataModel.id.length > 0) {
var id = Number(this.state.removeDataModel.id);
var collection = new JSONStoreCollection('people');
collection
.removeDocumentById(id)
.then(res => {
if (res == 1) {
this.setState({
result:
SUCCESS + '\nDocument with id ' + id + ' successully removed.',
removeDataModel: {id: ''},
});
} else {
this.setState({
result: SUCCESS + '\nDocument with id ' + id + " doesn't exist.",
removeDataModel: {id: ''},
});
}
})
.catch(err => {
this.setState({
result: FAILURE + '\n' + err,
removeDataModel: {id: ''},
});
});
}
}
countDocuments() {
var collection = new JSONStoreCollection('people');
collection
.countAllDocuments()
.then(res => {
this.setState({
result: SUCCESS + '\nTotal Count of Documents present: ' + res,
countQueryModel: {query: ''},
});
})
.catch(err => {
this.setState({
result: FAILURE + '\n' + err,
countQueryModel: {query: ''},
});
});
}
countDocumentsByName() {
var name = this.state.countQueryModel.query.trim();
if (name.length == 0) {
Alert.alert('Please enter a valid Name.');
return;
}
var collection = new JSONStoreCollection('people');
var query = new JSONStoreQueryPart();
query.addLike('name', name);
collection
.countDocuments([query])
.then(res => {
this.setState({
result:
SUCCESS +
'\nCount of Documents found with "name" containing: ' +
name +
'\n is: ' +
res,
countQueryModel: {query: ''},
});
})
.catch(err => {
this.setState({
result: FAILURE + '\n' + err,
countQueryModel: {query: ''},
});
});
}
countDocumentsByAge() {
var age = this.state.countQueryModel.query.trim();
if (age.length == 0 || isNaN(Number(age))) {
Alert.alert('Please enter a valid Age.');
return;
}
var collection = new JSONStoreCollection('people');
var query = new JSONStoreQueryPart();
query.addEqual('age', Number(age));
collection
.countDocuments([query])
.then(res => {
this.setState({
result:
SUCCESS +
'\nCount of Documents found with "age": ' +
age +
'\n is: ' +
res,
countQueryModel: {query: ''},
});
})
.catch(err => {
this.setState({
result: FAILURE + '\n' + err,
countQueryModel: {query: ''},
});
});
}
findDocuments() {
var collection = new JSONStoreCollection('people');
collection
.findAllDocuments()
.then(res => {
this.setState({
result: SUCCESS + '\nAll Documents: ' + '\n' + JSON.stringify(res),
findQueryModel: {query: ''},
});
})
.catch(err => {
this.setState({
result: FAILURE + '\n' + err,
findQueryModel: {query: ''},
});
});
}
findDocumentsByAge() {
var age = this.state.findQueryModel.query.trim();
if (age.length == 0 || isNaN(Number(age))) {
Alert.alert('Please enter a valid Age.');
return;
}
var collection = new JSONStoreCollection('people');
var query = new JSONStoreQueryPart();
query.addEqual('age', Number(age));
collection
.findDocuments([query])
.then(res => {
this.setState({
result:
SUCCESS +
'\nDocuments found with "age": ' +
age +
'\n' +
JSON.stringify(res),
findQueryModel: {query: ''},
});
})
.catch(err => {
this.setState({
result: FAILURE + '\n' + err,
findQueryModel: {query: ''},
});
});
}
findDocumentsByName() {
var name = this.state.findQueryModel.query.trim();
if (name.length == 0) {
Alert.alert('Please enter a valid Name.');
return;
}
var collection = new JSONStoreCollection('people');
var query = new JSONStoreQueryPart();
query.addLike('name', name);
collection
.findDocuments([query])
.then(res => {
this.setState({
result:
SUCCESS +
'\nDocuments found with "name" containing: ' +
name +
'\n' +
JSON.stringify(res),
findQueryModel: {query: ''},
});
})
.catch(err => {
this.setState({
result: FAILURE + '\n' + err,
findQueryModel: {query: ''},
});
});
}
findDocumentById() {
var id = this.state.findQueryModel.query.trim();
if (id.length == 0 || isNaN(Number(id))) {
Alert.alert('Please enter a valid ID.');
return;
}
var collection = new JSONStoreCollection('people');
collection
.findDocumentById(Number(id))
.then(res => {
if (res == '') {
this.setState({
result: SUCCESS + '\nNo Document found with id: ' + id,
findQueryModel: {query: ''},
});
} else {
this.setState({
result:
SUCCESS +
'\nDocument found with id: ' +
id +
'\n' +
JSON.stringify(res),
findQueryModel: {query: ''},
});
}
})
.catch(err => {
this.setState({
result: FAILURE + '\n' + err,
findQueryModel: {query: ''},
});
});
}
clearCollection() {
var collection = new JSONStoreCollection('people');
collection
.clearCollection()
.then(res => {
this.setState({result: SUCCESS + '\n' + res});
})
.catch(err => {
this.setState({result: FAILURE + '\n' + err});
});
}
removeCollection() {
var collection = new JSONStoreCollection('people');
collection
.removeCollection()
.then(res => {
this.setState({result: SUCCESS + '\n' + res});
})
.catch(err => {
this.setState({result: FAILURE + '\n' + err});
});
}
changePickerItem(itemValue) {
this.setState({
operation: itemValue,
result: '',
});
}
returnPickerView() {
if (Platform.OS === 'ios') {
return (
<View
style={{
borderStyle: 'solid',
borderColor: 'gray',
backgroundColor: 'beige',
borderWidth: 1,
flexDirection: 'row',
justifyContent: 'center',
}}>
<Button
onPress={this.showActionSheet}
color="black"
title={this.state.iosButtonTitle}
/>
<Image
style={{width: 20, height: 20, alignSelf: 'center'}}
source={{
uri:
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADXSURBVGhD7c9NCsIwGIThgCt3LvROeiZBD+AZXOl96nHc6aR0QCSVtvn7IvPCEAJZ5HFKKaWUUqpo6+G02qT/bbEOO/c3e+2wB3bqbyNtMI94DbOGIYL/O2LBVtgd40NLmG/EEztgo1nEzEYwS5jFCGYBE41gNTHJEKwGJoTYY9GVxGRDsBKY7AiWE1MMwXJgiiNYSkw1BEuBqY5gMRgzCLYEYw7B5mDMItgUjHkE+4VpBsFCmAvWFIKFMM0hmMfcsKYR7BPTLIJ5zBVrGqGUUkoppf4i5962ta4MW9O7rQAAAABJRU5ErkJggg==',
}}
/>
</View>
);
} else {
return (
<Picker
selectedValue={this.state.operation}
style={{height: '10%', width: '100%'}}
onValueChange={(itemValue, itemIndex) =>
this.changePickerItem(itemValue)
}>
<Picker.Item label="Initialize Collection" value="initCollection" />
<Picker.Item label="Add Document" value="add" />
<Picker.Item label="Remove Document" value="remove" />
<Picker.Item label="Find Document" value="find" />
<Picker.Item label="Count Document" value="count" />
<Picker.Item label="Clear Collection" value="clearCollection" />
<Picker.Item label="Remove Collection" value="removeCollection" />
</Picker>
);
}
}
showActionSheet() {
ActionSheetIOS.showActionSheetWithOptions(
{
options: BUTTONS_IOS_ACTIONSHEET_LABELS,
},
buttonIndex => {
this.setState({
iosButtonTitle: BUTTONS_IOS_ACTIONSHEET_LABELS[buttonIndex],
operation: BUTTONS_IOS_ACTIONSHEET_OPERATIONS[buttonIndex],
});
},
);
}
returnOperationDetailView() {
// initCollection, add, remove, find, count, clearCollection, removeCollection,
switch (this.state.operation) {
case 'initCollection':
return (
<View
style={{
flex: 1,
alignItems: 'center',
// justifyContent: "center",
width: '100%',
padding: 10,
}}>
{/* <Button
title="Click to Initialize"
color="purple"
onPress={this.initialize}
/> */}
{/* <AwesomeButton
stretch
progress
backgroundColor="#56e016"
backgroundProgress="#31800d"
backgroundShadow="yellow"
borderRadius={200}
// textSize={20}
onPress={next => {
this.initialize();
next();
}}
>
initialize
</AwesomeButton>
<AwesomeButtonRick
type="primary"
// stretch
progress
// textSize={20}
onPress={next => {
this.initialize();
next();
}}
>
Rick's Primary Button
</AwesomeButtonRick> */}
<AwesomeButtonRick
type="secondary"
progress
stretch
onPress={next => {
this.initialize();
next();
}}>
Initialize
</AwesomeButtonRick>
</View>
);
case 'add':
return (
<View
style={{
flex: 1,
width: '100%',
padding: 10,
}}>
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
onChangeText={text =>
this.setState({
addDataModel: {name: text, age: this.state.addDataModel.age},
})
}
value={this.state.addDataModel.name}
placeholder="Enter Name here"
textContentType="name"
/>
<TextInput
style={{
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginVertical: 5,
}}
onChangeText={text =>
this.setState({
addDataModel: {
age: text,
name: this.state.addDataModel.name,
},
})
}
value={this.state.addDataModel.age}
placeholder="Enter Age here"
textContentType="telephoneNumber"
/>
{/* <Button
title="Click to Add Document"
color="purple"
onPress={this.addDocument}
/> */}
<View alignItems="center">
<AwesomeButtonRick
type="primary"
onPress={next => {
this.addDocument();
next();
}}>
Click to Add Document
</AwesomeButtonRick>
</View>
</View>
);
case 'remove':
return (
<View style={{flex: 1, width: '100%', padding: 10}}>
<TextInput
style={{
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginVertical: 5,
}}
onChangeText={text =>
this.setState({removeDataModel: {id: text}})
}
value={this.state.removeDataModel.id}
placeholder="Enter Document id here"
textContentType="telephoneNumber"
/>
{/* <Button
title="Click to Delete Document"
color="purple"
onPress={this.removeDocument}
/> */}
<View alignItems="center">
<AwesomeButtonRick
type="primary"
onPress={next => {
this.removeDocument();
next();
}}>
Click to Delete Document
</AwesomeButtonRick>
</View>
</View>
);
case 'find':
return (
<View style={{flex: 1, width: '100%', padding: 10}}>
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
onChangeText={text =>
this.setState({findQueryModel: {query: text}})
}
value={this.state.findQueryModel.query}
placeholder="Enter query here"
/>
<View
style={{
height: 50,
flexDirection: 'row',
alignContent: 'center',
}}>
<View style={{margin: 5, width: '30%'}}>
<Button title="by ID" onPress={this.findDocumentById} />
</View>
<View style={{margin: 5, width: '31%'}}>
<Button title="by Name" onPress={this.findDocumentsByName} />
</View>
<View style={{margin: 5, width: '31%'}}>
<Button title="by Age" onPress={this.findDocumentsByAge} />
</View>
</View>
<View alignItems="center" style={{margin: 5}}>
{/* <Button
title="Find All Documents"
color="purple"
onPress={this.findDocuments}
/> */}
<AwesomeButtonRick
type="primary"
onPress={next => {
this.findDocuments();
next();
}}>
Find All Documents
</AwesomeButtonRick>
</View>
</View>
);
case 'count':
return (
<View style={{flex: 1, width: '100%', padding: 10}}>
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
onChangeText={text =>
this.setState({countQueryModel: {query: text}})
}
value={this.state.countQueryModel.query}
placeholder="Enter query here"
/>
<View
style={{
height: 50,
flexDirection: 'row',
alignContent: 'center',
}}>
<View style={{margin: 5, width: '48%'}}>
<Button title="by Name" onPress={this.countDocumentsByName} />
</View>
<View style={{margin: 5, width: '48%'}}>
<Button title="by Age" onPress={this.countDocumentsByAge} />
</View>
</View>
<View alignItems="center" style={{margin: 5}}>
{/* <Button
title="get total Documents count"
color="purple"
onPress={this.countDocuments}
/> */}
<AwesomeButtonRick
type="primary"
onPress={next => {
this.countDocuments();
next();
}}>
Get total Documents count
</AwesomeButtonRick>
</View>
</View>
);
case 'clearCollection':
return (
<View
style={{
flex: 1,
width: '100%',
padding: 10,
alignItems: 'center',
}}>
{/* <Button
title="Click to Clear Collection"
color="red"
onPress={this.clearCollection}
/> */}
<AwesomeButtonRick
type="primary"
progress
textColor="white"
backgroundColor="red"
onPress={next => {
this.clearCollection();
next();
}}>
Click to Clear Collection
</AwesomeButtonRick>
</View>
);
case 'removeCollection':
return (
<View
style={{
flex: 1,
width: '100%',
padding: 10,
alignItems: 'center',
}}>
{/* <Button
title="Click to Remove Collection"
color="red"
onPress={this.removeCollection}
/> */}
<AwesomeButtonRick
type="primary"
textColor="white"
backgroundColor="red"
onPress={next => {
this.removeCollection();
next();
}}>
Click to Remove Collection
</AwesomeButtonRick>
</View>
);
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.title}>IBM MobileFirst JSONStore</Text>
<View style={styles.testItemsContainer}>
{this.returnPickerView()}
{this.returnOperationDetailView()}
</View>
<View style={styles.testResultsContainer}>
<Text
style={{
color: 'black',
fontSize: 20,
textAlign: 'center',
marginVertical: 5,
}}>
RESULTS
</Text>
<Text style={{color: 'black', fontSize: 16, margin: 10}}>
{this.state.result}
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
paddingTop: 10,
backgroundColor: '#e1f5e6',
},
testItemsContainer: {
height: '60%',
width: '100%',
},
testResultsContainer: {
flex: 1,
height: '100%',
width: '100%',
backgroundColor: '#c7e8ba',
},
title: {
fontSize: 20,
textAlign: 'center',
marginTop: 30,
borderBottomColor: 'gray',
borderBottomWidth: 1,
paddingBottom: 10,
},
buttonStyle: {
width: '100%',
},
buttonContainer: {
margin: 5,
},
button: {
marginBottom: 10,
fontWeight: '500',
},
charan: {
alignItems: 'center',
},
});