Advertisement
Guest User

Untitled

a guest
Jul 11th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { StyleSheet, View, Text, ImageBackground, TouchableWithoutFeedback, TouchableOpacity } from 'react-native';
  3. import { Toast } from 'native-base';
  4. import { FlatGrid } from 'react-native-super-grid';
  5. import CustomIcon from '../components/CustomIcon.js';
  6. import axios from 'axios';
  7. import NavigationService from '../components/NavigationService.js';
  8. import Loading from '../components/Loading.js';
  9. import I18n from '../locales';
  10. import SQLite from 'react-native-sqlite-2';
  11. import RF from 'react-native-responsive-fontsize';
  12. const db = SQLite.openDatabase('favourite.db', '1.0', '', 1);
  13.  
  14. export default class PageImageView extends Component {
  15.  
  16.     constructor(props) {
  17.         super(props);
  18.         this.state = {
  19.             isLoading: false,
  20.             error: '',
  21.             items: [],
  22.             items_db: []
  23.         }
  24.         this.reRenderSomething = this.props.nav.addListener('willFocus', () => {
  25.             this.componentDidMount();
  26.         });
  27.     }
  28.  
  29.     addToFavourites = (item_id, title, image, desc_html, url, expanded, normal_price, type) => {
  30.         db.transaction(function (txn) {
  31.             txn.executeSql('CREATE TABLE IF NOT EXISTS Favourite(id INTEGER PRIMARY KEY NOT NULL, item_id INTEGER, title VARCHAR(225), image VARCHAR(225), desc_html LONGTEXT, url VARCHAR(225), expanded VARCHAR(10), normal_price VARCHAR(100), type VARCHAR(100))', []);
  32.             txn.executeSql('SELECT * FROM `favourite` WHERE item_id = ? and type = ?', [item_id, type], (tx, res) => {
  33.                 if (res.rows.length > 0) {
  34.                     txn.executeSql('DELETE FROM `favourite` WHERE item_id = ? and type = ?', [item_id, type]);
  35.                 } else {
  36.                     txn.executeSql('INSERT INTO Favourite (item_id, title, image, desc_html, url, expanded, normal_price, type) VALUES (:item_id, :title, :image, :desc_html, :url, :expanded, :normal_price, :type)', [item_id, title, image, desc_html, url, expanded, normal_price, type]);
  37.                 }
  38.             });
  39.  
  40.         });
  41.         this.selectFromTable(type).then((result) => {
  42.             this.setState({ items_db: result });
  43.         })
  44.     }
  45.  
  46.     showErrorToast = () => {
  47.         this.setState({ isLoading: false });
  48.         Toast.show({
  49.             text: I18n.t('internetErrorText'),
  50.             type: "danger",
  51.             duration: 3000,
  52.             position: "top"
  53.         });
  54.     }
  55.  
  56.     async componentDidMount() {
  57.         this.getItems(this.props.url);
  58.         this.selectFromTable(this.props.page_type).then((result) => {
  59.             this.setState({ items_db: result });
  60.         })
  61.     }
  62.  
  63.     selectFromTable = (page_type) => {
  64.         return new Promise(function (resolve, reject) {
  65.             db.transaction((txn) => {
  66.                 txn.executeSql('SELECT * FROM `favourite` WHERE type = ?', [page_type], (tx, res) => {
  67.                     let itemsArray = [];
  68.                     for (let i = 0; i < res.rows.length; ++i) {
  69.                         itemsArray.push(res.rows.item(i).item_id)
  70.                     }
  71.                     resolve(itemsArray);
  72.                 });
  73.             });
  74.         });
  75.     }
  76.  
  77.     getItems = (url_path) => {
  78.         this.setState({ isLoading: true });
  79.         axios.create({ headers: { 'from-app': '1' } }).get(url_path + '?view=list')
  80.             .then(result =>
  81.                 this.setState({
  82.                     items: result.data.page,
  83.                     isLoading: false
  84.                 })
  85.             )
  86.             .catch(error => this.showErrorToast());
  87.     }
  88.  
  89.     _renderItem = ({ item }) => {
  90.         let heart_color = this.state.items_db.includes(parseInt(item.id)) ? '#FF0014' : '#FFF';
  91.         return (
  92.             <TouchableOpacity onPress={() => NavigationService.navigate('PageDetail', { page_title: this.props.title, url: item.url, page_type: this.props.page_type, ltd: item.ltd, lng: item.lng })}>
  93.                 <ImageBackground style={styles.itemImage} imageStyle={{ borderRadius: 15 }} source={{ uri: item.image }} >
  94.                     <View style={styles.gradientStyle}>
  95.                         <TouchableWithoutFeedback onPress={() => this.addToFavourites(item.id, item.title, item.image, item.desc_html, item.url, false, item.normal_price, this.props.title)}>
  96.                             <CustomIcon name='white_heart' size={20} style={[styles.heartStyle, { color: heart_color }]} />
  97.                         </TouchableWithoutFeedback>
  98.                         <Text style={styles.normalPriceText}>{I18n.t('normalPriceText')}: {item.normal_price}</Text>
  99.                         <Text style={styles.discountPriceText}>{I18n.t('discountPriceText')}: {item.price_discount}</Text>
  100.                     </View>
  101.                 </ImageBackground>
  102.             </TouchableOpacity>
  103.         );
  104.     }
  105.  
  106.     render() {
  107.         const { isLoading, items } = this.state;
  108.         if (isLoading || items.length === 0) {
  109.             return (<Loading />);
  110.         }
  111.         return (
  112.             <View style={styles.viewStyle}>
  113.                 <FlatGrid
  114.                     itemDimension={130}
  115.                     items={items.blocks}
  116.                     renderItem={this._renderItem}
  117.                 />
  118.             </View>
  119.         );
  120.     }
  121.  
  122. }
  123.  
  124. const styles = StyleSheet.create({
  125.     viewStyle: {
  126.         flex: 1,
  127.         paddingLeft: 25,
  128.         paddingRight: 25,
  129.         paddingBottom: 30,
  130.         paddingTop: 10,
  131.         backgroundColor: '#F5F7F6'
  132.     },
  133.     itemImage: {
  134.         borderRadius: 15,
  135.         width: null,
  136.         height: 120,
  137.         marginTop: 15
  138.     },
  139.     gradientStyle: {
  140.         backgroundColor: 'rgba(0,0,0,.3)',
  141.         borderRadius: 15,
  142.         height: 120,
  143.         width: null
  144.     },
  145.     heartStyle: {
  146.         position: 'absolute',
  147.         top: 0,
  148.         right: 0,
  149.         paddingTop: 10,
  150.         paddingRight: 10,
  151.         justifyContent: 'flex-end',
  152.         alignItems: 'flex-end'
  153.     },
  154.     normalPriceText: {
  155.         textAlign: 'left',
  156.         position: 'absolute',
  157.         backgroundColor: 'transparent',
  158.         color: '#fff',
  159.         left: 0,
  160.         bottom: 0,
  161.         paddingLeft: 10,
  162.         paddingBottom: 20,
  163.         fontSize: RF(1.6),
  164.         justifyContent: 'flex-end',
  165.         alignItems: 'flex-end'
  166.     },
  167.     discountPriceText: {
  168.         textAlign: 'left',
  169.         position: 'absolute',
  170.         backgroundColor: 'transparent',
  171.         color: '#fff',
  172.         left: 0,
  173.         bottom: 0,
  174.         paddingLeft: 10,
  175.         paddingBottom: 5,
  176.         fontSize: RF(1.6),
  177.         justifyContent: 'flex-end',
  178.         alignItems: 'flex-end'
  179.     }
  180. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement