Advertisement
Guest User

splashScreen.js

a guest
Mar 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import React, { PureComponent } from "react";
  2. import { InteractionManager, StatusBar, Text, View } from "react-native";
  3. import firebase from "react-native-firebase";
  4. import { NavigationActions, StackActions } from "react-navigation";
  5. import { connect } from "react-redux";
  6. import { setFcmToken } from "../actions/auth";
  7. import globalStyles from "../assets/styles/global";
  8. import type { RemoteMessage } from "react-native-firebase";
  9.  
  10. class SplashScreen extends PureComponent {
  11. static navigationOptions = {
  12. header: null
  13. };
  14.  
  15. state = {
  16. ready: false
  17. };
  18.  
  19. componentDidMount() {
  20. firebase
  21. .messaging()
  22. .hasPermission()
  23. .then(enabled => {
  24. if (enabled) {
  25. firebase
  26. .messaging()
  27. .getToken()
  28. .then(token => {
  29. this.props.setFcmToken(token);
  30. });
  31. } else {
  32. firebase
  33. .messaging()
  34. .requestPermission()
  35. .then(() => {
  36. console.log("User Now Has Permission");
  37. })
  38. .catch(error => {
  39. alert("Error", error);
  40. });
  41. }
  42. });
  43.  
  44. InteractionManager.runAfterInteractions(() => {
  45. this.setState({ ready: true });
  46. if (this.props.isLogin) {
  47. return this.onResetNav("Home");
  48. } else {
  49. return this.onResetNav("Login");
  50. }
  51. });
  52. this.messageListener = firebase
  53. .messaging()
  54. .onMessage((message: RemoteMessage) => {
  55. // Process your message as required
  56. console.log("message", message);
  57. });
  58. }
  59.  
  60. componentWillUnmount() {
  61. this.messageListener();
  62. }
  63. onResetNav(routeName) {
  64. const resetAction = StackActions.reset({
  65. index: 0,
  66. actions: [NavigationActions.navigate({ routeName: routeName })]
  67. });
  68. this.props.navigation.dispatch(resetAction);
  69. }
  70.  
  71. render() {
  72. if (!this.state.ready || this.state.ready == null) {
  73. return <View style={globalStyles.alignCenterContainer} />;
  74. }
  75. return (
  76. <View
  77. style={[globalStyles.centerContainer, { backgroundColor: "#2845b5" }]}
  78. >
  79. <StatusBar hidden />
  80. <Text
  81. style={[
  82. globalStyles.font,
  83. { fontWeight: "bold", fontSize: 20, color: "white" }
  84. ]}
  85. >
  86. Abunawas
  87. </Text>
  88. <Text style={[globalStyles.font, { color: "white" }]}>Corporate</Text>
  89. </View>
  90. );
  91. }
  92. }
  93.  
  94. const mapStateToProps = ({ auth }) => ({
  95. isLogin: auth.isLogin
  96. });
  97.  
  98. const mapDispatchToProps = dispatch => ({
  99. setFcmToken: token => dispatch(setFcmToken(token))
  100. });
  101.  
  102. export default connect(
  103. mapStateToProps,
  104. mapDispatchToProps
  105. )(SplashScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement