forked from vicontiveros00/Helsinki-City-Bike-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Journeys.jsx
154 lines (144 loc) · 6.41 KB
/
Journeys.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
149
150
151
152
153
154
import React, { useEffect, useState } from 'react';
import Journey from './Journey/Journey';
import { getJourneys } from '../../util/apiCaller';
import PulseLoader from 'react-spinners/PulseLoader';
import Error from '../../util/Error';
import Pagination from '../Pagination/Pagination';
import './Journeys.css';
import useWindowWidth from '../../util/windowWidthHook';
const Journeys = ({ api }) => {
//journeys table
//grab api url from app.jsx
const [ journeys, setJourneys ] = useState([]);
//holds array of journey 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 [ isLoading, setIsLoading ] = useState(true);
//if loading is true, render pulse loader
const [ hasError, setHasError ] = useState(false);
//handle error is api call is unsuccesful
const [ sortMethod, setSortMethod ] = useState('departure');
//holds sort method from user input
const width = useWindowWidth();
//use custom window width hook to warn users about poor table viewablity on smaller screens
const handleError = () => {
//error handler
setIsLoading(false);
setHasError(true);
throw new Error('Unable to get journeys. 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);
}
//handle displaying sort marker
const updateSortMarker = (sort) => {
setSortMethod(sortMethod === sort ? `-${sort}` : sort)
}
useEffect(() => {
setIsLoading(true);
const getJourneysOnRender = async () => {
try {
const data = await getJourneys(api, currentPage, sortMethod);
const { items, totalPages } = data;
setJourneys(items);
setTotalPages(totalPages);
setIsLoading(false);
} catch (err) {
handleError();
throw new Error(err);
}
}
getJourneysOnRender();
}, [currentPage, sortMethod]);
//get new set of journeys when currentPage or sortMethod states is updated
//call error handler function if api call is unsuccesful
return (
<>
{!hasError ? //render page if there is no error
<div className='journey-list'>
{width < 800 && <p>Table doesn't look good on smaller screens :(</p>}
{/*show message for smaller screens*/}
<h1>All City Bike Journeys</h1>
<Pagination
handleFirstButton = {handleFirstButton}
handleSecondButton = {handleSecondButton}
handleThirdButton = {handleThirdButton}
handleLastButton = {handleLastButton}
currentPage = {currentPage}
totalPages = {totalPages}
/>
<div className='journeys'>
{!isLoading ? //render table if no pending api request
<table>
<tbody>
<tr>
<th className={`filter ${
sortMethod === 'departure' || sortMethod === '-departure' ? sortMethod : ''
//set appropriate class name based off of sortMethod to show asc/desc marker on table (check Journeys.css)
}`} onClick={() => {
setCurrentPage(1);
//revert user back to first page when sort method is updated
updateSortMarker('departure');
//update sort method, if sort method is already departure DESC set it to departure ASC, refer to function updateSortMarker
//continue same logic onto other th elements
}}>Departure</th>
<th className={`filter ${
sortMethod === 'return_time' || sortMethod === '-return_time' ? sortMethod : ''
}`} onClick={() => {
setCurrentPage(1);
updateSortMarker('return_time');
}}>Return</th>
<th>From</th>
<th>To</th>
<th className={`filter ${
sortMethod === 'distance_m' || sortMethod === '-distance_m' ? sortMethod : ''
}`} onClick={() => {
setCurrentPage(1);
updateSortMarker('distance_m');
}}>Distance</th>
<th className={`filter ${
sortMethod === 'duration_s' || sortMethod === '-duration_s' ? sortMethod : ''
}`} onClick={() => {
setCurrentPage(1);
updateSortMarker('duration_s');
}}>Duration</th>
</tr>
{journeys.map((journey) => {
return (
<tr>
<Journey key={journey.id} journey={journey} />
{/*render row for each journey in journey array, default 10 per api caller*/}
</tr>
)
})}
</tbody>
</table> :
<>
<PulseLoader color="#646cff" cssOverride={{
display: 'flex',
justifyContent: 'center',
margin: '1rem'
}}/>
{/*render pulse loader when waiting for new journeys*/}
</>
}
</div>
</div> :
<Error /> //display error message if api call unsuccesful
}
</>
)
}
export default Journeys;