Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. interface DashboardScreenProps extends NavigationScreenProps<{}> {
  2. navigationStore?: NavigationStore
  3. userStore?: UserStore
  4. availabilityStore?: AvailabilityStore
  5. washoutStore?: WashoutStore
  6. locationStore?: LocationStore
  7. forumStore?: ForumStore
  8. loadStore?: LoadStore
  9. permissionStore?: PermissionStore
  10. notificationStore?: NotificationStore
  11. }
  12.  
  13. const Dashboard: NavigationScreenComponent = (props: DashboardScreenProps) => {
  14. const [isRefreshing, setIsRefreshing] = useState(false)
  15. const [isLoadingWashouts, setIsLoadingWashouts] = useState(false)
  16. const [isLoadingForumPosts, setIsLoadingForumPosts] = useState(false)
  17. const [isLoadingLoads, setIsLoadingLoads] = useState(false)
  18. const [tabState, setTabState] = useState({ newIndex: 0 })
  19.  
  20. const { isLocationAuthorized } = props.permissionStore
  21. const { resetDashboardLoads, apiGetDashboardLoads } = props.loadStore
  22. const { getCurrentLocation } = props.locationStore
  23. const { resetDashboardWashouts, apiGetDashboardWashouts } = props.washoutStore
  24. const { resetForumPosts, apiGetForumPosts } = props.forumStore
  25. const { currentUser } = props.userStore
  26. const { isProfileComplete } = currentUser
  27.  
  28. const searchLoads = async () => {
  29. if (!isProfileComplete || !isLocationAuthorized) return
  30. setIsLoadingLoads(true)
  31. resetDashboardLoads()
  32. await apiGetDashboardLoads()
  33. setIsLoadingLoads(false)
  34. }
  35.  
  36. const refreshHomeTab = async () => {
  37. setIsRefreshing(true)
  38. await loadHomeTab()
  39. setIsRefreshing(false)
  40. }
  41.  
  42. const searchWashouts = async () => {
  43. setIsLoadingWashouts(true)
  44. resetDashboardWashouts()
  45. await apiGetDashboardWashouts(props.locationStore.currentLocation)
  46. setIsLoadingWashouts(false)
  47. }
  48.  
  49. const searchForumPosts = async () => {
  50. setIsLoadingForumPosts(true)
  51. resetForumPosts()
  52. await apiGetForumPosts()
  53. setIsLoadingForumPosts(false)
  54. }
  55.  
  56. const updateNotificationsCount = () => {
  57. props.notificationStore.apiGetNotificationsUnreadCount()
  58. }
  59.  
  60. const loadHomeTab = async () => {
  61. await getCurrentLocation()
  62. Promise.all([searchWashouts(), searchForumPosts(), searchLoads(), updateNotificationsCount()])
  63. }
  64.  
  65. const tabs = [
  66. {
  67. tx: "dashboard.screenToggleDashboard",
  68. screen: (
  69. <HomeTab
  70. isLoadingLoads={isLoadingLoads}
  71. isLoadingWashouts={isLoadingWashouts}
  72. isLoadingForumPosts={isLoadingForumPosts}
  73. />
  74. ),
  75. },
  76. {
  77. text: translate("dashboard.screenToggleAvailability", {
  78. count: props.availabilityStore.myTrucks.length,
  79. }),
  80. screen: <AvailabilityTab />,
  81. rightComponent: <WalkthroughPin style={AVAILABILITY_PIN} id="truck-availability-11" />,
  82. },
  83. ]
  84.  
  85. useEffect(() => {
  86. loadHomeTab()
  87. const screenWillFocus = props.navigation.addListener("willFocus", c => loadHomeTab())
  88. return () => screenWillFocus.remove()
  89. }, [props.navigation])
  90.  
  91. return (
  92. <Observer>
  93. {() => (
  94. <Screen
  95. backgroundColor={color.palette.offWhite}
  96. preset="scroll"
  97. stickyHeaderIndices={[0]}
  98. refreshControl={
  99. <RefreshControl
  100. onRefresh={refreshHomeTab}
  101. refreshing={isRefreshing}
  102. tintColor="transparent"
  103. colors={["transparent"]}
  104. style={{ backgroundColor: "transparent" }}
  105. />
  106. }>
  107. <ScreenToggleBar tabs={tabs} state={tabState} onTabChange={setTabState} />
  108.  
  109. <ScreenToggleScene screens={tabs} toggleBarState={tabState} />
  110. </Screen>
  111. )}
  112. </Observer>
  113. )
  114. }
  115.  
  116. export const DashboardScreen = inject(
  117. "navigationStore",
  118. "userStore",
  119. "availabilityStore",
  120. "washoutStore",
  121. "locationStore",
  122. "forumStore",
  123. "loadStore",
  124. "permissionStore",
  125. "notificationStore",
  126. )(Dashboard)
  127.  
  128. DashboardScreen.navigationOptions = ({ navigation }) => ({
  129. header: (
  130. <Header
  131. titleTx="dashboard.headerTitle"
  132. style={HEADER_STYLE}
  133. onLeftPress={() => navigation.navigate("accountSettings")}
  134. leftIcon="settings"
  135. leftIconStyle={{ height: 19 }}
  136. leftView={<HeaderLeftButton onLeftPress={() => navigation.navigate("accountSettings")} />}
  137. rightView={<HeaderRight />}
  138. noBackButton
  139. />
  140. ),
  141. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement