Guest User

Untitled

a guest
Oct 23rd, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import {
  3.   StyleSheet,
  4.   View,
  5.   Text,
  6.   TextInput,
  7.   ScrollView,
  8.   FlatList,
  9.   TouchableOpacity,
  10.   ToastAndroid,
  11.   StatusBar
  12. } from 'react-native';
  13. import FontAwesome from 'react-native-vector-icons/FontAwesome';
  14. import Ionicons from 'react-native-vector-icons/Ionicons';
  15. import MainToolbar from "./../../components/MainToolbar";
  16. import Barang from "./../../components/Barang";
  17. import commonStyles from "./../../common_styles";
  18. import RealmSchemas from "./../../realm/RealmSchemas";
  19. import DataService from "./../../realm/DataService";
  20.  
  21. const ToolbarRightContent = ({ goToFormInsert }) => (
  22.   <TouchableOpacity
  23.     activeOpacity={0.7}
  24.     onPress={goToFormInsert}
  25.   >
  26.     <Ionicons name="ios-add" size={28} color="#FFF" />
  27.   </TouchableOpacity>
  28. );
  29.  
  30. class ViewScreen extends Component {
  31.   state = {
  32.     barang: DataService.findAll('Barang'),
  33.     barangFiltered: DataService.findAll('Barang'),
  34.     q: ""
  35.   };
  36.   componentDidMount() {
  37.     this.didFocusListener = this.props.navigation.addListener(
  38.       'didFocus',
  39.       () => this.setState({ barang: DataService.findAll('Barang') })
  40.     );
  41.   }
  42.   componentWillUnmount() {
  43.     this.didFocusListener.remove();
  44.   }
  45.   _renderToolbar() {
  46.     return (
  47.       <MainToolbar
  48.         _onBackPress={() => this.props.navigation.goBack()}
  49.         title={'Lihat Barang'}
  50.         hasBackButton
  51.         rightContent={<ToolbarRightContent goToFormInsert={() => this.props.navigation.navigate("formInsertBarang")} />}
  52.       />
  53.     );
  54.   }
  55.   _renderBarang() {
  56.     return (
  57.       <FlatList
  58.         ref={(ref) => { this.flatListRef = ref; }}
  59.         contentContainerStyle={this.state.barangFiltered.length === 0 && { paddingTop: 10, justifyContent: 'center', alignItems: 'center' }}
  60.         showsVerticalScrollIndicator={false}
  61.         data={this.state.barangFiltered}
  62.         keyExtractor={(item, index) => `barang-${index}`}
  63.         renderItem={({ item, index }) => <Barang
  64.           item={item}
  65.           index={index}
  66.           navigation={this.props.navigation}
  67.           duplicateBarang={(item) => this._duplicateBarang(item)}
  68.           deleteBarang={(item) => this._deleteBarang(item)}
  69.         />}
  70.         ListEmptyComponent={<Text style={{ fontSize: 16, fontWeight: '400' }}>List is empty</Text>}
  71.         getItemLayout={(data, index) => { return {length: 200, index, offset: 200 * index} }}
  72.       />
  73.     );
  74.   }
  75.   _renderSearchBox() {
  76.     const { q } = this.state;
  77.     return (
  78.       <View style={commonStyles.searchContainer}>
  79.         <View style={commonStyles.searchBox}>
  80.           <View style={commonStyles.searchLeft}>
  81.             <FontAwesome
  82.               style={commonStyles.searchIcon}
  83.               name="search"
  84.               size={20}
  85.               color="#616161"
  86.             />
  87.           </View>
  88.           <View style={commonStyles.searchRight}>
  89.             <TextInput
  90.               value={q}
  91.               style={commonStyles.searchInput}
  92.               underlineColorAndroid="transparent"
  93.               placeholder={'Search'}
  94.               onChangeText={q => this._search(q)}
  95.             />
  96.           </View>
  97.         </View>
  98.       </View>
  99.     );
  100.   }
  101.   _search(q) {
  102.     const { barang } = this.state;
  103.  
  104.     q = q.toLowerCase();
  105.     const result = barang.filter(barang => String(barang['nama']).toLowerCase().includes(q));
  106.     this.setState({ q, barangFiltered: result });
  107.   }
  108.   _deleteBarang(item = null) {
  109.     if (item != null) {
  110.       DataService.delete(item);
  111.       ToastAndroid.show("Delete success", ToastAndroid.SHORT);
  112.  
  113.       this.setState({ barang: DataService.findAll('Barang') });
  114.     }
  115.   }
  116.   _duplicateBarang(item = null) {
  117.     if (item != null) {
  118.       const id = RealmSchemas.objects('Barang').length + 1;
  119.  
  120.       var obj = {
  121.         id,
  122.         kode: `${item.nama.substring(0, 2).toUpperCase()}${id}`,
  123.         nama: item.nama,
  124.         nama_toko: item.nama_toko,
  125.         harga_beli: item.harga_beli,
  126.         harga_jual: item.harga_jual,
  127.         foto: Array.from(item.foto),
  128.         keterangan: item.keterangan
  129.       };
  130.  
  131.       DataService.save('Barang', obj);
  132.  
  133.       ToastAndroid.show("Duplicate success", ToastAndroid.SHORT);
  134.  
  135.       this.setState({ barang: DataService.findAll('Barang') });
  136.     }
  137.   }
  138.   render() {
  139.     return (
  140.       <View style={styles.container}>
  141.         <StatusBar backgroundColor="#0035C9" barStyle="light-content" />
  142.         {this._renderToolbar()}
  143.         {this._renderSearchBox()}
  144.         <ScrollView
  145.           contentContainerStyle={{ paddingBottom: 8 }}
  146.           style={{ flex: 1, padding: 8 }}
  147.           showsVerticalScrollIndicator={false}
  148.         >
  149.           {this._renderBarang()}
  150.         </ScrollView>
  151.         <TouchableOpacity
  152.           activeOpacity={0.7}
  153.           style={styles.fab}
  154.           onPress={() => this.flatListRef.scrollToEnd()}
  155.         >
  156.           <Ionicons name="ios-add" size={32} color="#0035C9" />
  157.         </TouchableOpacity>
  158.       </View>
  159.     );
  160.   }
  161. }
  162.  
  163. const styles = StyleSheet.create({
  164.   container: {
  165.     flex: 1
  166.   },
  167.   fab: {
  168.     borderWidth: 1,
  169.     borderColor: 'rgba(0,0,0,0.2)',
  170.     alignItems: 'center',
  171.     justifyContent: 'center',
  172.     width: 70,
  173.     height: 70,
  174.     position: 'absolute',
  175.     right: 10,
  176.     top: 10,
  177.     backgroundColor: '#FFF',
  178.     borderRadius: 100
  179.   }
  180. });
  181.  
  182. export default ViewScreen;
Add Comment
Please, Sign In to add comment