Refactor and reformat frontend

This commit is contained in:
Santiago Lo Coco 2023-10-30 19:45:01 -03:00
parent 51db72ed54
commit c15455b008
36 changed files with 1071 additions and 1269 deletions

View File

@ -5,8 +5,6 @@ import "./FlightForm.css";
import { createFlight } from "../../Api"; import { createFlight } from "../../Api";
export const CreateFlight = () => { export const CreateFlight = () => {
const urlParams = new URLSearchParams(window.location.search);
const origin = urlParams.get('origin');
const navigate = useNavigate(); const navigate = useNavigate();
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [flight, setFlight] = useState<Flight>(); const [flight, setFlight] = useState<Flight>();

View File

@ -10,11 +10,6 @@ export const LogIn = () => {
return ( return (
<div className="Box Small"> <div className="Box Small">
<div className="Section"> <div className="Section">
<img
alt="logo"
className="Image"
src="https://www.seekpng.com/png/full/353-3537757_logo-itba.png"
/>
<div className="Section"> <div className="Section">
<Input <Input
placeholder="User" placeholder="User"

View File

@ -13,11 +13,6 @@ export const SignUp = () => {
return ( return (
<div className="Box Small"> <div className="Box Small">
<div className="Section"> <div className="Section">
<img
alt="logo"
className="Image"
src="https://www.seekpng.com/png/full/353-3537757_logo-itba.png"
/>
<div className="Section"> <div className="Section">
<Input <Input
type="email" type="email"

View File

@ -1,8 +1,3 @@
#!/bin/bash #!/bin/bash
curl -X DELETE api:5000/ping npm run test
curl -X POST api:5000/ping
# npm test
echo "NPM TEST"

View File

@ -1,5 +1,5 @@
import { Axios, AxiosError } from "axios"; import { Axios, AxiosError } from "axios";
import { Credentials, User, Flight } from "./Types"; import { Flight } from "./Types";
const instance = new Axios({ const instance = new Axios({
baseURL: process.env.REACT_APP_ENDPOINT ? process.env.REACT_APP_ENDPOINT : "http://127.0.0.1:5000/", baseURL: process.env.REACT_APP_ENDPOINT ? process.env.REACT_APP_ENDPOINT : "http://127.0.0.1:5000/",

View File

@ -1,15 +1,11 @@
import React, { useEffect } from "react";
import { useIsConnected } from "./hooks/useIsConnected"; import { useIsConnected } from "./hooks/useIsConnected";
import { Route, Routes } from "react-router"; import { Route, Routes } from "react-router";
import { Departure } from "./components/Home/Departure"; import { Departure } from "./components/Home/Departure";
import { Arrival } from "./components/Home/Arrival"; import { Arrival } from "./components/Home/Arrival";
import { Home } from "./components/Home/Home"; import { Home } from "./components/Home/Home";
import { Button } from "antd";
import { initDB } from "./db";
function App() { function App() {
const connection = useIsConnected(); const connection = useIsConnected();
// initDB();
return ( return (
<div className="App"> <div className="App">

View File

@ -11,7 +11,6 @@ interface Props {
} }
export const Arrival: React.FC<Props> = (props) => { export const Arrival: React.FC<Props> = (props) => {
// let origin = process.env.REACT_APP_ORIGIN;
let destination = process.env.REACT_APP_ORIGIN; let destination = process.env.REACT_APP_ORIGIN;
const { zones, error } = useFetchDestination(destination); const { zones, error } = useFetchDestination(destination);
@ -22,7 +21,6 @@ export const Arrival: React.FC<Props> = (props) => {
if (zones.length <= 10) { if (zones.length <= 10) {
return; return;
} }
// setStartIndex((prevIndex) => (prevIndex + 10) % zones.length);
setStartIndex((prevIndex) => (prevIndex + 10) >= zones.length ? 0 : (prevIndex + 10)); setStartIndex((prevIndex) => (prevIndex + 10) >= zones.length ? 0 : (prevIndex + 10));
}, 5000); }, 5000);
@ -34,29 +32,10 @@ export const Arrival: React.FC<Props> = (props) => {
<div > <div >
<h2>Arrival</h2> <h2>Arrival</h2>
<div className="Items"> <div className="Items">
{/* {(props.flights ? props.flights : zones).map((u) => {
return <Card key={u.id} flight={u} />;
})} */}
{/* <table>
<thead>
<tr>
<th>Flight code</th>
<th>Departure</th>
<th>Departure time</th>
<th>Destination</th>
<th>Arrival time</th>
<th>Gate</th>
<th>Status</th>
</tr>
</thead>
<tbody> */}
<Table> <Table>
<Thead> <Thead>
<Tr> <Tr>
<Th>Code</Th> <Th>Code</Th>
{/* <Th>Departure</Th> */}
{/* <Th>Time</Th> */}
{/* <Th>Destination</Th> */}
<Th>Origin</Th> <Th>Origin</Th>
<Th>Time</Th> <Th>Time</Th>
<Th>Gate</Th> <Th>Gate</Th>
@ -67,30 +46,18 @@ export const Arrival: React.FC<Props> = (props) => {
{zones.length > 0 && ( {zones.length > 0 && (
<> <>
{zones.slice(startIndex, startIndex + 10).map((flight) => ( {zones.slice(startIndex, startIndex + 10).map((flight) => (
// {/* {Array.from({ length: zones.length < 10 ? zones.length : 10 }).map((_, index) => {
// const flightIndex = (startIndex + index) % zones.length;
// const flight = zones[flightIndex];
// return ( */}
<Tr key={flight.id} className={flight.status === 'Delayed' ? 'delayed-flight' : ''}> <Tr key={flight.id} className={flight.status === 'Delayed' ? 'delayed-flight' : ''}>
{/* <td>{flight.id}</td> */} <Td>{flight.flight_code}</Td>
<Td>{flight.id}-{flight.flight_code}</Td>
<Td>{flight.origin}</Td> <Td>{flight.origin}</Td>
{/* <Td>{flight.departure_time}</Td> */}
{/* <Td>{flight.destination}</Td> */}
<Td>{flight.arrival_time}</Td> <Td>{flight.arrival_time}</Td>
<Td>{flight.gate}</Td> <Td>{flight.gate}</Td>
<Td>{flight.status}</Td> <Td>{flight.status}</Td>
</Tr> </Tr>
// );
))} ))}
{/* })} */}
</> </>
)} )}
</Tbody> </Tbody>
</Table> </Table>
{/* </tbody>
</table> */}
{error ? <div className="Disconnected">{error}</div> : <></>} {error ? <div className="Disconnected">{error}</div> : <></>}
</div> </div>
</div> </div>

View File

@ -1,30 +1,8 @@
/* .flight-card {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px;
border: 1px solid #ddd;
border-radius: 8px;
margin-bottom: 16px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
background-color: #fff;
transition: box-shadow 0.3s ease;
&:hover {
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.flight-details {
display: flex;
align-items: center;
}
} */
.flight-card { .flight-card {
display: flex; display: flex;
flex-direction: column; /* Display as a column instead of a row */ flex-direction: column;
justify-content: space-between; justify-content: space-between;
align-items: flex-start; /* Align items to the start of the column */ align-items: flex-start;
padding: 16px; padding: 16px;
border: 1px solid #ddd; border: 1px solid #ddd;
border-radius: 8px; border-radius: 8px;
@ -39,7 +17,7 @@
.flight-details { .flight-details {
display: flex; display: flex;
flex-direction: column; /* Display details as a column */ flex-direction: column;
margin-top: 16px; /* Add some space between the two rows */ margin-top: 16px;
} }
} }

View File

@ -1,46 +1,8 @@
// import React from "react";
// import { Avatar, Button } from "antd";
// interface FlightProps {
// flight_code: string;
// status: string;
// origin: string;
// destination: string;
// departure_time: string;
// arrival_time: string;
// gate: string;
// }
// interface CardProps {
// flight: FlightProps;
// }
// export const Card: React.FC<CardProps> = ({
// flight: { flight_code, status, origin, destination, departure_time, arrival_time, gate },
// }) => {
// console.log(flight_code)
// return (
// <div className="Card">
// <Avatar size="large">{flight_code.slice(0, 1).toUpperCase()}</Avatar>
// <div>
// <div>Name: {flight_code}</div>
// <div>Status: {status}</div>
// <div>Origin: {origin}</div>
// <div>Destination: {destination}</div>
// <div>Departure Time: {departure_time}</div>
// <div>Arrival Time: {arrival_time}</div>
// <div>Gate: {gate}</div>
// </div>
// 📍
// </div>
// );
// };
import React from "react"; import React from "react";
import { Avatar, Space, Typography, Tag } from "antd"; import { Avatar, Space, Typography, Tag } from "antd";
import { RightOutlined, ClockCircleOutlined, SwapOutlined, EnvironmentOutlined, CalendarOutlined } from "@ant-design/icons"; import { RightOutlined, ClockCircleOutlined, SwapOutlined, EnvironmentOutlined, CalendarOutlined } from "@ant-design/icons";
import "./Card.css"; // Import a CSS file for styling, you can create this file with your styles import "./Card.css";
interface FlightProps { interface FlightProps {
flight_code: string; flight_code: string;

View File

@ -12,7 +12,6 @@ interface Props {
export const Departure: React.FC<Props> = (props) => { export const Departure: React.FC<Props> = (props) => {
let origin = process.env.REACT_APP_ORIGIN; let origin = process.env.REACT_APP_ORIGIN;
// let destination = process.env.REACT_APP_ORIGIN;
const { zones, error } = useFetchOrigin(origin); const { zones, error } = useFetchOrigin(origin);
const [startIndex, setStartIndex] = useState(0); const [startIndex, setStartIndex] = useState(0);
@ -22,7 +21,6 @@ export const Departure: React.FC<Props> = (props) => {
if (zones.length <= 10) { if (zones.length <= 10) {
return; return;
} }
// setStartIndex((prevIndex) => (prevIndex + 10) % zones.length);
setStartIndex((prevIndex) => (prevIndex + 10) >= zones.length ? 0 : (prevIndex + 10)); setStartIndex((prevIndex) => (prevIndex + 10) >= zones.length ? 0 : (prevIndex + 10));
}, 5000); }, 5000);
@ -34,30 +32,12 @@ export const Departure: React.FC<Props> = (props) => {
<div > <div >
<h2>Departure</h2> <h2>Departure</h2>
<div className="Items"> <div className="Items">
{/* {(props.flights ? props.flights : zones).map((u) => {
return <Card key={u.id} flight={u} />;
})} */}
{/* <table>
<thead>
<tr>
<th>Flight code</th>
<th>Departure</th>
<th>Departure time</th>
<th>Destination</th>
<th>Arrival time</th>
<th>Gate</th>
<th>Status</th>
</tr>
</thead>
<tbody> */}
<Table> <Table>
<Thead> <Thead>
<Tr> <Tr>
<Th>Code</Th> <Th>Code</Th>
{/* <Th>Departure</Th> */}
<Th>Time</Th> <Th>Time</Th>
<Th>Destination</Th> <Th>Destination</Th>
{/* <Th>Arrival time</Th> */}
<Th>Gate</Th> <Th>Gate</Th>
<Th>Status</Th> <Th>Status</Th>
</Tr> </Tr>
@ -66,18 +46,10 @@ export const Departure: React.FC<Props> = (props) => {
{zones.length > 0 && ( {zones.length > 0 && (
<> <>
{zones.slice(startIndex, startIndex + 10).map((flight) => ( {zones.slice(startIndex, startIndex + 10).map((flight) => (
// {/* {Array.from({ length: zones.length < 10 ? zones.length : 10 }).map((_, index) => {
// const flightIndex = (startIndex + index) % zones.length;
// const flight = zones[flightIndex];
// return ( */}
<Tr key={flight.id} className={flight.status === 'Delayed' ? 'delayed-flight' : ''}> <Tr key={flight.id} className={flight.status === 'Delayed' ? 'delayed-flight' : ''}>
{/* <td>{flight.id}</td> */} <Td>{flight.flight_code}</Td>
<Td>{flight.id}-{flight.flight_code}</Td>
{/* <Td>{flight.origin}</Td> */}
<Td>{flight.departure_time}</Td> <Td>{flight.departure_time}</Td>
<Td>{flight.destination}</Td> <Td>{flight.destination}</Td>
{/* <Td>{flight.arrival_time}</Td> */}
<Td>{flight.gate}</Td> <Td>{flight.gate}</Td>
<Td>{flight.status}</Td> <Td>{flight.status}</Td>
</Tr> </Tr>
@ -99,13 +71,10 @@ export const Departure: React.FC<Props> = (props) => {
</> </>
) )
} }
{/* })} */}
</> </>
)} )}
</Tbody> </Tbody>
</Table> </Table>
{/* </tbody>
</table> */}
{error ? <div className="Disconnected">{error}</div> : <></>} {error ? <div className="Disconnected">{error}</div> : <></>}
</div> </div>
</div> </div>

View File

@ -16,7 +16,8 @@ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
background-color: #fff; background-color: #fff;
} }
th, td { th,
td {
border: 1px solid #ddd; border: 1px solid #ddd;
padding: 10px; padding: 10px;
text-align: left; text-align: left;
@ -37,6 +38,6 @@ color: #fff;
} }
.delayed-flight { .delayed-flight {
background-color: #ffcccc; /* Light red for delayed flights */ background-color: #ffcccc;
color: #ff0000; /* Red text for delayed flights */ color: #ff0000;
} }

View File

@ -14,7 +14,7 @@ body {
} }
button { button {
background-color: #4CAF50; /* Green */ background-color: #4CAF50;
border: none; border: none;
color: white; color: white;
padding: 15px 32px; padding: 15px 32px;
@ -29,5 +29,5 @@ body {
} }
button:hover { button:hover {
background-color: #45a049; /* Darker green on hover */ background-color: #45a049;
} }

View File

@ -7,10 +7,6 @@ export enum Stores {
Arrival = 'arrival' Arrival = 'arrival'
} }
interface EventTarget {
result: any
}
export const initDB = (): Promise<boolean | IDBDatabase> => { export const initDB = (): Promise<boolean | IDBDatabase> => {
return new Promise((resolve) => { return new Promise((resolve) => {
request = indexedDB.open('myDB'); request = indexedDB.open('myDB');
@ -42,35 +38,15 @@ export const initDB = (): Promise<boolean|IDBDatabase> => {
export const addData = <T>(storeName: string, data: T): Promise<T | string | null> => { export const addData = <T>(storeName: string, data: T): Promise<T | string | null> => {
return new Promise((resolve) => { return new Promise((resolve) => {
// request = indexedDB.open('myDB', version);
// request.onsuccess = (e) => {
// let req = (e.target as IDBOpenDBRequest)
// db = req.result;
const tx = db.transaction(storeName, 'readwrite'); const tx = db.transaction(storeName, 'readwrite');
const store = tx.objectStore(storeName); const store = tx.objectStore(storeName);
store.add(data); store.add(data);
resolve(data); resolve(data);
// };
// request.onerror = () => {
// const error = request.error?.message
// if (error) {
// resolve(error);
// } else {
// resolve('Unknown error');
// }
// };
}); });
}; };
export const deleteData = (storeName: string, key: number): Promise<boolean> => { export const deleteData = (storeName: string, key: number): Promise<boolean> => {
return new Promise((resolve) => { return new Promise((resolve) => {
// request = indexedDB.open('myDB', version);
// request.onsuccess = (e) => {
// let req = (e.target as IDBOpenDBRequest)
// db = req.result;
const tx = db.transaction(storeName, 'readwrite'); const tx = db.transaction(storeName, 'readwrite');
const store = tx.objectStore(storeName); const store = tx.objectStore(storeName);
const res = store.delete(key); const res = store.delete(key);
@ -83,17 +59,11 @@ export const deleteData = (storeName: string, key: number): Promise<boolean> =>
console.log("error") console.log("error")
resolve(false); resolve(false);
} }
// };
}); });
}; };
export const updateData = <T>(storeName: string, key: number, data: T): Promise<T | string | null> => { export const updateData = <T>(storeName: string, key: number, data: T): Promise<T | string | null> => {
return new Promise((resolve) => { return new Promise((resolve) => {
// request = indexedDB.open('myDB', version);
// request.onsuccess = (e) => {
// let req = (e.target as IDBOpenDBRequest)
// db = req.result;
const tx = db.transaction(storeName, 'readwrite'); const tx = db.transaction(storeName, 'readwrite');
const store = tx.objectStore(storeName); const store = tx.objectStore(storeName);
const res = store.get(key); const res = store.get(key);
@ -105,27 +75,17 @@ export const updateData = <T>(storeName: string, key: number, data: T): Promise<
res.onerror = () => { res.onerror = () => {
resolve(null); resolve(null);
} }
// };
}); });
}; };
export const getStoreData = <T>(storeName: Stores): Promise<T[] | null> => { export const getStoreData = <T>(storeName: Stores): Promise<T[] | null> => {
return new Promise((resolve) => { return new Promise((resolve) => {
// request = indexedDB.open('myDB');
// request.onsuccess = (e) => {
// let req = (e.target as IDBOpenDBRequest)
// if (!req.result) {
// resolve(null);
// }
// db = req.result;
const tx = db.transaction(storeName, 'readonly'); const tx = db.transaction(storeName, 'readonly');
const store = tx.objectStore(storeName); const store = tx.objectStore(storeName);
const res = store.getAll(); const res = store.getAll();
res.onsuccess = () => { res.onsuccess = () => {
resolve(res.result); resolve(res.result);
}; };
// };
}); });
}; };

View File

@ -1,13 +1,12 @@
import React, { useEffect } from "react"; import { useEffect } from "react";
import { useState } from "react"; import { useState } from "react";
import { User, Flight } from "../Types"; import { Flight } from "../Types";
import { fetchZones } from "../Api"; import { fetchZones } from "../Api";
import { Stores, addData, deleteData, getStoreData, updateData, initDB } from '../db'; import { Stores, addData, deleteData, getStoreData, updateData, initDB } from '../db';
export const useFetchDestination = (destination: string | undefined) => { export const useFetchDestination = (destination: string | undefined) => {
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [zones, setZones] = useState<Flight[]>([]); const [zones, setZones] = useState<Flight[]>([]);
// const [today, setToday] = useState<Date>(new Date());
const isToday = (someDate: Date) => { const isToday = (someDate: Date) => {
let today = new Date(); let today = new Date();
@ -18,7 +17,6 @@ export const useFetchDestination = (destination: string | undefined) => {
useEffect(() => { useEffect(() => {
const intervalId = setInterval(() => { const intervalId = setInterval(() => {
// {isToday(new Date(flight.departure_time)) ? 's' : 'n'}
let today = localStorage.getItem('date') let today = localStorage.getItem('date')
if (today && !isToday(new Date(today))) { if (today && !isToday(new Date(today))) {
@ -64,7 +62,6 @@ export const useFetchDestination = (destination: string | undefined) => {
toAdd.push(u) toAdd.push(u)
} }
}) })
// toAdd.sort((a,b) => new Date(a.departure_time).getTime() - new Date(b.departure_time).getTime())
toAdd.sort((a, b) => new Date(a.departure_time).getTime() - new Date(b.departure_time).getTime()) toAdd.sort((a, b) => new Date(a.departure_time).getTime() - new Date(b.departure_time).getTime())
setZones(toAdd); setZones(toAdd);
}) })
@ -80,7 +77,6 @@ export const useFetchDestination = (destination: string | undefined) => {
const intervalId = setInterval(() => { const intervalId = setInterval(() => {
let lastUpdate = localStorage.getItem('lastUpdated') let lastUpdate = localStorage.getItem('lastUpdated')
let newUpdate = new Date().toISOString() let newUpdate = new Date().toISOString()
// let newUpdate = new Date().toTimeString()
fetchZones(undefined, destination, lastUpdate) fetchZones(undefined, destination, lastUpdate)
.then((data) => { .then((data) => {
@ -112,8 +108,6 @@ export const useFetchDestination = (destination: string | undefined) => {
} }
}); });
// console.log(toAdd)
// console.log(toRemove)
let filtered = data.filter(o => let filtered = data.filter(o =>
!toAdd.some(b => { return o.id === b.id }) && !toRemove.some(b => { return o.id === b.id }) && !(new Date(o.departure_time) < new Date()) !toAdd.some(b => { return o.id === b.id }) && !toRemove.some(b => { return o.id === b.id }) && !(new Date(o.departure_time) < new Date())
) )
@ -124,7 +118,6 @@ export const useFetchDestination = (destination: string | undefined) => {
addData(Stores.Arrival, c) addData(Stores.Arrival, c)
}) })
// newArray.sort((a,b) => new Date(a.departure_time).getTime() - new Date(b.departure_time).getTime())
newArray.sort((a, b) => new Date(a.departure_time).getTime() - new Date(b.departure_time).getTime()) newArray.sort((a, b) => new Date(a.departure_time).getTime() - new Date(b.departure_time).getTime())
setZones(newArray); setZones(newArray);
}) })

View File

@ -1,13 +1,12 @@
import React, { useEffect } from "react"; import { useEffect } from "react";
import { useState } from "react"; import { useState } from "react";
import { User, Flight } from "../Types"; import { Flight } from "../Types";
import { fetchZones } from "../Api"; import { fetchZones } from "../Api";
import { Stores, addData, deleteData, getStoreData, updateData, initDB } from '../db'; import { Stores, addData, deleteData, getStoreData, updateData, initDB } from '../db';
export const useFetchOrigin = (origin: string | undefined) => { export const useFetchOrigin = (origin: string | undefined) => {
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [zones, setZones] = useState<Flight[]>([]); const [zones, setZones] = useState<Flight[]>([]);
// const [today, setToday] = useState<Date>(new Date());
const isToday = (someDate: Date) => { const isToday = (someDate: Date) => {
let today = new Date(); let today = new Date();
@ -18,7 +17,6 @@ export const useFetchOrigin = (origin: string | undefined) => {
useEffect(() => { useEffect(() => {
const intervalId = setInterval(() => { const intervalId = setInterval(() => {
// {isToday(new Date(flight.departure_time)) ? 's' : 'n'}
let today = localStorage.getItem('date') let today = localStorage.getItem('date')
if (today && !isToday(new Date(today))) { if (today && !isToday(new Date(today))) {
@ -64,7 +62,6 @@ export const useFetchOrigin = (origin: string | undefined) => {
toAdd.push(u) toAdd.push(u)
} }
}) })
// toAdd.sort((a,b) => new Date(a.departure_time).getTime() - new Date(b.departure_time).getTime())
toAdd.sort((a, b) => new Date(a.origin).getTime() - new Date(b.origin).getTime()) toAdd.sort((a, b) => new Date(a.origin).getTime() - new Date(b.origin).getTime())
setZones(toAdd); setZones(toAdd);
}) })
@ -80,7 +77,6 @@ export const useFetchOrigin = (origin: string | undefined) => {
const intervalId = setInterval(() => { const intervalId = setInterval(() => {
let lastUpdate = localStorage.getItem('lastUpdated') let lastUpdate = localStorage.getItem('lastUpdated')
let newUpdate = new Date().toISOString() let newUpdate = new Date().toISOString()
// let newUpdate = new Date().toTimeString()
fetchZones(origin, undefined, lastUpdate) fetchZones(origin, undefined, lastUpdate)
.then((data) => { .then((data) => {
@ -112,8 +108,6 @@ export const useFetchOrigin = (origin: string | undefined) => {
} }
}); });
// console.log(toAdd)
// console.log(toRemove)
let filtered = data.filter(o => let filtered = data.filter(o =>
!toAdd.some(b => { return o.id === b.id }) && !toRemove.some(b => { return o.id === b.id }) && !(new Date(o.departure_time) < new Date()) !toAdd.some(b => { return o.id === b.id }) && !toRemove.some(b => { return o.id === b.id }) && !(new Date(o.departure_time) < new Date())
) )
@ -124,7 +118,6 @@ export const useFetchOrigin = (origin: string | undefined) => {
addData(Stores.Departure, c) addData(Stores.Departure, c)
}) })
// newArray.sort((a,b) => new Date(a.departure_time).getTime() - new Date(b.departure_time).getTime())
newArray.sort((a, b) => new Date(a.origin).getTime() - new Date(b.origin).getTime()) newArray.sort((a, b) => new Date(a.origin).getTime() - new Date(b.origin).getTime())
setZones(newArray); setZones(newArray);
}) })