· 6 years ago · Sep 20, 2019, 08:12 AM
1/* eslint-disable react/jsx-key */
2/* eslint-disable no-plusplus */
3/* eslint-disable react/no-unused-state */
4/* eslint-disable no-unused-vars */
5/* eslint-disable no-unused-expressions */
6/* eslint-disable no-lone-blocks */
7/* eslint-disable no-console */
8/* eslint-disable no-shadow */
9import React from 'react';
10import { View, FlatList, Text, Image, TouchableOpacity, ScrollView, RefreshControl } from 'react-native';
11import { openDatabase } from 'react-native-sqlite-storage';
12import { Card, CardItem } from 'native-base';
13import Header from '../../components/elements/Header';
14import Button from '../../components/elements/Button';
15import Input from '../../components/elements/Input';
16import I18n from '../../i18n';
17import styles from './styles';
18import IMAGES from '../../configs/images';
19
20const db = openDatabase({ name: 'menu.db' });
21
22export default class Component extends React.Component {
23 constructor(props) {
24 super(props);
25 this.state = {
26 name: '',
27 url: '',
28 descMenu: '',
29 isLike: 0,
30 label: '',
31 insert: 'insert',
32 allData: [],
33 refreshing: true
34 };
35 }
36 async componentDidMount() {
37 await this._showAll();
38 }
39 _insert = () => {
40 this._openDb();
41 console.log(this.state.name);
42 db.transaction(tx => {
43 tx.executeSql(
44 'INSERT INTO favorite_menu (menuName, descMenu, imageMenu) VALUES (?,?,?)',
45 [this.state.name, this.state.descMenu, this.state.url],
46 (txn, result) => {
47 console.log('result', result.rowsAffected);
48 }
49 );
50 });
51 };
52 _openDb = () => {
53 db.transaction(txn => {
54 txn.executeSql(
55 'CREATE TABLE IF NOT EXISTS favorite_menu(id INTEGER PRIMARY KEY AUTOINCREMENT, menuName VARCHAR(20), imageMenu VARCHAR(20), descMenu VARCHAR(100))',
56 []
57 );
58 });
59 };
60
61 _delete = index => {
62 // Alert.alert(index);
63 this._openDb();
64 db.transaction(tx => {
65 tx.executeSql('DELETE FROM favorite_menu where id=?', [index], (txn, result) => {
66 console.log(index);
67 });
68 });
69 };
70
71 _searchById = () => {
72 this._openDb();
73 db.transaction(tx => {
74 tx.executeSql('SELECT * FROM favorite_menu where menuName = ?', [this.state.name], (txn, result) => {
75 console.log(result.rows.length);
76 {
77 result.rows.length > 0 ? console.log('data udah ada') : this._insert();
78 }
79 });
80 });
81 };
82
83 _showAll = () => {
84 this._openDb();
85 db.transaction(tx => {
86 tx.executeSql('select * from favorite_menu', [], (txn, result) => {
87 const temp = [];
88 for (let i = 0; i < result.rows.length; i++) {
89 temp.push(result.rows.item(i));
90 }
91 this.setState({
92 allData: temp,
93 refreshing: false
94 });
95 console.log(this.state.allData);
96 });
97 });
98 };
99 _settState = item => {
100 console.log(item);
101 this.setState({ nama: item });
102 this._delete(this.state.name);
103 };
104 render() {
105 return (
106 <View>
107 <Header title={I18n.t('favoriteFood')} />
108 <FlatList
109 data={this.state.allData}
110 keyExtractor={item => item.menuName}
111 refreshControl={<RefreshControl refreshing={this.state.refreshing} onRefresh={this._showAll} />}
112 renderItem={({ item }) => (
113 <View style={styles.container}>
114 <View style={styles.imageContainer}>
115 <Image
116 style={{ flex: 1, width: undefined, height: undefined }}
117 resizeMode="cover"
118 source={{ uri: item.imageMenu }}
119 />
120 </View>
121 <Card style={styles.card}>
122 <View style={styles.cardContainer}>
123 <View style={styles.wordContainer}>
124 <Text style={styles.wordTitle}>{item.menuName}</Text>
125 <Text style={styles.wordDesc}>{item.descMenu}</Text>
126 </View>
127 <TouchableOpacity onPress={() => this._delete(item.id)}>
128 <View style={styles.loveContainer}>
129 <Image
130 style={{ flex: 1, width: undefined, height: undefined }}
131 source={IMAGES.loveIcon}
132 resizeMode="contain"
133 />
134 </View>
135 </TouchableOpacity>
136 </View>
137 </Card>
138 </View>
139 )}
140 />
141 </View>
142 );
143 }
144}