Skip to content

Commit 95d9212

Browse files
authored
Merge pull request #37 from rzyuras/develop2
Develop2
2 parents 9605f52 + 97ceaee commit 95d9212

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

Sample-01/src/api/flights.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios';
22

3-
// const BASE_URL = 'http://localhost:3000';
3+
//const BASE_URL = 'http://localhost:3000';
44
const BASE_URL = 'https://rvvfas273i.execute-api.us-east-2.amazonaws.com/dev';
55

66
export const getAllFlights = async (token, filters = {}, pageNumber = 1) => {
@@ -45,7 +45,7 @@ export const getRecommendations = async ( token ) => {
4545
Authorization: `Bearer ${token}`
4646
};
4747
const response = await axios.get(`${BASE_URL}/flights/recommendations`, { headers });
48-
return response.data.flights;
48+
return response.data;
4949
} catch (error) {
5050
console.error("Error: details", error)
5151
throw error;

Sample-01/src/views/Recommendations.js

+56-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useEffect, useState } from 'react';
22
import { useAuth0 } from '@auth0/auth0-react';
3-
import { getRecommendations } from '../api/flights';
3+
import { getRecommendations, getFlightDetails } from '../api/flights';
44
import {
55
Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper,
66
TablePagination, Box
@@ -11,32 +11,74 @@ import { es } from 'date-fns/locale';
1111

1212
function Recommendations() {
1313
const { isAuthenticated, getAccessTokenSilently} = useAuth0();
14-
const [flights, setFlights] = useState([]);
14+
const [flights, setFlights] = useState(null);
15+
const [date_update, setUpdate] = useState();
16+
const [status, setStatus] = useState(null);
1517
//const [page, setPage] = useState(0);
1618
const navigate = useNavigate();
1719

1820
useEffect(() => {
1921
const fetchFlights = async () => {
20-
if (!isAuthenticated) return;
21-
try {
22-
const token = await getAccessTokenSilently();
23-
const flightsData = await getRecommendations(token);
24-
setFlights(flightsData);
25-
} catch (error) {
26-
console.error("Error fetching flights:", error);
22+
if (!isAuthenticated) return;
23+
24+
try {
25+
const token = await getAccessTokenSilently();
26+
const flightsData = await getRecommendations(token);
27+
console.log("get recommendations:", flightsData);
28+
29+
const id_1 = flightsData.flights.flight1;
30+
const id_2 = flightsData.flights.flight2;
31+
const id_3 = flightsData.flights.flight3;
32+
33+
if (!id_1 || !id_2 || !id_3) {
34+
setFlights(null);
35+
} else {
36+
const [flight1Data, flight2Data, flight3Data] = await Promise.all([
37+
getFlightDetails(token, id_1),
38+
getFlightDetails(token, id_2),
39+
getFlightDetails(token, id_3)
40+
]);
41+
setFlights([flight1Data.flight, flight2Data.flight, flight3Data.flight]);
42+
setUpdate(flightsData.flights.updatedAt);
2743
}
44+
} catch (error) {
45+
console.error("Error fetching recommendations:", error);
46+
}
2847
};
29-
48+
3049
fetchFlights();
3150
}, [getAccessTokenSilently, isAuthenticated]);
3251

52+
useEffect(() => {
53+
const fetchHearbeat = async () => {
54+
if (!isAuthenticated) return;
55+
try {
56+
const response = await fetch('https://worker.matiasoliva.me/heartbeat');
57+
const data = await response.json();
58+
setStatus(data.status);
59+
} catch (error) {
60+
console.error("Error fetching status:", error);
61+
}
62+
};
63+
64+
fetchHearbeat();
65+
const intervalId = setInterval(fetchHearbeat, 5000); // Adjust the interval as needed
66+
67+
return () => clearInterval(intervalId);
68+
}, [isAuthenticated]);
69+
3370
/*const handleChangePage = (event, newPage) => {
3471
setPage(newPage);
3572
};*/
3673

3774
if (!isAuthenticated) return <div>Please log in to view this content.</div>;
3875

3976
return (
77+
<div>
78+
<p>Status: {status ? 'Workers disponibles' : 'Workers no disponibles'}</p>
79+
{flights ? (
80+
<>
81+
<p>Última actualización: {format(new Date(date_update), "d 'de' MMMM yyyy 'a las' HH:mm", { locale: es })}</p>
4082
<Box sx={{ flexGrow: 1 , margin: 3}}>
4183
<TableContainer component={Paper}>
4284
<Table>
@@ -83,7 +125,10 @@ function Recommendations() {
83125
rowsPerPageOptions={[]}
84126
/>*/}
85127
</TableContainer>
86-
</Box>
128+
</Box></>) : (
129+
<p>No hay recomendaciones disponibles.</p>
130+
)}
131+
</div>
87132
);
88133
}
89134

0 commit comments

Comments
 (0)