Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. import React from "react"
  2. import { View, ViewStyle, TextStyle, RefreshControl } from "react-native"
  3. import { FlatList } from "react-navigation"
  4. import { NavigationStackScreenProps } from "react-navigation-stack"
  5.  
  6. import { observer, inject } from "mobx-react"
  7. import { Placeholder, PlaceholderMedia, Shine, PlaceholderLine, Fade } from "rn-placeholder"
  8. import { Card } from "../../components/card"
  9. import { Screen } from "../../components/screen"
  10. import { color, spacing, typography } from "../../theme"
  11. import { Tag } from "../../components/tag"
  12. import { NavigatorScreenProps, NavigationStore } from "../../navigation/navigation-store"
  13. import { Header } from "../../components/header"
  14. import { SpaceStore, AppendSpaceSnapshot } from "../../models/space-store"
  15.  
  16. const noop = () => null
  17.  
  18. interface SpaceListScreenProps
  19. extends NavigationStackScreenProps<{
  20. screenSprops: NavigatorScreenProps
  21. }> {
  22. spaceStore: SpaceStore
  23. navigationStore: NavigationStore
  24. }
  25.  
  26. interface SpaceListSpaceState {
  27. isRefreshing: boolean
  28. }
  29.  
  30. const LIST_CONTAINER: ViewStyle = {
  31. alignItems: "center",
  32. paddingBottom: spacing[5],
  33. }
  34.  
  35. const LIST: ViewStyle = {
  36. paddingHorizontal: spacing[5],
  37. }
  38.  
  39. const HEADER: ViewStyle = {
  40. backgroundColor: color.palette.grayFAF,
  41. paddingBottom: 0,
  42. marginBottom: 0,
  43. }
  44.  
  45. const HEADER_LEFT_TEXT: TextStyle = {
  46. color: color.palette.black,
  47. fontSize: 20,
  48. fontFamily: typography.headerBold,
  49. }
  50.  
  51. const HEADER_RIGHT_TEXT: TextStyle = {
  52. color: color.primaryDarker,
  53. fontFamily: typography.primaryBold,
  54. marginTop: 4,
  55. }
  56.  
  57. const SPACE_LIST_TAGS: ViewStyle = {
  58. flexDirection: "row",
  59. justifyContent: "flex-start",
  60. flexGrow: 0,
  61. flexShrink: 0,
  62. flexWrap: "wrap",
  63. }
  64.  
  65. const TAG: ViewStyle = {
  66. flexDirection: "row",
  67. flexWrap: "nowrap",
  68. alignItems: "center",
  69. backgroundColor: color.palette.white,
  70. }
  71.  
  72. const TAG_PREFIX: TextStyle = {
  73. color: color.palette.gray949,
  74. fontFamily: typography.primaryBold,
  75. fontSize: 12,
  76. lineHeight: 12,
  77. marginRight: spacing[1],
  78. }
  79.  
  80. const TAG_VALUE: TextStyle = {
  81. color: color.palette.gray5F6,
  82. fontFamily: typography.primaryBold,
  83. lineHeight: 12,
  84. }
  85.  
  86. const PLACEHOLDER_TAG: ViewStyle = {
  87. borderRadius: 1,
  88. marginRight: spacing[2],
  89. marginBottom: spacing[2],
  90. }
  91.  
  92. const PLACEHOLDER_CARD_WRAPPER: ViewStyle = {
  93. width: "100%",
  94. shadowColor: "#000",
  95. shadowOffset: {
  96. width: 0,
  97. height: 1,
  98. },
  99. shadowOpacity: 0.12,
  100. shadowRadius: 2.22,
  101. elevation: 1,
  102. }
  103.  
  104. const PLACEHOLDER_CARD: ViewStyle = {
  105. height: 288,
  106. marginTop: 16,
  107. borderRadius: 3,
  108. width: "100%",
  109. backgroundColor: color.palette.offWhite,
  110. }
  111.  
  112. interface SpaceListSpace extends AppendSpaceSnapshot {
  113. averageRating: number
  114. }
  115.  
  116. @inject("spaceStore", "navigationStore")
  117. @observer
  118. export class SpaceListScreen extends React.Component<SpaceListScreenProps, SpaceListSpaceState> {
  119. static navigationOptions = ({ navigation }) => ({
  120. header: (
  121. <Header
  122. onRightPress={() => {
  123. navigation.navigate("searchFilters")
  124. }}
  125. onLeftPress={() => noop()}
  126. leftTx="spaceListScreen.searchSpaces"
  127. rightTx="spaceListScreen.filter"
  128. style={HEADER}
  129. leftTextStyle={HEADER_LEFT_TEXT}
  130. rightTextStyle={HEADER_RIGHT_TEXT}
  131. />
  132. ),
  133. })
  134.  
  135. state = {
  136. isRefreshing: false,
  137. }
  138.  
  139. async componentDidMount() {
  140. this.refreshSpaces()
  141. }
  142.  
  143. refreshSpaces = async () => {
  144. const { apiFindManySpace, resetSpaces } = this.props.spaceStore
  145. this.setState({ isRefreshing: true })
  146. resetSpaces()
  147. await apiFindManySpace()
  148. this.setState({ isRefreshing: false })
  149. }
  150.  
  151. onCardPress = async (id: string) => {
  152. const { apiFindOneSpace } = this.props.spaceStore
  153. const { navigateTo, showSpinnerModal, hideSpinnerModal } = this.props.navigationStore
  154.  
  155. showSpinnerModal()
  156. await apiFindOneSpace({
  157. id,
  158. })
  159. navigateTo("space")
  160. hideSpinnerModal()
  161. }
  162.  
  163. renderCard = ({ item }) => {
  164. const space: SpaceListSpace = item
  165. const { isRefreshing } = this.state
  166.  
  167. if (isRefreshing) {
  168. return (
  169. <View style={PLACEHOLDER_CARD_WRAPPER}>
  170. <Placeholder Animation={Fade}>
  171. <PlaceholderMedia style={PLACEHOLDER_CARD} />
  172. </Placeholder>
  173. </View>
  174. )
  175. }
  176.  
  177. return (
  178. <Card
  179. titleText={space.title}
  180. onPress={() => this.onCardPress(space.id)}
  181. subTitleText={space.spaceLocationFormatted}
  182. tagText="/hr"
  183. tagPrefixText={`$${space.price.hourly}`}
  184. rating={space.averageRating}
  185. imageURL={space.images[0].url}
  186. />
  187. )
  188. }
  189.  
  190. renderTags = () => {
  191. const { isRefreshing } = this.state
  192. if (isRefreshing) {
  193. return (
  194. <Placeholder Animation={Shine}>
  195. <View style={SPACE_LIST_TAGS}>
  196. <PlaceholderLine height={15} width={21} style={PLACEHOLDER_TAG} />
  197. <PlaceholderLine width={25} style={PLACEHOLDER_TAG} />
  198. <PlaceholderLine width={43} style={PLACEHOLDER_TAG} />
  199. <PlaceholderLine width={30} style={PLACEHOLDER_TAG} />
  200. </View>
  201. </Placeholder>
  202. )
  203. }
  204. return (
  205. <View style={SPACE_LIST_TAGS}>
  206. <Tag
  207. style={TAG}
  208. prefixTx={null}
  209. tx="spaceListScreen.tags.time.value"
  210. prefixTextStyle={TAG_PREFIX}
  211. textStyle={TAG_VALUE}
  212. />
  213. <Tag
  214. style={TAG}
  215. prefixTx="spaceListScreen.tags.location.prefix"
  216. tx="spaceListScreen.tags.location.value"
  217. prefixTextStyle={TAG_PREFIX}
  218. textStyle={TAG_VALUE}
  219. />
  220. <Tag
  221. style={TAG}
  222. prefixTx="spaceListScreen.tags.activity.prefix"
  223. tx="spaceListScreen.tags.activity.value"
  224. prefixTextStyle={TAG_PREFIX}
  225. textStyle={TAG_VALUE}
  226. />
  227. <Tag
  228. style={TAG}
  229. prefixTx="spaceListScreen.tags.amenities.prefix"
  230. tx="spaceListScreen.tags.amenities.value"
  231. prefixTextStyle={TAG_PREFIX}
  232. textStyle={TAG_VALUE}
  233. />
  234. </View>
  235. )
  236. }
  237.  
  238. render() {
  239. const { spaces } = this.props.spaceStore
  240. const { isRefreshing } = this.state
  241.  
  242. const data = isRefreshing ? [{ id: 1 }, { id: 2 }, { id: 3 }] : spaces
  243.  
  244. return (
  245. <Screen
  246. preset="scroll"
  247. backgroundColor={color.palette.grayFAF}
  248. refreshControl={
  249. <RefreshControl
  250. onRefresh={this.refreshSpaces}
  251. refreshing={isRefreshing}
  252. tintColor={color.primary}
  253. colors={[color.primary, color.primaryDarker]}
  254. style={{ backgroundColor: "transparent" }}
  255. />
  256. }>
  257. <FlatList
  258. style={LIST}
  259. contentContainerStyle={LIST_CONTAINER}
  260. data={data}
  261. keyExtractor={item => String(item.id)}
  262. renderItem={this.renderCard}
  263. ListHeaderComponent={this.renderTags()}
  264. />
  265. </Screen>
  266. )
  267. }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement