-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct-list.js
164 lines (148 loc) · 4.71 KB
/
product-list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React from 'react';
import { Dimensions, ImageBackground, ListRenderItemInfo, View } from 'react-native';
import { Button, Card, Icon, List, StyleService, Text, useStyleSheet } from '@ui-kitten/components';
import { CartIcon } from './extra/icons';
import { Product } from './extra/data';
import axios from 'axios';
import Reactotron from 'reactotron-react-native'
import AsyncStorage from '@react-native-async-storage/async-storage';
const products = [
Product.pinkChair(),
Product.blackLamp(),
Product.whiteChair(),
Product.woodChair(),
Product.pinkChair(),
Product.blackLamp(),
Product.whiteChair(),
Product.woodChair(),
];
var baseUrl = "https://standtogetherforchange.org";
export const ProductListScreen = ({ navigation, route }) => {
let [products, setProducts] = React.useState([]);
const [userCategory, setUserCategory] = React.useState("");
const styles = useStyleSheet(themedStyles);
React.useEffect(() => {
const fetchData = async () => {
let session = await AsyncStorage.getItem('@session');
session = JSON.parse(session);
const id = session.id;
setUserCategory(session.userCategory);
const params = {
target: 'products',
userCategory: session.userCategory,
id
};
try {
// get the data from the api
const response = await axios({
method: 'get',
url: `${baseUrl}/api.php`,
params: params,
});
// set the data to the products state
setProducts(response.data);
} catch (error) {
console.error(error);
}
};
fetchData();
}, []);
const displayProducts = products.filter(product => product.category === route.name);
const onItemPress = (item) => {
navigation && navigation.navigate('ViewProduct', { item, userCategory });
};
const onItemCartPress = (index) => {
navigation && navigation.navigate('ShoppingCart');
};
const renderItemFooter = (info) => {
Reactotron.log({ info });
return (
<View style={styles.itemFooter}>
<Text category='s1'>
{info.item.Price} RWF
</Text>
{/* <Button
style={styles.iconButton}
size='small'
accessoryLeft={CartIcon}
onPress={() => onItemCartPress(info.index)}
/> */}
</View>
);
}
const renderItemHeader = (info) => (
<ImageBackground
style={styles.itemHeader}
source={info.item.ImagePath ? { uri: baseUrl + '/' + info.item.ImagePath } : require('./assets/image-product-3.jpg')}
/>
);
const renderProductItem = (info) => (
<Card
style={styles.productItem}
header={() => renderItemHeader(info)}
footer={() => renderItemFooter(info)}
onPress={() => onItemPress(info.item)}>
<Text category='s1'>
{info.item.ProductName}
</Text>
<Text
appearance='hint'
category='c1'>
{info.item.Description}
</Text>
</Card>
);
const PlusIcon = (props) => (
<Icon {...props} name='plus' />
)
const onNewButton = () => {
navigation && navigation.navigate('CreateProduct', {
context: 'create',
});
};
return (
<>
{userCategory === "supplier" && <View>
<Button style={styles.button} status='primary' accessoryLeft={PlusIcon} onPress={onNewButton}>
NEW
</Button>
</View>
}
<List
contentContainerStyle={styles.productList}
data={products}
numColumns={2}
renderItem={renderProductItem}
/>
</>
);
};
const themedStyles = StyleService.create({
container: {
flex: 1,
backgroundColor: 'background-basic-color-2',
},
productList: {
paddingHorizontal: 8,
paddingVertical: 16,
},
productItem: {
flex: 1,
margin: 8,
maxWidth: Dimensions.get('window').width / 2 - 24,
backgroundColor: 'background-basic-color-1',
},
itemHeader: {
height: 140,
},
itemFooter: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingVertical: 20,
paddingHorizontal: 20,
},
iconButton: {
paddingHorizontal: 0,
},
});