11 import React, {FC, ReactElement, useState} from 'react';
22 import {
33 Alert,
44 Image,
55 View,
66 Platform,
77 ScrollView,
88 StyleSheet,
99 } from 'react-native';
1010 import Parse from 'parse/react-native';
1111 import {
1212 List,
1313 Title,
1414 TextInput as PaperTextInput,
1515 Button as PaperButton,
1616 Text as PaperText,
1717 } from 'react-native-paper';
1818
1919 export const MovieRatings: FC<{}> = ({}): ReactElement => {
2020
2121 const [queryResults, setQueryResults] = useState(null);
2222 const [ratingsAverage, setRatingsAverage] = useState('');
2323 const [reviewText, setReviewText] = useState('');
2424 const [reviewRate, setReviewRate] = useState('');
2525
2626 const runGetMovieAverageRating = async function (): Promise<boolean> {
2727 try {
2828 const params: {movie: string} = {
2929 movie: 'Mission Very Possible',
3030 };
3131 let resultObject: {result: number} = await Parse.Cloud.run(
3232 'getMovieAverageRating',
3333 params,
3434 );
3535
3636 setRatingsAverage(resultObject.result.toFixed(1));
3737 return true;
3838 } catch (error) {
3939
4040
4141 Alert.alert(
4242 'Error!',
4343 'Make sure that the cloud function is deployed and that the Review class table is created',
4444 );
4545 return false;
4646 }
4747 };
4848
4949 const doReviewQuery = async function (): Promise<boolean> {
5050
5151 let parseQuery: Parse.Query = new Parse.Query('Review');
5252 try {
5353 let results: [Parse.Object] = await parseQuery.find();
5454
5555 setQueryResults(results);
5656 return true;
5757 } catch (error) {
5858
5959 Alert.alert('Error!', error.message);
6060 return false;
6161 }
6262 };
6363
6464 const createReview = async function (): Promise<boolean> {
6565 try {
6666
6767
6868 const reviewTextValue: string = reviewText;
6969 const reviewRateValue: number = Number(reviewRate);
7070
7171
7272 let Review: Parse.Object = new Parse.Object('Review');
7373
7474
7575
7676 Review.set('text', reviewTextValue);
7777
7878
7979 Review.set('rate', reviewRateValue);
8080
8181
8282 Review.set('movie', 'Mission Very Possible');
8383
8484
8585 try {
8686 await Review.save();
8787
8888 Alert.alert('Success!');
8989
9090 doReviewQuery();
9191 runGetMovieAverageRating();
9292 return true;
9393 } catch (error) {
9494
9595 Alert.alert('Error!', error.message);
9696 return false;
9797 }
9898 } catch (error) {
9999
100100 Alert.alert('Error!', error.message);
101101 return false;
102102 }
103103 };
104104
105105 return (
106106 <>
107107 <View style={Styles.header}>
108108 <Image
109109 style={Styles.header_logo}
110110 source={ {
111111 uri:
112112 'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png',
113113 } }
114114 />
115115 <PaperText style={Styles.header_text}>
116116 <PaperText style={Styles.header_text_bold}>
117117 {'React Native on Back4App - '}
118118 </PaperText>
119119 {' Cloud Code Movie Ratings'}
120120 </PaperText>
121121 </View>
122122 <ScrollView style={Styles.wrapper}>
123123 <View>
124124 <Title>{'Mission Very Possible Reviews'}</Title>
125125 <PaperText>{`Ratings Average: ${ratingsAverage}`}</PaperText>
126126 {}
127127 {queryResults !== null &&
128128 queryResults !== undefined &&
129129 queryResults.map((result: Parse.Object) => (
130130 <List.Item
131131 key={result.id}
132132 title={`Review text: ${result.get('text')}`}
133133 description={`Rate: ${result.get('rate')}`}
134134 titleStyle={Styles.list_text}
135135 style={Styles.list_item}
136136 />
137137 ))}
138138 {queryResults === null ||
139139 queryResults === undefined ||
140140 (queryResults !== null &&
141141 queryResults !== undefined &&
142142 queryResults.length <= 0) ? (
143143 <PaperText>{'No results here!'}</PaperText>
144144 ) : null}
145145 </View>
146146 <View>
147147 <Title>Action Buttons</Title>
148148 <PaperButton
149149 onPress={() => runGetMovieAverageRating()}
150150 mode="contained"
151151 icon="search-web"
152152 color={'#208AEC'}
153153 style={Styles.list_button}>
154154 {'Calculate Review Average'}
155155 </PaperButton>
156156 <PaperButton
157157 onPress={() => doReviewQuery()}
158158 mode="contained"
159159 icon="search-web"
160160 color={'#208AEC'}
161161 style={Styles.list_button}>
162162 {'Query Reviews'}
163163 </PaperButton>
164164 </View>
165165 <View>
166166 <Title>Add new review</Title>
167167 <PaperTextInput
168168 value={reviewText}
169169 onChangeText={text => setReviewText(text)}
170170 label="Text"
171171 mode="outlined"
172172 style={Styles.form_input}
173173 />
174174 <PaperTextInput
175175 value={reviewRate}
176176 onChangeText={text => setReviewRate(text)}
177177 keyboardType={'number-pad'}
178178 label="Rate (1-5)"
179179 mode="outlined"
180180 style={Styles.form_input}
181181 />
182182 <PaperButton
183183 onPress={() => createReview()}
184184 mode="contained"
185185 icon="plus"
186186 style={Styles.submit_button}>
187187 {'Add'}
188188 </PaperButton>
189189 </View>
190190 </ScrollView>
191191 </>
192192 );
193193 };
194194
195195
196196 const Styles = StyleSheet.create({
197197 header: {
198198 alignItems: 'center',
199199 paddingTop: 30,
200200 paddingBottom: 50,
201201 backgroundColor: '#208AEC',
202202 },
203203 header_logo: {
204204 height: 50,
205205 width: 220,
206206 resizeMode: 'contain',
207207 },
208208 header_text: {
209209 marginTop: 15,
210210 color: '#f0f0f0',
211211 fontSize: 16,
212212 },
213213 header_text_bold: {
214214 color: '#fff',
215215 fontWeight: 'bold',
216216 },
217217 wrapper: {
218218 width: '90%',
219219 alignSelf: 'center',
220220 },
221221 list_button: {
222222 marginTop: 6,
223223 marginLeft: 15,
224224 height: 40,
225225 },
226226 list_item: {
227227 borderBottomWidth: 1,
228228 borderBottomColor: 'rgba(0, 0, 0, 0.12)',
229229 },
230230 list_text: {
231231 fontSize: 15,
232232 },
233233 form_input: {
234234 height: 44,
235235 marginBottom: 16,
236236 backgroundColor: '#FFF',
237237 fontSize: 14,
238238 },
239239 submit_button: {
240240 width: '100%',
241241 maxHeight: 50,
242242 alignSelf: 'center',
243243 backgroundColor: '#208AEC',
244244 },
245245 });