|
1 | 1 | import React, { Component } from 'react'; |
2 | | -import { StyleSheet, ScrollView, View } from 'react-native'; |
| 2 | +import { StyleSheet, View, ListView } from 'react-native'; |
3 | 3 | import OrderItem from './OrderItem'; |
4 | 4 | import Total from './Total'; |
5 | 5 |
|
6 | 6 | class ShoppingList extends Component { |
7 | 7 | //state = { total: 0 }; |
8 | 8 | constructor(props) { |
9 | 9 | super(props); |
10 | | - /*this.ds = new ListView.DataSource({ |
| 10 | + this.ds = new ListView.DataSource({ |
11 | 11 | rowHasChanged: (r1, r2) => r1.title !== r2.title |
12 | | - });*/ |
13 | | - this.state = { total: 0 }; |
| 12 | + }); |
| 13 | + this.state = { |
| 14 | + total: 0, |
| 15 | + dataSource: this.ds.cloneWithRows([]), |
| 16 | + }; |
| 17 | + } |
| 18 | + /*<OrderItem |
| 19 | + img="https://pbs.twimg.com/profile_images/569593540674473984/slHSRznT.jpeg" |
| 20 | + title="BUH" |
| 21 | + desc="buh" |
| 22 | + price={25.36} |
| 23 | + onQuantityUpdate={this.updateTotal.bind(this)} |
| 24 | + /> |
| 25 | + <OrderItem |
| 26 | + img="https://pbs.twimg.com/profile_images/569593540674473984/slHSRznT.jpeg" |
| 27 | + title="BUH" |
| 28 | + desc="buh" |
| 29 | + price={25.36} |
| 30 | + onQuantityUpdate={this.updateTotal.bind(this)} |
| 31 | + /> |
| 32 | + <OrderItem |
| 33 | + img="https://pbs.twimg.com/profile_images/569593540674473984/slHSRznT.jpeg" |
| 34 | + title="BUH" |
| 35 | + desc="buh" |
| 36 | + price={25.36} |
| 37 | + onQuantityUpdate={this.updateTotal.bind(this)} |
| 38 | + /> |
| 39 | + <OrderItem |
| 40 | + img="https://pbs.twimg.com/profile_images/569593540674473984/slHSRznT.jpeg" |
| 41 | + title="BUH" |
| 42 | + desc="buh" |
| 43 | + price={25.36} |
| 44 | + onQuantityUpdate={this.updateTotal.bind(this)} |
| 45 | + />*/ |
| 46 | + componentWillMount() { |
| 47 | + fetch('https://www.dmi.unict.it/~calanducci/LAP2/data.json', |
| 48 | + { headers: { 'Cache-Control': 'no-cache' } } |
| 49 | + ) |
| 50 | + .then(response => response.json()) |
| 51 | + .then(responseData => this.setState({ dataSource: this.ds.cloneWithRows(responseData.items) })) |
| 52 | + .catch(error => alert('Errore caricando file json', error)); |
14 | 53 | } |
15 | | - |
16 | 54 | updateTotal(amount) { |
17 | 55 | this.setState({ total: this.state.total + amount }); |
18 | 56 | } |
19 | | - |
20 | 57 | render() { |
21 | 58 | return ( |
22 | 59 | <View style={styles.wrapper}> |
23 | 60 | <Total total={this.state.total} /> |
24 | | - <ScrollView> |
25 | | - |
26 | | - <OrderItem |
27 | | - img="https://pbs.twimg.com/profile_images/569593540674473984/slHSRznT.jpeg" |
28 | | - title="BUH" |
29 | | - desc="buh" |
30 | | - price={25.36} |
31 | | - onQuantityUpdate={this.updateTotal.bind(this)} |
32 | | - /> |
33 | | - <OrderItem |
34 | | - img="https://pbs.twimg.com/profile_images/569593540674473984/slHSRznT.jpeg" |
35 | | - title="BUH" |
36 | | - desc="buh" |
37 | | - price={25.36} |
38 | | - onQuantityUpdate={this.updateTotal.bind(this)} |
39 | | - /> |
40 | | - <OrderItem |
41 | | - img="https://pbs.twimg.com/profile_images/569593540674473984/slHSRznT.jpeg" |
42 | | - title="BUH" |
43 | | - desc="buh" |
44 | | - price={25.36} |
45 | | - onQuantityUpdate={this.updateTotal.bind(this)} |
46 | | - /> |
47 | | - <OrderItem |
48 | | - img="https://pbs.twimg.com/profile_images/569593540674473984/slHSRznT.jpeg" |
49 | | - title="BUH" |
50 | | - desc="buh" |
51 | | - price={25.36} |
52 | | - onQuantityUpdate={this.updateTotal.bind(this)} |
53 | | - /> |
54 | | - |
55 | | - </ScrollView> |
| 61 | + <ListView |
| 62 | + enableEmptySections |
| 63 | + dataSource={this.state.dataSource} |
| 64 | + renderRow={row => |
| 65 | + <OrderItem |
| 66 | + img={row.thumbnailUrl} |
| 67 | + title={row.title} |
| 68 | + desc={row.description} |
| 69 | + price={row.price} |
| 70 | + onQuantityUpdate={this.updateTotal.bind(this)} |
| 71 | + />} |
| 72 | + /> |
56 | 73 | </View> |
57 | 74 | ); |
58 | 75 | } |
|
0 commit comments