Advertisement
Guest User

MoreScreen.js

a guest
Apr 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. import React from 'react';
  2. import PlatformTouchable from 'react-native-platform-touchable';
  3. import axios from 'axios';
  4. import Touchable from 'react-native-platform-touchable';
  5. import DateTimePicker from 'react-native-modal-datetime-picker';
  6. import firebase from 'firebase';
  7. import {
  8. Text,
  9. View,
  10. SafeAreaView,
  11. TouchableOpacity,
  12. TextInput,
  13. StyleSheet,
  14. Picker,
  15. ScrollView,
  16. Alert,
  17. } from 'react-native';
  18. import { Ionicons } from '@expo/vector-icons'; // 6.2.2
  19. import { StackNavigator, TabNavigator, TabBarBottom } from 'react-navigation';
  20. import { Button } from 'react-native-elements';
  21. import styles from '.././styles/styles';
  22. import createIconSetFromFontello from '@expo/vector-icons/createIconSetFromFontello';
  23.  
  24.  
  25. const INITIAL_TIME = new Date();
  26. class MoreScreen extends React.Component {
  27.  
  28. componentWillMount() {
  29.  
  30. //Reading the location data from firebase
  31. const ref = firebase.database().ref('locationMap');
  32. ref.on('value', (snapshot) => { this.setState({ DATA_RETURNED: snapshot.val() }); });
  33. }
  34. static navigationOptions = {
  35. tabBarLabel: '',
  36. headerTitle: <Text style={styles.headertitle}>Create Appointment</Text>,
  37. headerStyle: {
  38. elevation: 0.4,
  39. shadowOpacity: 0.4
  40. }
  41. };
  42. state = {
  43. name: '',
  44. num: '',
  45. email: '',
  46. Time: INITIAL_TIME,
  47. location: '',
  48. DATA_RETURNED: { markers: [], region: {} },
  49. showPicker: false,
  50. showPicker2: false,
  51.  
  52. };
  53.  
  54. setDate = (newDate) => {
  55. this.setState({ Time: newDate });
  56. this.setState({ showPicker: false });
  57. }
  58.  
  59. handleTime = (text) => {
  60. this.setState({ Time: text });
  61. }
  62.  
  63. /*
  64. This is the method used to implement the login feature
  65. We have the different variables whoes value we get from the state object
  66. Then we try to push those values to our firebase database
  67. It will give the optimal message depending upon the result
  68. */
  69.  
  70.  
  71. login = () => {
  72. const { name, num, email, Time, location } = this.state;
  73. const localDate = Time.toLocaleDateString();
  74. const localTime = Time.toLocaleTimeString();
  75. const apptDate = localDate;
  76. const time = localTime;
  77. const customerName = name;
  78. const emailId = email;
  79. const phoneNumber = num;
  80. //It will not allow login when any of the fields is unaddressed
  81. if (this.state.name === '' || this.state.num === ' ' || this.state.email === '' || this.state.Time === INITIAL_TIME || this.state.location === '') {
  82. //Alert.alert('Cannot Login');
  83. if(this.state.name === '') {
  84. Alert.alert('Enter your name in the name field');
  85. }
  86. else if(this.state.num === '') {
  87. Alert.alert('Enter your phone number in the phone number field');
  88. }
  89. else if(this.state.email === '') {
  90. Alert.alert('Enter your email in the email field');
  91. }
  92. else if (this.state.Time === INITIAL_TIME) {
  93. Alert.alert('Choose a different time');
  94. }
  95. else if (this.state.location === '') {
  96. Alert.alert('Choose a location');
  97. }
  98. return;
  99. }
  100. //Pushing the information to firebase
  101. firebase.database().ref('/customers/')
  102. .push({ customerName, phoneNumber, emailId, apptDate, time, location })
  103. .then(() => {
  104. Alert.alert('Your appointment request has been received');
  105. //Reset the information
  106. this.setState({ name: '', num: '', email: '', Time: INITIAL_TIME, location: '' });
  107. })
  108. .catch(() => {
  109. if (this.state.name !== '') {
  110. Alert.alert('Appointment can not be created');
  111. }
  112. });
  113. this.setState({ showPicker2: false });
  114. };
  115.  
  116. showSlotPicker = () => {
  117. this.setState({ showPicker: true });
  118. };
  119.  
  120. hideSlotPicker = () => {
  121. this.setState({ showPicker: false });
  122. };
  123.  
  124. handleDatePicked = (date) => {
  125. this.setDate(date);
  126. };
  127.  
  128. render() {
  129. const chooseTime =
  130. this.state.Time !== INITIAL_TIME ? this.state.Time.toString() : 'Choose a time';
  131. const chooseLocation =
  132. this.state.location ? this.state.location : 'Choose a location';
  133. const size = this.state.showPicker === false
  134. ? { height: 0, width: 0, alignSelf: 'center' } : { width: 300, alignSelf: 'center' };
  135. const size2 = this.state.showPicker2 === false ? { height: 0, width: 0 } : { flex: 1 };
  136. return (
  137. <View style={{ backgroundColor: 'white', flex: 1 }}>
  138. <ScrollView>
  139. <Text style={styles.requestTitle}>Request an appointment</Text>
  140.  
  141. <TextInput
  142. style={myStyles.input}
  143. underlineColorAndroid="transparent"
  144. placeholder="Name"
  145. placeholderTextColor="#dddddd"
  146. autoCapitalize="none"
  147. value={this.state.name}
  148. onChangeText={name => this.setState({ name })}
  149. />
  150. <TextInput
  151. textContentType="telephoneNumber"
  152. style={myStyles.input}
  153. underlineColorAndroid="transparent"
  154. placeholder="Phone Number"
  155. placeholderTextColor="#dddddd"
  156. keyboardType='number-pad'
  157. autoCapitalize="none"
  158. value={this.state.num}
  159. onChangeText={num => this.setState({ num })}
  160. />
  161. <TextInput
  162. style={myStyles.input}
  163. textContentType="emailAddress"
  164. underlineColorAndroid="transparent"
  165. placeholder="Email"
  166. placeholderTextColor="#dddddd"
  167. keyboardType='email-address'
  168. autoCapitalize="none"
  169. value={this.state.email}
  170. onChangeText={email => this.setState({ email })}
  171. />
  172.  
  173.  
  174. <Touchable
  175. onPress={this.showSlotPicker}
  176. style={styles.button}
  177. >
  178. <View>
  179. <Text style={styles.cardtext}>
  180. {chooseTime}
  181. </Text>
  182. </View>
  183. </Touchable>
  184.  
  185. <View style={size}>
  186. <DateTimePicker
  187. isVisible={this.state.showPicker}
  188. onConfirm={this.handleDatePicked}
  189. onCancel={this.hideSlotPicker}
  190. mode='datetime'
  191. />
  192. </View>
  193.  
  194.  
  195. <Touchable
  196. onPress={showPicker2 => this.setState({ showPicker2: !this.state.showPicker2 })}
  197. style={[styles.button, { backgroundColor: '#666666' }]}
  198. >
  199. <View>
  200. <Text style={styles.cardtext}>
  201. {chooseLocation}
  202. </Text>
  203. </View>
  204. </Touchable>
  205. <View>
  206. <Picker
  207. selectedValue={this.state.location}
  208. style={size2}
  209. onValueChange={(itemValue, itemIndex) => this.setState({ location: itemValue })}
  210. >
  211. {this.state.DATA_RETURNED.markers.map(
  212. Location =>
  213. <
  214. Picker.Item
  215. key={Location.title}
  216. label={Location.title}
  217. value={Location.title}
  218. />
  219. )}
  220. </Picker>
  221. </View>
  222. <Touchable
  223. onPress={this.login}
  224. style={styles.submitButton}
  225. >
  226. <View>
  227. <Text style={styles.cardtext}>
  228. Sign Up
  229. </Text>
  230. </View>
  231. </Touchable>
  232. </ScrollView>
  233. </View>
  234. );
  235. }
  236. }
  237.  
  238. const myStyles = StyleSheet.create({
  239. container: {
  240. paddingTop: 23
  241. },
  242. title: {
  243. backgroundColor: 'transparent',
  244. padding: 10,
  245. fontSize: 22,
  246. textAlign: 'center',
  247. },
  248. input: {
  249. margin: 15,
  250. padding: 5,
  251. height: 40,
  252. borderColor: '#aaaaaa',
  253. borderRadius: 4,
  254. borderWidth: 1,
  255. fontFamily: 'mainFont'
  256. },
  257. submitButton: {
  258. backgroundColor: '#7a42f4',
  259. padding: 10,
  260. margin: 15,
  261. height: 40,
  262. },
  263. submitButtonText: {
  264. color: 'white'
  265. }
  266. });
  267.  
  268. export default MoreScreen;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement