-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
83 lines (73 loc) · 2.01 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
import React, { useEffect, useState } from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';
import { API_URL, API_KEY } from '@env'
import Filters from './components/Filters';
import Pagination from './components/Pagination';
import CarParksList from './components/CarParksList';
export default function App() {
const [carParks, setCarParks] = useState([]);
const [pageLinks, setPageLinks] = useState([]);
const [isError, setError] = useState(false);
const [isLoading, setLoading] = useState(true);
function getCarParks(url, filters) {
if (!API_URL || !API_KEY) {
throw new Error('Missing API_URL or API_KEY');
}
setLoading(true);
setError(false);
url = url ? url : getCarParksApiUrl(filters);
fetch(url, {
headers: {
'accept': 'application/json',
'x-api-key': API_KEY
}
})
.then(response => response.json())
.then(results => {
setCarParks(results.data);
setPageLinks(results.links);
})
.catch(error => setError(error))
.finally(() => setLoading(false));
}
useEffect(() => {
getCarParks(API_URL);
}, []);
return (
<View style={styles.container}>
<Text style={styles.title}>Car Parks</Text>
<Text style={styles.filterTitle}>Filter by:</Text>
<Filters getCarParks={getCarParks} />
<CarParksList isLoading={isLoading} isError={isError} carParks={carParks} />
<Pagination isLoading={isLoading} isError={isError} pageLinks={pageLinks} getCarParks={getCarParks} />
</View>
);
}
function getCarParksApiUrl({
filterParkAndRide,
filterElectricChargePoint
}) {
let url = `${API_URL}?page=1`
if(filterElectricChargePoint) {
url += `&filters%5Belectric_car_charge_point%5D=true`;
}
if(filterParkAndRide) {
url += `&filters%5Bpark_and_ride%5D=true`;
}
return url;
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
title: {
marginTop: 20,
fontSize: 20,
padding: 10,
fontWeight: "bold"
},
});