Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.96 KB | None | 0 0
  1. /**
  2. * Niagara Fitness App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6.  
  7. import React, { Component } from "react";
  8. import {
  9. KeyboardAvoidingView,
  10. TouchableOpacity,
  11. ImageBackground,
  12. AsyncStorage,
  13. BackAndroid,
  14. StyleSheet,
  15. Platform,
  16. StatusBar,
  17. TextInput,
  18. Image,
  19. Text,
  20. View
  21. } from "react-native";
  22.  
  23. import { StackNavigator, NavigationActions } from "react-navigation";
  24. import PushNotificationAndroid from "react-native-push-notification";
  25. import Swiper from "react-native-swiper";
  26.  
  27. import styles from "./assets/global_styles";
  28. import config from "./config";
  29. // import translations from "./lang";
  30.  
  31. import LoadingScreen from "./views/loading";
  32.  
  33. import BackgroundJob from "react-native-background-job";
  34. var PushNotification = require("react-native-push-notification");
  35.  
  36. PushNotification.configure({
  37. // (optional) Called when Token is generated (iOS and Android)
  38. onRegister: function(token) {
  39. console.log("TOKEN:", token);
  40. },
  41.  
  42. // (required) Called when a remote or local notification is opened or received
  43. onNotification: function(notification) {
  44. const { navigate } = this.props.navigation;
  45. navigate("Home", { isLoggedIn: false });
  46. // process the notification
  47.  
  48. // required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
  49. notification.finish(PushNotificationIOS.FetchResult.NoData);
  50. },
  51.  
  52. // ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
  53. senderID: "YOUR GCM SENDER ID",
  54.  
  55. // IOS ONLY (optional): default: all - Permissions to register.
  56. permissions: {
  57. alert: true,
  58. badge: true,
  59. sound: true
  60. },
  61.  
  62. // Should the initial notification be popped automatically
  63. // default: true
  64. popInitialNotification: true,
  65.  
  66. /**
  67. * (optional) default: true
  68. * - Specified if permissions (ios) and token (android and ios) will requested or not,
  69. * - if not, you must call PushNotificationsHandler.requestPermissions() later
  70. */
  71. requestPermissions: true
  72. });
  73.  
  74. async function GetLatestNews() {
  75. console.log("Running in background");
  76. var PushNotification = require("react-native-push-notification");
  77. fetch(
  78. "http://app.niagara.md/api/?ApiToken=eb5a65e694c10a534e53167967daf6f8&action=get_notification"
  79. )
  80. .then(response => response.json())
  81. .then(responseJson => {
  82. if (72 != responseJson.item.notificationID) {
  83. PushNotification.localNotification({
  84. /* Android Only Properties */
  85. foreground: true,
  86. id: "0", // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
  87. ticker: GetNotificationID(), // (optional)
  88. autoCancel: true, // (optional) default: true
  89. largeIcon: "ic_launcher", // (optional) default: "ic_launcher"
  90. smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
  91. bigText: JSON.stringify(responseJson.item.post_content), // (optional) default: "message" prop
  92. subText: "This is a subText", // (optional) default: none
  93. color: "red", // (optional) default: system default
  94. vibrate: true, // (optional) default: true
  95. vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
  96. tag: "some_tag", // (optional) add tag to message
  97. group: "group", // (optional) add group to message
  98. ongoing: true, // (optional) set whether this is an "ongoing" notification
  99.  
  100. /* iOS and Android properties */
  101. title: JSON.stringify(responseJson.item.post_title), // (optional, for iOS this is only used in apple watch, the title will be the app name on other iOS devices)
  102. message: JSON.stringify(responseJson.item.post_content), // (required)
  103. playSound: false, // (optional) default: true
  104. soundName: "default", // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
  105. number: "10", // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
  106. repeatType: "day", // (Android only) Repeating interval. Could be one of `week`, `day`, `hour`, `minute, `time`. If specified as time, it should be accompanied by one more parameter 'repeatTime` which should the number of milliseconds between each interval
  107. actions: false,
  108. date: new Date(Date.now() + 60 * 0) // in 60 secs
  109. });
  110. let notificationID = responseJson.item.notificationID;
  111. SetNotificationID(notificationID);
  112. }
  113. })
  114. .catch(error => {});
  115. }
  116.  
  117. const backgroundJob = {
  118. jobKey: "myJob",
  119. job: () => GetLatestNews()
  120. };
  121.  
  122. BackgroundJob.register(backgroundJob);
  123.  
  124. var backgroundSchedule = {
  125. jobKey: "myJob",
  126. period: 15000,
  127. exact: true,
  128. allowExecutionInForeground: true
  129. };
  130.  
  131. BackgroundJob.schedule(backgroundSchedule);
  132.  
  133. console.disableYellowBox = true;
  134.  
  135. const resetAction = NavigationActions.reset({
  136. index: 0,
  137. actions: [NavigationActions.navigate({ routeName: "Home" })]
  138. });
  139.  
  140. class LoginScreen extends React.Component {
  141. static navigationOptions = {
  142. title: "LoginPage"
  143. };
  144.  
  145. constructor(props) {
  146. super(props);
  147. this.state = {
  148. isLoading: true,
  149. name: "",
  150. password: "",
  151. errorMsg: "",
  152. isLoggedIn: false
  153. };
  154. }
  155.  
  156. FetchLogin = async () => {
  157. const { navigate } = this.props.navigation;
  158.  
  159. return fetch(
  160. config.hostProtocol +
  161. config.hostName +
  162. config.APIBreakPoint +
  163. config.ApiTokenParam +
  164. config.APIToken +
  165. config.actionLogin +
  166. config.usernameParam +
  167. this.state.name +
  168. config.passwordParam +
  169. this.state.password
  170. )
  171. .then(response => response.json())
  172. .then(responseJson => {
  173. this.setState(
  174. {
  175. isLoading: false,
  176. errorMsg: responseJson.message,
  177. userID: responseJson.userID,
  178. userRole: JSON.stringify(responseJson.userRole)
  179. },
  180. async function() {
  181. console.log(responseJson);
  182. if (responseJson.loggedin) {
  183. try {
  184. await AsyncStorage.setItem(
  185. "@USER:isLoggedIn",
  186. JSON.stringify(true)
  187. );
  188. await AsyncStorage.setItem(
  189. "@USER:userID",
  190. JSON.stringify(responseJson.userID)
  191. );
  192. await AsyncStorage.setItem(
  193. "@USER:userRole",
  194. JSON.stringify(responseJson.userRole)
  195. );
  196. } catch (error) {
  197. // Error saving data
  198. }
  199. const { navigate } = this.props.navigation;
  200. this.props.navigation.dispatch(resetAction);
  201. } else {
  202. try {
  203. await AsyncStorage.setItem(
  204. "@USER:isLoggedIn",
  205. JSON.stringify(false)
  206. );
  207. await AsyncStorage.setItem("@USER:userID", responseJson.userID);
  208. } catch (error) {
  209. // Error saving data
  210. }
  211. }
  212. }
  213. );
  214. })
  215. .catch(error => {
  216. this.setState(
  217. {
  218. isLoading: false
  219. },
  220. function() {
  221. // do something with new state
  222. }
  223. );
  224. });
  225. };
  226.  
  227. async componentWillMount() {
  228. try {
  229. let isLoggedIn = await AsyncStorage.getItem("@USER:isLoggedIn");
  230. if (isLoggedIn !== null) {
  231. this.setState({
  232. isLoggedIn: isLoggedIn,
  233. isLoading: false
  234. });
  235. } else {
  236. this.setState({
  237. isLoggedIn: false,
  238. isLoading: false
  239. });
  240. }
  241. } catch (error) {
  242. // Error retrieving data
  243. }
  244. if (this.state.isLoggedIn) {
  245. const { navigate } = this.props.navigation;
  246. this.props.navigation.dispatch(resetAction);
  247. }
  248. }
  249.  
  250. render() {
  251. const { navigate } = this.props.navigation;
  252.  
  253. if (this.state.isLoggedIn && this.state.isLoading) {
  254. return <LoadingScreen />;
  255. }
  256.  
  257. return (
  258. <View style={styles.fullScreenContainer}>
  259. <StatusBar
  260. backgroundColor="transparent"
  261. barStyle="light-content"
  262. translucent
  263. />
  264.  
  265. <KeyboardAvoidingView />
  266. <ImageBackground
  267. source={require("./images/background.jpg")}
  268. style={styles.fullScreenImage}
  269. >
  270. <Image
  271. source={require("./images/logologin.png")}
  272. style={styles.loginLogo}
  273. />
  274. <Text style={styles.smallText}>{this.state.errorMsg}</Text>
  275. <TextInput
  276. placeholder="Phone Number *"
  277. style={styles.inputsText}
  278. onChangeText={text => this.setState({ name: text })}
  279. value={this.state.name}
  280. multiline={true}
  281. placeholderTextColor="white"
  282. underlineColorAndroid="transparent"
  283. returnKeyType="next"
  284. />
  285. <TextInput
  286. placeholder="Password"
  287. style={styles.inputsText}
  288. onChangeText={text => this.setState({ password: text })}
  289. value={this.state.password}
  290. multiline={true}
  291. placeholderTextColor="white"
  292. underlineColorAndroid="transparent"
  293. returnKeyType="go"
  294. secureTextEntry={true}
  295. />
  296. <View style={styles.halfScreenContainer}>
  297. <TouchableOpacity
  298. onPress={this.FetchLogin}
  299. style={styles.halfWidthButton}
  300. >
  301. <Text style={[styles.fullWidthButtonLabel]}>Sign in</Text>
  302. </TouchableOpacity>
  303.  
  304. <TouchableOpacity
  305. onPress={() => {
  306. const { navigate } = this.props.navigation;
  307. navigate("Home", { isLoggedIn: false });
  308. }}
  309. style={styles.halfWidthButtonBlue}
  310. >
  311. <Text style={styles.fullWidthButtonLabel}>Login as quest</Text>
  312. </TouchableOpacity>
  313. </View>
  314. </ImageBackground>
  315. </View>
  316. );
  317. }
  318. }
  319.  
  320. class HomeScreen extends React.Component {
  321. static navigationOptions = {
  322. title: "Welcome"
  323. };
  324.  
  325. constructor(props) {
  326. super(props);
  327. this.state = {
  328. isLoading: true,
  329. JSON_Array: [],
  330. user_sex: "",
  331. user_image:
  332. "https://www.shareicon.net/download/2016/05/29/772558_user_512x512.png",
  333. user_username: ""
  334. };
  335. }
  336.  
  337. async componentWillMount() {
  338. const { params } = this.props.navigation.state;
  339. try {
  340. let userID = await AsyncStorage.getItem("@USER:userID");
  341. let userRole = await AsyncStorage.getItem("@USER:userRole");
  342. let user = await AsyncStorage.getItem("@USER:isLoggedIn");
  343. this.setState({
  344. userID: userID,
  345. userRole: userRole,
  346. JSON_Array: user
  347. });
  348. } catch (error) {
  349. this.setState({
  350. userID: userID,
  351. userRole: "guest",
  352. JSON_Array: user
  353. });
  354. }
  355. fetch(
  356. config.hostProtocol +
  357. config.hostName +
  358. config.APIBreakPoint +
  359. config.ApiTokenParam +
  360. config.APIToken +
  361. config.actionGetUser +
  362. config.userParam +
  363. this.state.userID
  364. )
  365. .then(response => response.json())
  366. .then(responseJson => {
  367. this.setState(
  368. {
  369. isLoading: false,
  370. user_username: responseJson.user.username,
  371. signed_trainer: responseJson.user.signed_trainer.ID,
  372. signed_manager: responseJson.user.signed_manager.ID,
  373. user_username: responseJson.user.username,
  374. user_firstName: responseJson.user.firstName,
  375. user_lastName: responseJson.user.lastName,
  376. offers_num: responseJson.offers_num
  377. },
  378. function() {
  379. // do something with new state
  380. }
  381. );
  382. })
  383. .catch(error => {
  384. // console.error(error);
  385. alert(responseJson);
  386. });
  387. }
  388.  
  389. render() {
  390. if (this.state.isLoggedIn && this.state.isLoading) {
  391. return <LoadingScreen />;
  392. }
  393. if (this.state.userRole == '"subscriber"') {
  394. return (
  395. <View style={styles.fullScreenContainer}>
  396. <ImageBackground
  397. source={require("./images/menu.png")}
  398. style={(styles.fullScreenImage, styles.fullScreenImageLeft)}
  399. >
  400. <StatusBar
  401. backgroundColor="transparent"
  402. barStyle="light-content"
  403. translucent
  404. />
  405. <Image
  406. source={require("./images/logonavigation.png")}
  407. style={styles.navigationLogo}
  408. />
  409.  
  410. <TouchableOpacity style={(styles.container, styles.container_left)}>
  411. <Text style={styles.menuItemContent}>New Offers</Text>
  412. </TouchableOpacity>
  413.  
  414. <TouchableOpacity style={(styles.container, styles.container_left)}>
  415. <Text style={styles.menuItemContent}>Schedule</Text>
  416. </TouchableOpacity>
  417.  
  418. <TouchableOpacity style={(styles.container, styles.container_left)}>
  419. <Text style={styles.menuItemContent}>Trainings</Text>
  420. </TouchableOpacity>
  421.  
  422. <TouchableOpacity style={(styles.container, styles.container_left)}>
  423. <Text style={styles.menuItemContent}>Messages</Text>
  424. </TouchableOpacity>
  425.  
  426. <TouchableOpacity style={(styles.container, styles.container_left)}>
  427. <Text style={styles.menuItemContent}>Spa&Beauty</Text>
  428. </TouchableOpacity>
  429.  
  430. <TouchableOpacity
  431. onPress={async function() {
  432. try {
  433. await AsyncStorage.removeItem("@USER:isLoggedIn");
  434. await AsyncStorage.removeItem("@USER:userID");
  435. await AsyncStorage.removeItem("@USER:userRole");
  436. BackAndroid.exitApp();
  437. } catch (error) {
  438. // Error saving data
  439. }
  440. }}
  441. style={(styles.container, styles.container_left)}
  442. >
  443. <Text style={styles.menuItemContent}>Log Out</Text>
  444. </TouchableOpacity>
  445. </ImageBackground>
  446. </View>
  447. );
  448. }
  449.  
  450. if (this.state.userRole == null) {
  451. return (
  452. <View style={styles.fullScreenContainer}>
  453. <ImageBackground
  454. source={require("./images/menu.png")}
  455. style={(styles.fullScreenImage, styles.fullScreenImageLeft)}
  456. >
  457. <StatusBar
  458. backgroundColor="transparent"
  459. barStyle="light-content"
  460. translucent
  461. />
  462. <Image
  463. source={require("./images/logonavigation.png")}
  464. style={styles.navigationLogo}
  465. />
  466.  
  467. <TouchableOpacity style={(styles.container, styles.container_left)}>
  468. <Text style={styles.menuItemContent}>New Offers</Text>
  469. </TouchableOpacity>
  470.  
  471. <TouchableOpacity style={(styles.container, styles.container_left)}>
  472. <Text style={styles.menuItemContent}>Schedule</Text>
  473. </TouchableOpacity>
  474.  
  475. <TouchableOpacity style={(styles.container, styles.container_left)}>
  476. <Text style={styles.menuItemContent}>Trainings</Text>
  477. </TouchableOpacity>
  478.  
  479. <TouchableOpacity style={(styles.container, styles.container_left)}>
  480. <Text style={styles.menuItemContent}>Spa&Beauty</Text>
  481. </TouchableOpacity>
  482. </ImageBackground>
  483. </View>
  484. );
  485. }
  486.  
  487. return (
  488. <View style={styles.fullScreenContainer}>
  489. <ImageBackground
  490. source={require("./images/menu.png")}
  491. style={(styles.fullScreenImage, styles.fullScreenImageLeft)}
  492. >
  493. <StatusBar
  494. backgroundColor="transparent"
  495. barStyle="light-content"
  496. translucent
  497. />
  498. <Image
  499. source={require("./images/logonavigation.png")}
  500. style={styles.navigationLogo}
  501. />
  502. <Text style={styles.smallText}>{"quests"}</Text>
  503. <TouchableOpacity
  504. onPress={async function() {
  505. try {
  506. await AsyncStorage.removeItem("@USER:isLoggedIn");
  507. BackAndroid.exitApp();
  508. } catch (error) {
  509. // Error saving data
  510. }
  511. }}
  512. style={(styles.container, styles.container_left)}
  513. >
  514. <Text style={styles.menuItemContent}>Log Out</Text>
  515. </TouchableOpacity>
  516. <Text style={styles.menuItemContent}>Guest</Text>
  517. <TouchableOpacity
  518. onPress={() => {
  519. const { navigate } = this.props.navigation;
  520. navigate("Profile", { UserID: this.state.userID });
  521. }}
  522. >
  523. <View style={styles.userInfoBox}>
  524. <View>
  525. <Image
  526. source={{ uri: this.state.user_image }}
  527. style={{ width: 50, height: 50, borderRadius: 100 }}
  528. />
  529. <Text style={styles.menuItemContent}>
  530. {this.state.user_firstName} {this.state.user_lastName}
  531. </Text>
  532. </View>
  533. </View>
  534. </TouchableOpacity>
  535. <TouchableOpacity
  536. style={styles.button}
  537. onPress={() => {
  538. BackgroundJob.schedule({
  539. jobKey: "myJob",
  540. period: 1500
  541. });
  542. }}
  543. >
  544. <Text>Schedule regular job</Text>
  545. </TouchableOpacity>
  546. </ImageBackground>
  547. </View>
  548. );
  549. }
  550. }
  551.  
  552. class ProfileScreen extends React.Component {
  553. constructor(props) {
  554. super(props);
  555. this.state = {};
  556. }
  557.  
  558. async componentWillMount() {
  559. try {
  560. let userID = await AsyncStorage.getItem("@Client:userID");
  561. let user = await AsyncStorage.getItem("@Client:isLoggedIn");
  562. this.setState({
  563. userID: userID,
  564. JSON_Array: user
  565. });
  566. } catch (error) {
  567. // Error saving data
  568. }
  569. fetch(
  570. config.hostProtocol +
  571. config.hostName +
  572. config.APIBreakPoint +
  573. config.ApiTokenParam +
  574. config.APIToken +
  575. config.actionGetUser +
  576. config.userParam +
  577. this.state.userID
  578. )
  579. .then(response => response.json())
  580. .then(responseJson => {
  581. this.setState(
  582. {
  583. isLoading: false,
  584. JSON_Array: responseJson,
  585. user: responseJson.user,
  586. user_sex: responseJson.user.sex,
  587. user_image: responseJson.user.image,
  588. user_username: responseJson.user.username,
  589. user_email: responseJson.user.email,
  590. user_phone: responseJson.user.mobile_phone,
  591. user_birthday: responseJson.user.birthday_date
  592. },
  593. function() {
  594. // var PushNotification = require('react-native-push-notification');
  595. // PushNotification.localNotification({
  596. // message: "My Notification Message", // (required)
  597. // });
  598. }
  599. );
  600. })
  601. .catch(error => {
  602. console.error(error);
  603. });
  604. }
  605.  
  606. render() {
  607. return (
  608. <ScrollView style={styles.ProfileWrapper}>
  609. <ImageBackground
  610. source={require("./images/header_profile.png")}
  611. style={styles.backgroundImage}
  612. >
  613. <View style={styles.ProfileHeader}>
  614. <Image
  615. source={{ uri: this.state.user_image }}
  616. style={styles.ProfileAvatar}
  617. />
  618. </View>
  619. </ImageBackground>
  620. <View style={styles.ProfileBody}>
  621. <View style={styles.ProfileInfo}>
  622. <Text style={styles.ProfileInfoLabel}>Username</Text>
  623. <Text style={styles.ProfileInfoValue}>
  624. {this.state.user_username}
  625. </Text>
  626. </View>
  627. <View style={styles.ProfileInfo}>
  628. <Text style={styles.ProfileInfoLabel}>Email</Text>
  629. <Text style={styles.ProfileInfoValue}>{this.state.user_email}</Text>
  630. </View>
  631. <View style={styles.ProfileInfo}>
  632. <Text style={styles.ProfileInfoLabel}>Phone Number</Text>
  633. <Text style={styles.ProfileInfoValue}>{this.state.user_phone}</Text>
  634. </View>
  635. <View style={styles.ProfileInfo}>
  636. <Text style={styles.ProfileInfoLabel}>Sex</Text>
  637. <Text style={styles.ProfileInfoValue}>{this.state.user_sex}</Text>
  638. </View>
  639. <View style={styles.ProfileInfo}>
  640. <Text style={styles.ProfileInfoLabel}>Your Birthday</Text>
  641. <Text style={styles.ProfileInfoValue}>
  642. {this.state.user_birthday}
  643. </Text>
  644. </View>
  645. </View>
  646. </ScrollView>
  647. );
  648. }
  649. }
  650.  
  651. const NiagaraApp = StackNavigator({
  652. Login: {
  653. screen: LoginScreen,
  654. headerMode: "none",
  655. headerLeft: null,
  656. header: null,
  657. navigationOptions: {
  658. header: null,
  659. headerLeft: null
  660. }
  661. },
  662. Home: {
  663. screen: HomeScreen,
  664. headerMode: "none",
  665. headerLeft: null,
  666. header: null,
  667. navigationOptions: {
  668. header: null,
  669. headerLeft: null
  670. }
  671. },
  672. Profile: {
  673. screen: ProfileScreen,
  674. headerLeft: null,
  675. headerMode: "none",
  676. header: null,
  677. navigationOptions: {
  678. header: null,
  679. headerLeft: null
  680. }
  681. }
  682. });
  683.  
  684. export default NiagaraApp;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement