forked from vicontiveros00/Helsinki-City-Bike-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStations.jsx
148 lines (136 loc) · 5.67 KB
/
Stations.jsx
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
import { getAllStations, searchStations } from '../../util/apiCaller.js';
import Station from './Station/Station.jsx';
import Error from '../../util/Error';
import Pagination from '../Pagination/Pagination.jsx';
import React, { useState, useEffect } from "react";
import PulseLoader from 'react-spinners/PulseLoader';
import './Stations.css';
const Stations = ({ api }) => {
//stations list component, api is api url prop
const [ stations, setStations ] = useState([]);
//holds array of station objects from api call
const [ totalPages, setTotalPages ] = useState(1);
//holds amount of pages from api call
const [ currentPage, setCurrentPage ] = useState(1);
//holds current page from user input
const [ isSearching, setIsSearching ] = useState(false);
//true if api request is pending
const [ isLoading, setIsLoading ] = useState(false);
//if loading is true, render pulse loader
const [ hasError, setHasError ] = useState(false);
////handle error is api call is unsuccesful
const handleError = () => {
//error handler
setIsLoading(false);
setHasError(true);
throw new Error('Unable to get stations. Call Vic!');
}
//next 4 functions are handlers to be passed to pagination to handle updating states here
const handleFirstButton = () => {
setCurrentPage(1);
}
const handleSecondButton = () => {
setCurrentPage(currentPage - 1);
}
const handleThirdButton = () => {
setCurrentPage(currentPage + 1);
}
const handleLastButton = () => {
setCurrentPage(totalPages);
}
const reset = async () => {
setIsLoading(true);
try {
const data = await getAllStations(api, currentPage);
const { items, totalPages } = data;
setStations(items);
setTotalPages(totalPages);
setIsLoading(false);
setIsSearching(false);
} catch (err) {
handleError();
throw new Error(err);
}
}
useEffect(() => {
//reset other states if current page updates
reset();
}, [currentPage]);
const handleSearch = async (query) => {
//search handler
if (!query.includes('\'') && !query.includes(';') && !query.includes('\\') && !query.includes('\"')) {
//for some reason the above characters causes api errors, modify above line at own risk
try {
const data = await searchStations(api, query);
const { items, totalPages } = data;
setIsSearching(true);
setTotalPages(totalPages);
setStations(items);
} catch {
handleError()
}
} else if (query === '') {
reset();
//reset states when input contains empty string
}
}
return (
<>
{!hasError ?
<div className='station-list'>
<h1>All City Bike Stations</h1>
<input
type='text'
maxLength='10'
//overly long requests slow down api
placeholder='Search by name or address'
onChange={(e)=> {
e.target.value ?
handleSearch(e.target.value) :
reset();
}}
/>
{!isSearching && <Pagination
handleFirstButton = {handleFirstButton}
handleSecondButton = {handleSecondButton}
handleThirdButton = {handleThirdButton}
handleLastButton = {handleLastButton}
currentPage = {currentPage}
totalPages = {totalPages}
/>}
{!isLoading ?
//render stations if no pending api request
<div className='stations'>
<table>
<tbody>
<tr>
<th>Name</th>
<th>City</th>
</tr>
{stations.map((station) => {
return (
<tr>
<Station key={station.id} station={station} />
{/*render row for each station object in station array, default 10 per api caller*/}
</tr>
)
})}
</tbody>
</table>
</div> :
<>
<PulseLoader color="#646cff" cssOverride={{
display: 'flex',
justifyContent: 'center',
margin: '1rem'
}}/>
{/*render pulse loader when waiting for new stations*/}
</>
}
</div> :
<Error /> //display error message if api call unsuccesful
}
</>
)
}
export default Stations;