11 import React, {useState} from 'react';
22 import {Alert, Image, View, ScrollView, StyleSheet} from 'react-native';
33 import Parse from 'parse/react-native';
44 import {
55 List,
66 Title,
77 Button as PaperButton,
88 Text as PaperText,
99 } from 'react-native-paper';
1010
1111 export const BookList = () => {
1212
1313 const [queryResults, setQueryResults] = useState(null);
1414
1515 const doQueryByName = async function () {
1616
1717
1818 const parseQuery = new Parse.Query('Profile');
1919
2020
2121
2222 parseQuery.contains('name', 'Adam');
2323
2424 try {
2525 let profiles = await parseQuery.find();
2626 setQueryResults(profiles);
2727 return true;
2828 } catch (error) {
2929
3030 Alert.alert('Error!', error.message);
3131 return false;
3232 }
3333 };
3434
3535 const doQueryByFriendCount = async function () {
3636
3737
3838 const parseQuery = new Parse.Query('Profile');
3939
4040
4141
4242 parseQuery.greaterThan('friendCount', 20);
4343
4444 try {
4545 let profiles = await parseQuery.find();
4646 setQueryResults(profiles);
4747 return true;
4848 } catch (error) {
4949
5050 Alert.alert('Error!', error.message);
5151 return false;
5252 }
5353 };
5454
5555 const doQueryByOrdering = async function () {
5656
5757
5858 const parseQuery = new Parse.Query('Profile');
5959
6060
6161
6262 parseQuery.descending('birthDay');
6363
6464 try {
6565 let profiles = await parseQuery.find();
6666 setQueryResults(profiles);
6767 return true;
6868 } catch (error) {
6969
7070 Alert.alert('Error!', error.message);
7171 return false;
7272 }
7373 };
7474
7575 const doQueryByAll = async function () {
7676
7777
7878 const parseQuery = new Parse.Query('Profile');
7979
8080 parseQuery.contains('name', 'Adam');
8181 parseQuery.greaterThan('friendCount', 20);
8282 parseQuery.descending('birthDay');
8383
8484 try {
8585 let profiles = await parseQuery.find();
8686 setQueryResults(profiles);
8787 return true;
8888 } catch (error) {
8989
9090 Alert.alert('Error!', error.message);
9191 return false;
9292 }
9393 };
9494
9595 const clearQueryResults = async function () {
9696 setQueryResults(null);
9797 return true;
9898 };
9999
100100 return (
101101 <>
102102 <View style={Styles.header}>
103103 <Image
104104 style={Styles.header_logo}
105105 source={ {
106106 uri:
107107 'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png',
108108 } }
109109 />
110110 <PaperText style={Styles.header_text}>
111111 <PaperText style={Styles.header_text_bold}>
112112 {'React Native on Back4App - '}
113113 </PaperText>
114114 {' Basic Queries'}
115115 </PaperText>
116116 </View>
117117 <ScrollView style={Styles.wrapper}>
118118 <View>
119119 <Title>{'Result List'}</Title>
120120 {}
121121 {queryResults !== null &&
122122 queryResults !== undefined &&
123123 queryResults.map((profile) => (
124124 <List.Item
125125 key={profile.id}
126126 title={profile.get('name')}
127127 description={`Friend count: ${profile.get(
128128 'friendCount',
129129 )}, Birthday: ${profile.get('birthDay')}`}
130130 titleStyle={Styles.list_text}
131131 style={Styles.list_item}
132132 />
133133 ))}
134134 {queryResults === null ||
135135 queryResults === undefined ||
136136 (queryResults !== null &&
137137 queryResults !== undefined &&
138138 queryResults.length <= 0) ? (
139139 <PaperText>{'No results here!'}</PaperText>
140140 ) : null}
141141 </View>
142142 <View>
143143 <Title>{'Query buttons'}</Title>
144144 <PaperButton
145145 onPress={() => doQueryByName()}
146146 mode="contained"
147147 icon="search-web"
148148 color={'#208AEC'}
149149 style={Styles.list_button}>
150150 {'Query by name'}
151151 </PaperButton>
152152 <PaperButton
153153 onPress={() => doQueryByFriendCount()}
154154 mode="contained"
155155 icon="search-web"
156156 color={'#208AEC'}
157157 style={Styles.list_button}>
158158 {'Query by friend count'}
159159 </PaperButton>
160160 <PaperButton
161161 onPress={() => doQueryByOrdering()}
162162 mode="contained"
163163 icon="search-web"
164164 color={'#208AEC'}
165165 style={Styles.list_button}>
166166 {'Query by ordering'}
167167 </PaperButton>
168168 <PaperButton
169169 onPress={() => doQueryByAll()}
170170 mode="contained"
171171 icon="search-web"
172172 color={'#208AEC'}
173173 style={Styles.list_button}>
174174 {'Query by all'}
175175 </PaperButton>
176176 <PaperButton
177177 onPress={() => clearQueryResults()}
178178 mode="contained"
179179 icon="delete"
180180 color={'#208AEC'}
181181 style={Styles.list_button}>
182182 {'Clear Results'}
183183 </PaperButton>
184184 </View>
185185 </ScrollView>
186186 </>
187187 );
188188 };
189189
190190
191191 const Styles = StyleSheet.create({
192192 header: {
193193 alignItems: 'center',
194194 paddingTop: 30,
195195 paddingBottom: 50,
196196 backgroundColor: '#208AEC',
197197 },
198198 header_logo: {
199199 height: 50,
200200 width: 220,
201201 resizeMode: 'contain',
202202 },
203203 header_text: {
204204 marginTop: 15,
205205 color: '#f0f0f0',
206206 fontSize: 16,
207207 },
208208 header_text_bold: {
209209 color: '#fff',
210210 fontWeight: 'bold',
211211 },
212212 wrapper: {
213213 width: '90%',
214214 alignSelf: 'center',
215215 },
216216 list_button: {
217217 marginTop: 6,
218218 marginLeft: 15,
219219 height: 40,
220220 },
221221 list_item: {
222222 borderBottomWidth: 1,
223223 borderBottomColor: 'rgba(0, 0, 0, 0.12)',
224224 },
225225 list_text: {
226226 fontSize: 15,
227227 },
228228 });