Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. /* eslint react/forbid-prop-types: "off"*/
  2. import React, { PureComponent } from "react";
  3. import { AppState } from "react-native";
  4. import DrawerStack from "./DrawerStack";
  5. import AuthStack from "./auth/stack";
  6. import PropTypes from "prop-types";
  7. import { connect } from "react-redux";
  8. import { NavigationActions } from "react-navigation";
  9. import * as cache from "utils/cache";
  10. import { AUTH_STATUS } from "../enums/authStatus";
  11. import { resetState } from "navigation/actions";
  12. import { setSelectedEvent } from "events/actions";
  13. import { setSelectedSurvey } from "surveys-ballots/actions";
  14. import { setSelectedArticle } from "articles/actions";
  15. import { setSelectedAlert } from "action-alerts/actions";
  16. import * as articleHelpers from "articles/helpers/actions-helpers";
  17. import * as eventHelpers from "events/helpers/actions-helper";
  18. import * as alertHelpers from "action-alerts/helpers/actions-helpers";
  19. import * as appcenter from "utils/appcenterLogger";
  20. import * as surveysHelpers from "surveys-ballots/helpers/data-helpers";
  21.  
  22. class RootNavigation extends PureComponent {
  23. static propTypes = {
  24. deepLinkElementRoute: PropTypes.string,
  25. deepLinkElementType: PropTypes.string,
  26. notification: PropTypes.string,
  27. resetPasswordDeepLinkKey: PropTypes.string,
  28. resetState: PropTypes.func,
  29. setSelectedAlert: PropTypes.func,
  30. setSelectedArticle: PropTypes.func,
  31. setSelectedEvent: PropTypes.func,
  32. setSelectedSurvey: PropTypes.func.isRequired,
  33. user: PropTypes.object
  34. };
  35.  
  36. static defaultProps = {
  37. resetPasswordDeepLinkKey: "",
  38. user: null
  39. };
  40.  
  41. constructor(props) {
  42. super(props);
  43. this.transitioned = false;
  44.  
  45. this.state = {
  46. appState: AppState.currentState
  47. };
  48. }
  49.  
  50. componentDidMount() {
  51. AppState.addEventListener("change", this._handleAppStateChange);
  52.  
  53. const resetPasswordDeepLinked = this.props.resetPasswordDeepLinkKey !== "";
  54. if (resetPasswordDeepLinked) {
  55. return this._pushNavigation("SetNewPassword");
  56. }
  57.  
  58. const eventsDeepLinked = this.props.deepLinkElementType === "event";
  59. if (eventsDeepLinked) {
  60. return this._pushNavigation(this.props.deepLinkElementRoute);
  61. }
  62. const surveyDeepLinked = this.props.deepLinkElementType === "survey_ballot";
  63. if (surveyDeepLinked) {
  64. return this._pushNavigation(this.props.deepLinkElementRoute);
  65. }
  66. }
  67. // should be changed to `static getDerivedStateFromProps` but said lifecycle
  68. // method cannot do props comparisons
  69. componentWillReceiveProps(nextProps) {
  70. //deep link set/reset password
  71. const resetPasswordDeepLinked =
  72. this.props.resetPasswordDeepLinkKey === "" &&
  73. nextProps.resetPasswordDeepLinkKey !== "";
  74. if (resetPasswordDeepLinked) {
  75. return this._pushNavigation("SetNewPassword");
  76. }
  77.  
  78. //prettier-ignore
  79. const notifiationReceived = !this.props.notification && nextProps.notification;
  80. if (notifiationReceived) {
  81. this._handleNotification(nextProps);
  82. }
  83.  
  84. if (this._deepLinkChanged(nextProps)) {
  85. const eventsDeepLinked = nextProps.deepLinkElementType === "event";
  86. if (eventsDeepLinked) {
  87. return this._pushNavigation(nextProps.deepLinkElementRoute);
  88. }
  89. const surveyDeepLinked =
  90. nextProps.deepLinkElementType === "survey_ballot";
  91. if (surveyDeepLinked) {
  92. return this._pushNavigation(nextProps.deepLinkElementRoute);
  93. }
  94. }
  95. }
  96.  
  97. componentWillUnmount() {
  98. AppState.removeEventListener("change", this._handleAppStateChange);
  99. }
  100.  
  101. _deepLinkChanged = nextProps => {
  102. return nextProps.deepLinkElementRoute !== "";
  103. };
  104.  
  105. _handleAppStateChange = async nextAppState => {
  106. if (
  107. this.state.appState.match(/inactive|background/) &&
  108. nextAppState === "active"
  109. ) {
  110. const redirect = await cache.shouldInactivityRedirect();
  111. if (redirect && this.navigator) {
  112. this.setState({ appState: nextAppState }, () => {
  113. const resetAction = NavigationActions.reset({
  114. index: 0,
  115. key: null,
  116. actions: [NavigationActions.navigate({ routeName: "MainDrawer" })]
  117. });
  118. this.navigator.dispatch(resetAction);
  119. });
  120. }
  121. } else {
  122. this.setState({ appState: nextAppState });
  123. cache.setBackgroundTime();
  124. appcenter.trackAppBackgrounding();
  125. }
  126. };
  127.  
  128. _handleNotification = async ({ notification }) => {
  129. const { Type } = notification;
  130. const objectKey = notification.Key || notification.Id;
  131.  
  132. if (Type.toLowerCase() === "event") {
  133. const event = await eventHelpers.fetchEventDetails({
  134. Key: objectKey
  135. });
  136. this.props.setSelectedEvent(event);
  137. this._pushNavigation("EventDetails");
  138. }
  139.  
  140. if (["SURVEY_SURVEY", "SURVEY_BALLOT"].includes(Type)) {
  141. const routeName = surveysHelpers.ballotOrSurvey(Type);
  142. this.props.setSelectedSurvey({ Key: objectKey });
  143. this._pushNavigation(routeName);
  144. }
  145.  
  146. if (Type === "ALERT") {
  147. const alert = await alertHelpers.fetchCampaign(objectKey, "DETAIL_FULL");
  148. this.props.setSelectedAlert(alert);
  149. this._pushNavigation("ActionAlertDetails", {
  150. content: alert.Alert,
  151. id: alert.CampaignId,
  152. title: alert.Headline
  153. });
  154. }
  155.  
  156. if (Type === "ARTICLE") {
  157. const article = await articleHelpers.fetchArticleDetails({
  158. Id: objectKey
  159. });
  160. this.props.setSelectedArticle(article);
  161. this._pushNavigation("ArticleDetails");
  162. }
  163. };
  164.  
  165. _pushNavigation = (routeName, params = {}) => {
  166. if (this.navigator && !this.transitioned) {
  167. this.navigator.dispatch(
  168. NavigationActions.navigate({
  169. routeName,
  170. params
  171. })
  172. );
  173. this.props.resetState();
  174. this.transitioned = true;
  175. setTimeout(() => {
  176. this.transitioned = false;
  177. }, 2000);
  178. }
  179. };
  180.  
  181. _getCurrentRouteName = navigationState => {
  182. if (!navigationState) {
  183. return null;
  184. }
  185. const route = navigationState.routes[navigationState.index];
  186. if (route.routes) {
  187. return this._getCurrentRouteName(route);
  188. }
  189. return route.routeName;
  190. };
  191.  
  192. _screenTracking = (prevState, currentState) => {
  193. const currentScreen = this._getCurrentRouteName(currentState);
  194. const prevScreen = this._getCurrentRouteName(prevState);
  195. // comment out if removing appcenter
  196. if (prevScreen !== currentScreen) {
  197. appcenter.trackNavigation(prevScreen, currentScreen);
  198. }
  199. };
  200.  
  201. render() {
  202. if (
  203. this.props.user &&
  204. this.props.user.authStatus === AUTH_STATUS.LOGGED_IN
  205. ) {
  206. return (
  207. <DrawerStack
  208. ref={nav => {
  209. this.navigator = nav;
  210. }}
  211. onNavigationStateChange={this._screenTracking}
  212. />
  213. );
  214. }
  215. return (
  216. <AuthStack
  217. authStatus={this.props.user.authStatus}
  218. // onNavigationStateChange={this._screenTracking}
  219. ref={nav => {
  220. this.navigator = nav;
  221. }}
  222. />
  223. );
  224. }
  225. }
  226.  
  227. const mapStateToProps = ({ auth, registrations, navigation }) => {
  228. const { user } = auth;
  229. return {
  230. user,
  231. resetPasswordDeepLinkKey: registrations.resetPasswordData.resetKey,
  232. ...navigation
  233. };
  234. };
  235.  
  236. export default connect(
  237. mapStateToProps,
  238. {
  239. resetState,
  240. setSelectedEvent,
  241. setSelectedSurvey,
  242. setSelectedArticle,
  243. setSelectedAlert
  244. }
  245. )(RootNavigation);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement