11 import React, {FC, ReactElement, 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 QueryList: FC<{}> = ({}): ReactElement => {
1212
1313 const [queryResults, setQueryResults] = useState(null);
1414
1515 const doQueryA = async function (): Promise<boolean> {
1616
1717 const PublisherAQuery: Parse.Query = new Parse.Query('Publisher');
1818 PublisherAQuery.equalTo('name', 'Acacia Publishings');
1919 const PublisherA: Parse.Object = await PublisherAQuery.first();
2020
2121
2222 const bookQuery: Parse.Query = new Parse.Query('Book');
2323 bookQuery.equalTo('publisher', PublisherA);
2424
2525 try {
2626 let results: [Parse.Object] = await bookQuery.find();
2727 setQueryResults(results);
2828 return true;
2929 } catch (error) {
3030
3131 Alert.alert('Error!', error.message);
3232 return false;
3333 }
3434 };
3535
3636 const doQueryB = async function (): Promise<boolean> {
3737
3838 const bookQuery: Parse.Query = new Parse.Query('Book');
3939 bookQuery.greaterThan('publishingDate', new Date('01/01/2010'));
4040
4141
4242 const bookStoreQuery: Parse.Query = new Parse.Query('BookStore');
4343 bookStoreQuery.matchesQuery('books', bookQuery);
4444
4545 try {
4646 let results: [Parse.Object] = await bookStoreQuery.find();
4747 setQueryResults(results);
4848 return true;
4949 } catch (error) {
5050
5151 Alert.alert('Error!', error.message);
5252 return false;
5353 }
5454 };
5555
5656 const doQueryC = async function (): Promise<boolean> {
5757
5858 const AuthorAQuery: Parse.Query = new Parse.Query('Author');
5959 AuthorAQuery.equalTo('name', 'Aaron Writer');
6060 const AuthorA: Parse.Object = await AuthorAQuery.first();
6161
6262
6363 const bookQuery: Parse.Query = new Parse.Query('Book');
6464 bookQuery.equalTo('authors', AuthorA);
6565
6666
6767 const bookStoreQuery: Parse.Query = new Parse.Query('BookStore');
6868 bookStoreQuery.matchesQuery('books', bookQuery);
6969
7070 try {
7171 let results: [Parse.Object] = await bookStoreQuery.find();
7272 setQueryResults(results);
7373 return true;
7474 } catch (error) {
7575
7676 Alert.alert('Error!', error.message);
7777 return false;
7878 }
7979 };
8080
8181 const clearQueryResults = async function (): Promise<boolean> {
8282 setQueryResults(null);
8383 return true;
8484 };
8585
8686 return (
8787 <>
8888 <View style={Styles.header}>
8989 <Image
9090 style={Styles.header_logo}
9191 source={ {
9292 uri:
9393 'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png',
9494 } }
9595 />
9696 <PaperText style={Styles.header_text}>
9797 <PaperText style={Styles.header_text_bold}>
9898 {'React Native on Back4App - '}
9999 </PaperText>
100100 {' Relational Queries'}
101101 </PaperText>
102102 </View>
103103 <ScrollView style={Styles.wrapper}>
104104 <View>
105105 <Title>{'Result List'}</Title>
106106 {}
107107 {queryResults !== null &&
108108 queryResults !== undefined &&
109109 queryResults.map((result: Parse.Object) => (
110110 <List.Item
111111 key={result.id}
112112 title={
113113 result.get('name') !== undefined
114114 ? result.get('name')
115115 : result.get('title')
116116 }
117117 titleStyle={Styles.list_text}
118118 style={Styles.list_item}
119119 />
120120 ))}
121121 {queryResults === null ||
122122 queryResults === undefined ||
123123 (queryResults !== null &&
124124 queryResults !== undefined &&
125125 queryResults.length <= 0) ? (
126126 <PaperText>{'No results here!'}</PaperText>
127127 ) : null}
128128 </View>
129129 <View>
130130 <Title>{'Query buttons'}</Title>
131131 <PaperButton
132132 onPress={() => doQueryA()}
133133 mode="contained"
134134 icon="search-web"
135135 color={'#208AEC'}
136136 style={Styles.list_button}>
137137 {'Query A'}
138138 </PaperButton>
139139 <PaperButton
140140 onPress={() => doQueryB()}
141141 mode="contained"
142142 icon="search-web"
143143 color={'#208AEC'}
144144 style={Styles.list_button}>
145145 {'Query B'}
146146 </PaperButton>
147147 <PaperButton
148148 onPress={() => doQueryC()}
149149 mode="contained"
150150 icon="search-web"
151151 color={'#208AEC'}
152152 style={Styles.list_button}>
153153 {'Query C'}
154154 </PaperButton>
155155 <PaperButton
156156 onPress={() => clearQueryResults()}
157157 mode="contained"
158158 icon="delete"
159159 color={'#208AEC'}
160160 style={Styles.list_button}>
161161 {'Clear Results'}
162162 </PaperButton>
163163 </View>
164164 </ScrollView>
165165 </>
166166 );
167167 };
168168
169169
170170 const Styles = StyleSheet.create({
171171 header: {
172172 alignItems: 'center',
173173 paddingTop: 30,
174174 paddingBottom: 50,
175175 backgroundColor: '#208AEC',
176176 },
177177 header_logo: {
178178 height: 50,
179179 width: 220,
180180 resizeMode: 'contain',
181181 },
182182 header_text: {
183183 marginTop: 15,
184184 color: '#f0f0f0',
185185 fontSize: 16,
186186 },
187187 header_text_bold: {
188188 color: '#fff',
189189 fontWeight: 'bold',
190190 },
191191 wrapper: {
192192 width: '90%',
193193 alignSelf: 'center',
194194 },
195195 list_button: {
196196 marginTop: 6,
197197 marginLeft: 15,
198198 height: 40,
199199 },
200200 list_item: {
201201 borderBottomWidth: 1,
202202 borderBottomColor: 'rgba(0, 0, 0, 0.12)',
203203 },
204204 list_text: {
205205 fontSize: 15,
206206 },
207207 });