-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
169 lines (158 loc) · 4.05 KB
/
App.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
165
166
167
168
169
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Component, useState, useEffect } from "react";
import {
Platform,
StyleSheet,
Text,
View,
TextInput,
FlatList,
Button,
ActivityIndicator,
ScrollView
} from "react-native";
import ProductList from "./android/app/src/components/ProductList.js";
import FilteredProductList from "./android/app/src/components/FilteredProductList";
import { createStackNavigator, createAppContainer } from "react-navigation";
import ProductPage from "./android/app/src/components/ProductPage";
import Cart from "./android/app/src/components/Cart.js";
import CartIcon from "./android/app/src/components/CartIcon.js";
import CardPayment from "./android/app/src/components/CardPayment";
function HomeScreen(props) {
const [products, setProducts] = useState([]);
const [storeTitle, setTitle] = useState("Red Bull Apparel Finder");
const [searchInput, setSearchInput] = useState("");
const [productListType, setProductListType] = useState("everything");
useEffect(() => {
const fetchProducts = async () => {
const promise = await fetch(
"https://www.redbullshopus.com/products.json"
);
const data = await promise.json();
setProducts(data.products);
};
fetchProducts();
}, []);
const renderProductList = () => {
if (productListType === "filtered") {
const filteredProducts = products.filter(
product =>
product.title.toLowerCase().includes(searchInput.toLowerCase()) ||
product.product_type
.toLowerCase()
.includes(searchInput.toLowerCase()) ||
product.tags.includes(searchInput)
);
return (
<React.Fragment>
{products && (
<FilteredProductList
products={filteredProducts}
found={filteredProducts.length}
/>
)}
</React.Fragment>
);
} else {
return (
<React.Fragment>
{products && <ProductList products={products} />}
</React.Fragment>
);
}
};
const updateProductListType = () => {
if (searchInput) {
setProductListType("filtered");
} else {
setProductListType("everything");
}
};
return (
<View style={styles.mainContainer}>
<Text style={styles.welcome}> {storeTitle} </Text>
<Text style={styles.subheader}>Search The Red Bull Inventory!</Text>
<TextInput
style={styles.searchInput}
placeholder="Search for a particular item"
onChangeText={text => setSearchInput(text)}
onChange={() => updateProductListType()}
/>
<Text style={styles.searchText}>
Searching for: {searchInput ? searchInput : "Everything"}
</Text>
{products ? (
renderProductList()
) : (
<View>
<ActivityIndicator color="#880D1E" size="large" />
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
justifyContent: "center",
flex: 1
},
welcome: {
fontSize: 20,
textAlign: "center",
margin: 10,
color: "#880D1E"
},
subheader: {
fontSize: 15,
textAlign: "center",
color: "#880D1E",
fontWeight: "600"
},
searchInput: {
borderBottomWidth: 1,
borderBottomColor: "#880D1E",
borderStyle: "solid"
},
searchText: {
paddingTop: 10
},
mainContainer: {
marginHorizontal: 10
}
});
const MainNavigator = createStackNavigator(
{
Home: {
screen: HomeScreen,
navigationOptions: () => ({
title: "Red Bull"
})
},
ProductPage: { screen: ProductPage },
Cart: {
screen: Cart,
navigationOptions: () => ({
title: "Your Cart"
})
},
CardPayment: { screen: CardPayment }
},
{
initialRouteName: "Home",
defaultNavigationOptions: {
headerTintColor: "#880D1E",
headerRight: <CartIcon />,
headerRightContainerStyle: {
marginRight: 10
}
}
}
);
const App = createAppContainer(MainNavigator);
export default App;