Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. /* eslint-disable react/destructuring-assignment */
  2. /* eslint-disable no-underscore-dangle */
  3. /* eslint-disable react/state-in-constructor */
  4. import React, { Component } from 'react';
  5. import { View, FlatList } from 'react-native';
  6. import PropTypes from 'prop-types';
  7. import Icon from 'react-native-vector-icons/Ionicons';
  8. import { ActivityIndicator } from 'react-native-paper';
  9. import { connect } from 'react-redux';
  10. import { bindActionCreators } from 'redux';
  11. import { SearchBar } from 'react-native-elements';
  12. import ItemNote from '../components/ItemNote';
  13. import { getAll } from '../redux/actions/notesActions';
  14.  
  15. class HomeScreen extends Component {
  16. static navigationOptions = e => {
  17. return {
  18. title: 'HomePage',
  19. headerRight: (
  20. <Icon
  21. size={25}
  22. name="md-add"
  23. style={{
  24. padding: 10
  25. }}
  26. onPress={() => {
  27. e.navigation.push('Authentication');
  28. }}
  29. />
  30. ),
  31. headerLeft: (
  32. <Icon
  33. size={25}
  34. name="md-share"
  35. style={{
  36. marginStart: 10
  37. }}
  38. onPress={() => {
  39. e.navigation.push('Sharing');
  40. }}
  41. />
  42. )
  43. };
  44. };
  45.  
  46. static propTypes = {
  47. getAll: PropTypes.func.isRequired,
  48. notes: PropTypes.array.isRequired
  49. };
  50.  
  51. constructor(props) {
  52. super(props);
  53. // setting default state
  54. this.state = {
  55. search: '',
  56. notes: null,
  57. arrayholder: []
  58. };
  59. }
  60.  
  61. componentDidMount() {
  62. this.props.getAll();
  63. this.setState({
  64. notes: this.props.notes,
  65. dataSource: this.props.notes
  66. });
  67. }
  68.  
  69. search = text => {
  70. console.log(text);
  71. };
  72.  
  73. clear = () => {
  74. this.search.clear();
  75. };
  76.  
  77. searchFilterFunction = text => {
  78. // passing the inserted text in textinput
  79. const newData = this.state.notes.filter(item => {
  80. const itemData = item.name ? item.name.toUpperCase() : ''.toUpperCase();
  81. const textData = text.toUpperCase();
  82.  
  83. return itemData.indexOf(textData) > -1;
  84. });
  85.  
  86. this.setState({
  87. dataSource: newData,
  88. search: text
  89. });
  90. };
  91.  
  92. delete = () => {
  93. console.info('delete');
  94. };
  95.  
  96. ListViewItemSeparator = () => {
  97. return (
  98. <View
  99. style={{
  100. height: 1,
  101.  
  102. backgroundColor: '#fff'
  103. }}
  104. />
  105. );
  106. };
  107.  
  108. render() {
  109. return (
  110. <View
  111. style={{
  112. flex: 1
  113. }}
  114. >
  115. {' '}
  116. {this.state.dataSource === null ? (
  117. <View>
  118. <ActivityIndicator animating size="large" />
  119. </View>
  120. ) : (
  121. <>
  122. <View>
  123. <SearchBar
  124. round
  125. showCancel
  126. cancelIcon
  127. lightTheme
  128. platform={Platform.OS === 'ios' ? 'ios' : 'android'}
  129. searchIcon={{
  130. size: 24
  131. }}
  132. placeholder="Search..."
  133. onChangeText={text => this.searchFilterFunction(text)}
  134. onClear={text => this.searchFilterFunction('')}
  135. value={this.state.search}
  136. />{' '}
  137. <FlatList
  138. data={this.state.dataSource}
  139. ItemSeparatorComponent={this.ListViewItemSeparator}
  140. renderItem={e => (
  141. <ItemNote
  142. key={e.item.id}
  143. note={e.item}
  144. onDelete={this.delete}
  145. />
  146. )}
  147. keyExtractor={item => item.id.toString()}
  148. />{' '}
  149. </View>{' '}
  150. </>
  151. )}{' '}
  152. </View>
  153. );
  154. }
  155. }
  156.  
  157. const mapStateToProps = stateStore => {
  158. return {
  159. notes: stateStore.notes.notes
  160. };
  161. };
  162.  
  163. const mapActionsToProps = payload => {
  164. return {
  165. getAll: bindActionCreators(getAll, payload)
  166. };
  167. };
  168.  
  169. export default connect(mapStateToProps, mapActionsToProps)(HomeScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement