Skip to content

Commit 6fce826

Browse files
committed
#2 fetch products by laravel Json API
1 parent 6769a01 commit 6fce826

File tree

2 files changed

+97
-26
lines changed

2 files changed

+97
-26
lines changed

nativeShop/pages/home.js

+30-18
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,58 @@ import { AppRegistry,View,Text,StyleSheet
44

55
export default class home extends Component{
66
static navigationOptions = {
7-
title: 'Welcome',
7+
title: 'Welcome',
88
};
99
render(){
1010
const { navigate } = this.props.navigation;
1111
return(
12-
<View style={{display:'flex',alignItems:'center', justifyContent:'center'}}>
13-
<Text style={{margin:10,fontWeight:'bold',color:'#000'}}>Hello from home</Text>
12+
<View style={styles.container}>
13+
<Text style={styles.pageName}>Hello from home</Text>
1414

15-
<TouchableOpacity
16-
style={styles.cat}
17-
onPress={() => navigate('Products', {cat:'electronics'})}
18-
><Text Text style={{color:'#fff'}}>Electronics ></Text>
15+
<TouchableOpacity
16+
style={styles.cat}
17+
onPress={() => navigate('Products',{cat:'electronics',id:'1'})}>
18+
<Text style={styles.btnText}> Electronics > </Text>
1919
</TouchableOpacity>
2020

21-
<TouchableOpacity
22-
style={styles.cat}
23-
onPress={() => navigate('Products', {cat:'automobiles'})}
24-
><Text Text style={{color:'#fff'}}>Automobiles ></Text>
21+
<TouchableOpacity
22+
style={styles.cat}
23+
onPress={() => navigate('Products',{cat:'automobiles',id:'2'})}>
24+
<Text style={styles.btnText}> Automobiles > </Text>
2525
</TouchableOpacity>
2626

2727
<TouchableOpacity
28-
style={styles.cat}
29-
onPress={() => navigate('Products', {cat:'Movies'})}
30-
><Text Text style={{color:'#fff'}}>Movies ></Text>
31-
</TouchableOpacity>
28+
style={styles.cat}
29+
onPress={() => navigate('Products',{cat:'Movies',id:'3'})}>
30+
<Text style={styles.btnText}> Movies > </Text>
31+
</TouchableOpacity>
3232

3333
<TouchableOpacity
34-
style={styles.cat}
35-
onPress={() => navigate('Products', {cat:'Books'})}
36-
><Text Text style={{color:'#fff'}}>Books ></Text>
34+
style={styles.cat}
35+
onPress={() => navigate('Products',{cat:'Books',id:'4'})}>
36+
<Text style={styles.btnText}> Books > </Text>
3737
</TouchableOpacity>
3838

39+
3940
</View>
4041
);
4142
}
4243
}
4344
const styles = StyleSheet.create({
45+
container:{
46+
display:'flex',alignItems:'center',
47+
justifyContent:'center'
48+
},
4449
cat:{
4550
backgroundColor:'blue',
4651
padding:10,margin:10,width:'95%'
52+
},
53+
pageName:{
54+
margin:10,fontWeight:'bold',
55+
color:'#000', textAlign:'center'
56+
},
57+
btnText:{
58+
color:'#fff',fontWeight:'bold'
4759
}
4860
})
4961
AppRegistry.registerComponent('home', () => home);

nativeShop/pages/products.js

+67-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,80 @@
11
import React, { Component } from 'react';
2-
import { AppRegistry,View,Text,StyleSheet } from 'react-native';
2+
import { AppRegistry,View,Text,
3+
StyleSheet,ListView,Image } from 'react-native';
34

45
export default class products extends Component{
5-
static navigationOptions = {
6+
7+
static navigationOptions = {
68
title: 'Products',
7-
};
9+
};
10+
11+
constructor(){
12+
super();
13+
14+
this.state={
15+
16+
dataSource: new ListView.DataSource({rowHasChanged:(r1,r2)=> r1!=r2}),
17+
//link: 'http://hardeepcoder.com/laravel/easyshop/api/products/' + params.id,
18+
}
19+
20+
21+
}
22+
componentDidMount(){
23+
const { params } = this.props.navigation.state;
24+
fetch('http://hardeepcoder.com/laravel/easyshop/api/products/' + params.id)
25+
.then((response) => response.json())
26+
.then((responseJson) =>{
27+
data = responseJson; // here we have all products data
28+
this.setState({
29+
dataSource: this.state.dataSource.cloneWithRows(data)
30+
})
31+
})
32+
.catch((error) =>{
33+
console.error(error);
34+
});
35+
36+
}
37+
838
render(){
939
const { params } = this.props.navigation.state;
1040
return(
11-
<View >
12-
<Text>hello from products</Text>
13-
<Text style={{color:'#000',fontWeight:'bold'}}>{params.cat}</Text>
14-
</View>
41+
<View style={styles.container}>
42+
43+
<Text style={styles.pageName}>Hello from Products</Text>
44+
<Text> Category Name: {params.cat} Category id: {params.id}</Text>
45+
46+
47+
<ListView
48+
dataSource={this.state.dataSource}
49+
renderRow={(rowData)=>
50+
51+
<View style={{padding:10,margin:10}}>
52+
<Text style={{fontWeight:'bold',textAlign:'center'}}>{rowData.pro_name}</Text>
53+
54+
<Image
55+
style={{width:'100%',height:250}}
56+
source={{uri:rowData.pro_img}}
57+
/>
58+
<Text>Price: {rowData.pro_price}</Text>
59+
60+
</View>
61+
}
62+
/>
63+
</View>
64+
1565
);
1666
}
67+
68+
69+
1770
}
1871
const styles = StyleSheet.create({
19-
72+
container:{
73+
display:'flex'
74+
},
75+
pageName:{
76+
margin:10,fontWeight:'bold',
77+
color:'#000', textAlign:'center'
78+
}
2079
})
2180
AppRegistry.registerComponent('products', () => products);

0 commit comments

Comments
 (0)