Guest User

Untitled

a guest
Jan 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import {
  3. Text,
  4. View,
  5. Image,
  6. ScrollView,
  7. TouchableOpacity
  8. } from 'react-native'
  9. import _ from 'lodash'
  10. import { graphql } from 'react-apollo'
  11. import { createFilter } from 'react-native-search-filter'
  12. import Picker from 'react-native-wheel-picker'
  13. import Icon from 'react-native-vector-icons/MaterialCommunityIcons'
  14. import { Header, VideoBack, Loading, Separator } from '../common'
  15.  
  16. import GET_STUDIO_NEWS_QUERY from '../graphql/queries/new/getNews'
  17. import ADDED_SUBSCRIPTION from '../graphql/subscriptions/new/newAdded'
  18. import UPDATED_SUBSCRIPTION from '../graphql/subscriptions/new/newUpdated'
  19. import DELETED_SUBSCRIPTION from '../graphql/subscriptions/new/newDeleted'
  20.  
  21. const PickerItem = Picker.Item
  22. const colortheme = '#00FFC5'
  23.  
  24. class Main extends Component {
  25. static navigationOptions = ({ navigation }) => ({
  26. header:
  27. <Header
  28. title='Новости'
  29. leftButton
  30. leftIcon='md-add'
  31. colorLeft='#242323'
  32. rightButton
  33. rightIcon='ios-contact-outline'
  34. colorRight={colortheme}
  35. navigation={navigation}
  36. screen='User'
  37. />
  38. })
  39.  
  40. constructor(props) {
  41. super(props)
  42. this.state = {
  43. searchTerm: '',
  44. keyToFilters: ['title'],
  45. selectedItem: '',
  46. selectedValue: ''
  47. }
  48. }
  49.  
  50. componentWillMount() {
  51. this.props.data.subscribeToMore({
  52. document: ADDED_SUBSCRIPTION,
  53. updateQuery: (prev, { subscriptionData }) => {
  54. if (!subscriptionData.data) {
  55. return prev
  56. }
  57.  
  58. const newItem = subscriptionData.data.newAdded
  59. const shouldIgnore = (item) => {
  60. return ((newItem.studio._id !== item.studio._id) || (newItem._id === item._id))
  61. }
  62.  
  63. if (prev.getNews.some(shouldIgnore)) {
  64. console.log('garbage')
  65. } else {
  66. return {
  67. ...prev,
  68. getNews: [{ ...newItem }, ...prev.getNews] }
  69. }
  70. return prev
  71. }
  72. })
  73.  
  74. this.props.data.subscribeToMore({
  75. document: UPDATED_SUBSCRIPTION,
  76. updateQuery: (prev, { subscriptionData }) => {
  77. if (!subscriptionData.data) {
  78. return prev
  79. }
  80.  
  81. const updateItem = subscriptionData.data.newUpdated
  82. const newVar = prev.getNews.findIndex(item => (item._id === updateItem._id))
  83. const newGetStudioNews = [...prev.getNews]
  84. newGetStudioNews[newVar] = updateItem
  85.  
  86. if (prev.getNews.find(t => (t._id === updateItem._id))) {
  87. return {
  88. ...prev,
  89. getNews: newGetStudioNews
  90. }
  91. }
  92. return prev
  93. }
  94. })
  95.  
  96. this.props.data.subscribeToMore({
  97. document: DELETED_SUBSCRIPTION,
  98. updateQuery: (prev, { subscriptionData }) => {
  99. if (!subscriptionData.data) {
  100. return prev
  101. }
  102.  
  103. const deleted = subscriptionData.data.newDeleted
  104. if (prev.getNews.find(t => t._id === deleted._id)) {
  105. return {
  106. ...prev,
  107. getNews: prev.getNews.filter(t => t._id !== deleted._id)
  108. }
  109. }
  110. return prev
  111. }
  112. })
  113. }
  114.  
  115. onPickerSelect(value, index) {
  116. this.setState({
  117. searchTerm: value,
  118. selectedItem: index
  119. })
  120. }
  121.  
  122. searchUpdated(term) {
  123. this.setState({ searchTerm: term })
  124. }
  125.  
  126. render() {
  127. const { data: { getNews, loading } } = this.props
  128. const { searchTerm, keyToFilters } = this.state
  129. const filteredData = getNews ? getNews.filter(createFilter(searchTerm, keyToFilters)) : []
  130.  
  131. const uniqueData = getNews ? _.uniqBy(getNews, 'title') : []
  132.  
  133. const { container, subContainer, thumbImg, titleContainer, titleContainer2, h1, chevron, itemStyle, picker } = styles
  134.  
  135. if (loading) { return <Loading /> }
  136.  
  137. return (
  138. <View style={container}>
  139. <VideoBack />
  140. <ScrollView>
  141. <Picker
  142. style={picker}
  143. itemStyle={itemStyle}
  144. selectedValue={this.state.searchTerm}
  145. onValueChange={(value, index) => { this.onPickerSelect(value, index) } }
  146. >
  147. <PickerItem label='Все' value='' />
  148. {uniqueData.map((data) => (
  149. <PickerItem
  150. key={data._id}
  151. value={data._id}
  152. label={data.title}
  153. />
  154. )
  155. )}
  156. </Picker>
  157. {filteredData.map(item => {
  158. return (
  159. <TouchableOpacity onPress={ () => this.props.navigation.navigate('Detail', (item)) } key={item._id} >
  160. <Separator />
  161. <View style={subContainer}>
  162. <View style={titleContainer}>
  163. <Image style={thumbImg} source={{ uri: item.img }} />
  164. <View style={titleContainer2}>
  165. <Text style={h1} numberOfLines={1} ellipsizeMode='tail'>{item.subtitle}</Text>
  166. </View>
  167. </View>
  168. <Icon style={chevron} name="chevron-right" size={50} color="#DBD7D2" />
  169. </View>
  170. </TouchableOpacity>
  171. )
  172. })}
  173. </ScrollView>
  174. </View>
  175. )
  176. }
  177. }
  178.  
  179. const styles = {
  180. container: {
  181. flex: 1,
  182. justifyContent: 'center',
  183. marginTop: -75
  184. },
  185. subContainer: {
  186. flex: 1,
  187. backgroundColor: 'rgba(36, 36, 35, 0.4)',
  188. flexDirection: 'row',
  189. justifyContent: 'space-between',
  190. padding: 5
  191. },
  192. titleContainer: {
  193. flex: 1,
  194. backgroundColor: 'transparent',
  195. flexDirection: 'row'
  196. },
  197. thumbImg: {
  198. width: 100,
  199. height: 100
  200. },
  201. titleContainer2: {
  202. flex: 1,
  203. backgroundColor: 'transparent',
  204. justifyContent: 'center',
  205. alignItems: 'flex-start'
  206. },
  207. h1: {
  208. backgroundColor: 'transparent',
  209. flex: -1,
  210. alignSelf: 'flex-start',
  211. justifyContent: 'flex-start',
  212. fontFamily: 'AppleSDGothicNeo-Light',
  213. paddingLeft: 10,
  214. fontWeight: '400',
  215. color: '#DBDCDC',
  216. fontSize: 17
  217. },
  218. chevron: {
  219. backgroundColor: 'transparent',
  220. justifyContent: 'flex-end',
  221. alignSelf: 'center'
  222. },
  223. picker: {
  224. backgroundColor: 'rgba(36, 36, 35, 0.4)',
  225. height: 180,
  226. width: '100%'
  227. },
  228. itemStyle: {
  229. fontFamily: 'AppleSDGothicNeo-Light',
  230. backgroundColor: 'transparent',
  231. fontWeight: '700',
  232. color: '#DBDCDC',
  233. fontSize: 20
  234. }
  235. }
  236.  
  237. export default graphql(GET_STUDIO_NEWS_QUERY)(Main)
Add Comment
Please, Sign In to add comment