Advertisement
Guest User

Untitled

a guest
Sep 26th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { ReactNode } from 'react';
  2.  
  3. import {
  4.     StyleProp,
  5.     StyleSheet,
  6.     TouchableOpacity,
  7.     ViewStyle,
  8. } from 'react-native';
  9.  
  10. import { SwipeListView } from 'react-native-swipe-list-view';
  11. import SFBaseComponent from '../SFBaseComponent';
  12. import SFButtonImage from '../SFButton/ButtonImage';
  13. import SFContainer from '../SFContainer';
  14. import SFImage from '../SFImage';
  15. import SFLine from '../SFLine';
  16. import SFText from '../SFText';
  17. import ProductListItem from './ProductListItem';
  18.  
  19. interface SFSwipeListViewProductProps {
  20.     style?: StyleProp<ViewStyle>;
  21.     productDatas: ProductListItem[];
  22.     onDeleteClick: (productListItem: ProductListItem) => void;
  23.     onItemClick: (productListItem: ProductListItem) => void;
  24. }
  25. let openRowId: any = null;
  26.  
  27. export default class SFSwipeListViewProduct extends SFBaseComponent<SFSwipeListViewProductProps, {}> {
  28.  
  29.     getMarginBottomListItem = (length: number, position: number) => {
  30.         if (position === (length - 1)) {
  31.             return 84;
  32.         }
  33.         return 0;
  34.     }
  35.  
  36.     openRow = (rowRef: any) => {
  37.         rowRef.manuallySwipeRow(-60);
  38.     }
  39.  
  40.     closeRow = (rowRef: any) => {
  41.         rowRef.closeRow();
  42.     }
  43.  
  44.     renderItem(rowData: any, rowMap: any): ReactNode {
  45.         const _item: ProductListItem = rowData as ProductListItem;
  46.         const index: number = this.props.productDatas.indexOf(_item);
  47.  
  48.         return (
  49.             <SFContainer
  50.                 style={{
  51.                     backgroundColor: 'white', flex: 1,
  52.                     // tslint:disable-next-line:max-line-length
  53.                     marginBottom: this.getMarginBottomListItem(this.props.productDatas.length, index),
  54.                 }}>
  55.                 <SFLine />
  56.                 <TouchableOpacity
  57.                     onPress={() => this.props.onItemClick(_item)}>
  58.                     <SFContainer
  59.                         // tslint:disable-next-line:max-line-length
  60.                         style={{
  61.                             flex: 1, flexDirection: 'row',
  62.                             height: 80,
  63.                             justifyContent: 'space-between',
  64.                             alignItems: 'center',
  65.                         }}>
  66.  
  67.                         {_item.stock <= 0 && _item.isOrderUpdatingStock &&
  68.                             <SFContainer>
  69.                                 <SFImage
  70.                                     resizeMode={'cover'}
  71.                                     style={{
  72.                                         height: 64, width: 64, marginBottom: 8,
  73.                                         marginTop: 8, marginLeft: 8, borderRadius: 8, marginRight: 12,
  74.                                     }}
  75.                                     // tslint:disable-next-line:max-line-length
  76.                                     source={_item.image || require('../../assets/images/img_product_placeholder/img_product_placeholder.png')}
  77.                                 />
  78.  
  79.                                 <SFContainer
  80.                                     style={{
  81.                                         position: 'absolute', height: 64, width: 64, borderRadius: 8, margin: 8,
  82.                                         backgroundColor: 'rgba(255, 255, 255, 0.50)',
  83.                                     }} />
  84.                             </SFContainer>
  85.                         }
  86.  
  87.                         {(_item.stock > 0 || (_item.stock <= 0 && !_item.isOrderUpdatingStock)) &&
  88.                             <SFContainer>
  89.                                 {(_item.stock > _item.minStock || (_item.stock <= _item.minStock &&
  90.                                     !_item.isOrderUpdatingStock)) &&
  91.                                     <SFImage
  92.                                         resizeMode={'cover'}
  93.                                         style={{
  94.                                             height: 64, width: 64, marginBottom: 8,
  95.                                             marginTop: 8, marginLeft: 8, borderRadius: 8, marginRight: 12,
  96.                                         }}
  97.                                         // tslint:disable-next-line:max-line-length
  98.                                         source={_item.image || require('../../assets/images/img_product_placeholder/img_product_placeholder.png')}
  99.                                     />
  100.                                 }
  101.  
  102.                                 {_item.stock <= _item.minStock && _item.isOrderUpdatingStock &&
  103.                                     <SFContainer style={{ flexDirection: 'row', paddingBottom: 6 }}>
  104.                                         <SFImage
  105.                                             resizeMode={'cover'}
  106.                                             style={{ height: 64, width: 64, margin: 8, borderRadius: 8 }}
  107.                                             // tslint:disable-next-line:max-line-length
  108.                                             source={_item.image || require('../../assets/images/img_product_placeholder/img_product_placeholder.png')}
  109.                                         />
  110.  
  111.                                         <SFContainer style={{
  112.                                             height: 24, width: 24,
  113.                                             borderRadius: 16,
  114.                                             backgroundColor: 'white',
  115.                                             marginLeft: -20,
  116.                                             marginBottom: -2,
  117.                                             alignSelf: 'flex-end',
  118.                                         }}>
  119.                                             <SFImage
  120.                                                 style={{ height: 24, width: 24 }}
  121.                                                 // tslint:disable-next-line:max-line-length
  122.                                                 source={require('../../assets/images/message_ic_alert/message_ic_alert.png')}
  123.                                             />
  124.                                         </SFContainer>
  125.                                     </SFContainer>
  126.                                 }
  127.                             </SFContainer>
  128.                         }
  129.  
  130.                         <SFContainer
  131.                             style={{ flex: 1, marginLeft: 16 }}>
  132.                             <SFText
  133.                                 text={_item.title!!.toString()}
  134.                                 style={s.title}
  135.                             />
  136.  
  137.                             {_item.stock <= 0 && _item.isOrderUpdatingStock &&
  138.                                 <SFText
  139.                                     style={[s.subtitle, { color: '#ea4161' }]}
  140.                                     text={'Stok habis'}
  141.                                 />
  142.                             }
  143.  
  144.                             {(_item.stock > 0 || (_item.stock <= 0 && !_item.isOrderUpdatingStock)) &&
  145.                                 <SFText
  146.                                     text={_item.subtitle}
  147.                                     style={s.subtitle}
  148.                                 />
  149.                             }
  150.                         </SFContainer>
  151.  
  152.                         <SFText
  153.                             text={_item.subtxt}
  154.                             style={[s.title, {
  155.                                 marginRight: 8,
  156.                                 fontWeight: 'bold',
  157.                                 textAlign: 'right',
  158.                                 color: '#333333',
  159.                             }]}
  160.                         />
  161.  
  162.                         <SFButtonImage style={{ margin: 24 }}
  163.                             onClick={() => {
  164.                                 if (this.props.onDeleteClick)
  165.                                     this.props.onDeleteClick(_item);
  166.                             }}
  167.                             source={require('../../assets/images/ic_delete_red/ic_delete_red.png')}
  168.                         />
  169.                     </SFContainer>
  170.                 </TouchableOpacity>
  171.             </SFContainer>
  172.         );
  173.     }
  174.  
  175.     renderHiddenItem(rowData: any): ReactNode {
  176.         const _item: ProductListItem = rowData as ProductListItem;
  177.         const index: number = this.props.productDatas.indexOf(_item);
  178.  
  179.         return (
  180.             <TouchableOpacity
  181.                 style={{
  182.                     flex: 1, alignSelf: 'flex-end', height: 60, width: 60,
  183.                     // tslint:disable-next-line:max-line-length
  184.                     marginBottom: this.getMarginBottomListItem(this.props.productDatas.length, index),
  185.                 }}
  186.                 onPress={() => this.props.onDeleteClick(_item!!)}>
  187.                 <SFContainer
  188.                     style={{
  189.                         flex: 1, backgroundColor: '#ea4161',
  190.                         width: '100%', height: '100%', justifyContent: 'center', alignItems: 'center',
  191.                     }}>
  192.                     <SFImage
  193.                         source={require('../../assets/images/ic_delete_white/ic_delete_white.png')}
  194.                     />
  195.                 </SFContainer>
  196.             </TouchableOpacity>
  197.         );
  198.     }
  199.  
  200.     doRender() {
  201.         return (
  202.             <SFContainer
  203.                 style={this.props.style}>
  204.                 <SwipeListView
  205.                     disableRightSwipe
  206.                     disableLeftSwipe
  207.                     useFlatList={true}
  208.                     data={this.props.productDatas}
  209.                     renderItem={(rowData: any, rowMap: any) => (
  210.                         this.renderItem(rowData.item, rowMap)
  211.                     )}
  212.                     renderHiddenItem={(rowData: any) => (
  213.                         this.renderHiddenItem(rowData.item)
  214.                     )}
  215.                     rightOpenValue={-60}
  216.                     onRowOpen={(rowKey: any, rowMap: any) => { openRowId = rowMap[rowKey]; }}
  217.                     onRowClose={() => { openRowId = null; }}
  218.                 />
  219.             </SFContainer>
  220.         );
  221.     }
  222. }
  223.  
  224. const s = StyleSheet.create({
  225.     title: {
  226.         fontFamily: 'Nunito',
  227.         fontSize: 14,
  228.         fontStyle: 'normal',
  229.         letterSpacing: 0,
  230.         fontWeight: '600',
  231.         color: '#333333',
  232.         marginRight: 16,
  233.     },
  234.     subtitle: {
  235.         fontFamily: 'Nunito',
  236.         fontSize: 12,
  237.         fontStyle: 'normal',
  238.         letterSpacing: 0,
  239.         color: '#999999',
  240.         marginRight: 16,
  241.     },
  242. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement