Add flight comparator web app with full scan pipeline
Full-stack flight price scanner built on fast-flights v3 (SOCS cookie bypass): Backend (FastAPI + SQLite): - REST API with rate limiting, Pydantic v2 validation, paginated responses - Scan pipeline: resolves airports, queries every day in the window, saves individual flights + aggregate route stats to SQLite - Background async scan processor with real-time progress tracking - Airport search endpoint backed by OpenFlights dataset - Daily scan window (all dates, not monthly samples) Frontend (React 19 + TypeScript + Tailwind CSS v4): - Dashboard with live scan status and recent scans - Create scan form: country mode or specific airports (searchable dropdown) - Scan detail page with expandable route rows showing individual flights (date, airline, departure, arrival, price) loaded on demand - AirportSearch component with debounced live search and multi-select Database: - scans → routes → flights schema with FK cascade and auto-update triggers - Migrations for schema evolution (relaxed country constraint) Tests: - 74 tests: unit + integration, isolated per-test SQLite DB - Confirmed flight fixtures in tests/confirmed_flights.json (50 real flights, BDS→FMM Ryanair + BDS→DUS Eurowings, scraped Feb 2026) - Integration tests parametrized from confirmed routes Docker: - Multi-stage builds, Compose orchestration, Nginx reverse proxy Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
24
flight-comparator/frontend/.gitignore
vendored
Normal file
24
flight-comparator/frontend/.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
23
flight-comparator/frontend/eslint.config.js
Normal file
23
flight-comparator/frontend/eslint.config.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import js from '@eslint/js'
|
||||
import globals from 'globals'
|
||||
import reactHooks from 'eslint-plugin-react-hooks'
|
||||
import reactRefresh from 'eslint-plugin-react-refresh'
|
||||
import tseslint from 'typescript-eslint'
|
||||
import { defineConfig, globalIgnores } from 'eslint/config'
|
||||
|
||||
export default defineConfig([
|
||||
globalIgnores(['dist']),
|
||||
{
|
||||
files: ['**/*.{ts,tsx}'],
|
||||
extends: [
|
||||
js.configs.recommended,
|
||||
tseslint.configs.recommended,
|
||||
reactHooks.configs.flat.recommended,
|
||||
reactRefresh.configs.vite,
|
||||
],
|
||||
languageOptions: {
|
||||
ecmaVersion: 2020,
|
||||
globals: globals.browser,
|
||||
},
|
||||
},
|
||||
])
|
||||
13
flight-comparator/frontend/index.html
Normal file
13
flight-comparator/frontend/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>frontend</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
6
flight-comparator/frontend/postcss.config.js
Normal file
6
flight-comparator/frontend/postcss.config.js
Normal file
@@ -0,0 +1,6 @@
|
||||
export default {
|
||||
plugins: {
|
||||
'@tailwindcss/postcss': {},
|
||||
autoprefixer: {},
|
||||
},
|
||||
}
|
||||
1
flight-comparator/frontend/public/vite.svg
Normal file
1
flight-comparator/frontend/public/vite.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 1.5 KiB |
42
flight-comparator/frontend/src/App.css
Normal file
42
flight-comparator/frontend/src/App.css
Normal file
@@ -0,0 +1,42 @@
|
||||
#root {
|
||||
max-width: 1280px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.logo {
|
||||
height: 6em;
|
||||
padding: 1.5em;
|
||||
will-change: filter;
|
||||
transition: filter 300ms;
|
||||
}
|
||||
.logo:hover {
|
||||
filter: drop-shadow(0 0 2em #646cffaa);
|
||||
}
|
||||
.logo.react:hover {
|
||||
filter: drop-shadow(0 0 2em #61dafbaa);
|
||||
}
|
||||
|
||||
@keyframes logo-spin {
|
||||
from {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: no-preference) {
|
||||
a:nth-of-type(2) .logo {
|
||||
animation: logo-spin infinite 20s linear;
|
||||
}
|
||||
}
|
||||
|
||||
.card {
|
||||
padding: 2em;
|
||||
}
|
||||
|
||||
.read-the-docs {
|
||||
color: #888;
|
||||
}
|
||||
28
flight-comparator/frontend/src/App.tsx
Normal file
28
flight-comparator/frontend/src/App.tsx
Normal file
@@ -0,0 +1,28 @@
|
||||
import { BrowserRouter, Routes, Route } from 'react-router-dom';
|
||||
import Layout from './components/Layout';
|
||||
import Dashboard from './pages/Dashboard';
|
||||
import Scans from './pages/Scans';
|
||||
import ScanDetails from './pages/ScanDetails';
|
||||
import Airports from './pages/Airports';
|
||||
import Logs from './pages/Logs';
|
||||
import ErrorBoundary from './components/ErrorBoundary';
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<ErrorBoundary>
|
||||
<BrowserRouter>
|
||||
<Routes>
|
||||
<Route path="/" element={<Layout />}>
|
||||
<Route index element={<Dashboard />} />
|
||||
<Route path="scans" element={<Scans />} />
|
||||
<Route path="scans/:id" element={<ScanDetails />} />
|
||||
<Route path="airports" element={<Airports />} />
|
||||
<Route path="logs" element={<Logs />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</BrowserRouter>
|
||||
</ErrorBoundary>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
145
flight-comparator/frontend/src/api.ts
Normal file
145
flight-comparator/frontend/src/api.ts
Normal file
@@ -0,0 +1,145 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: '/api/v1',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Types
|
||||
export interface Scan {
|
||||
id: number;
|
||||
origin: string;
|
||||
country: string;
|
||||
start_date: string;
|
||||
end_date: string;
|
||||
status: 'pending' | 'running' | 'completed' | 'failed';
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
total_routes: number;
|
||||
routes_scanned: number;
|
||||
total_flights: number;
|
||||
error_message?: string;
|
||||
seat_class: string;
|
||||
adults: number;
|
||||
}
|
||||
|
||||
export interface Route {
|
||||
id: number;
|
||||
scan_id: number;
|
||||
destination: string;
|
||||
destination_name: string;
|
||||
destination_city?: string;
|
||||
flight_count: number;
|
||||
airlines: string[];
|
||||
min_price?: number;
|
||||
max_price?: number;
|
||||
avg_price?: number;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface Flight {
|
||||
id: number;
|
||||
scan_id: number;
|
||||
destination: string;
|
||||
date: string;
|
||||
airline?: string;
|
||||
departure_time?: string;
|
||||
arrival_time?: string;
|
||||
price?: number;
|
||||
stops: number;
|
||||
}
|
||||
|
||||
export interface Airport {
|
||||
iata: string;
|
||||
name: string;
|
||||
city: string;
|
||||
country: string;
|
||||
}
|
||||
|
||||
export interface LogEntry {
|
||||
timestamp: string;
|
||||
level: string;
|
||||
message: string;
|
||||
module?: string;
|
||||
function?: string;
|
||||
line?: number;
|
||||
}
|
||||
|
||||
export interface PaginatedResponse<T> {
|
||||
data: T[];
|
||||
pagination: {
|
||||
page: number;
|
||||
limit: number;
|
||||
total: number;
|
||||
pages: number;
|
||||
has_next: boolean;
|
||||
has_prev: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export interface CreateScanRequest {
|
||||
origin: string;
|
||||
country?: string; // Optional: provide either country or destinations
|
||||
destinations?: string[]; // Optional: provide either country or destinations
|
||||
start_date?: string;
|
||||
end_date?: string;
|
||||
window_months?: number;
|
||||
seat_class?: 'economy' | 'premium' | 'business' | 'first';
|
||||
adults?: number;
|
||||
}
|
||||
|
||||
export interface CreateScanResponse {
|
||||
status: string;
|
||||
id: number;
|
||||
scan: Scan;
|
||||
}
|
||||
|
||||
// API functions
|
||||
export const scanApi = {
|
||||
list: (page = 1, limit = 20, status?: string) => {
|
||||
const params: any = { page, limit };
|
||||
if (status) params.status = status;
|
||||
return api.get<PaginatedResponse<Scan>>('/scans', { params });
|
||||
},
|
||||
|
||||
get: (id: number) => {
|
||||
return api.get<Scan>(`/scans/${id}`);
|
||||
},
|
||||
|
||||
create: (data: CreateScanRequest) => {
|
||||
return api.post<CreateScanResponse>('/scans', data);
|
||||
},
|
||||
|
||||
getRoutes: (id: number, page = 1, limit = 20) => {
|
||||
return api.get<PaginatedResponse<Route>>(`/scans/${id}/routes`, {
|
||||
params: { page, limit }
|
||||
});
|
||||
},
|
||||
|
||||
getFlights: (id: number, destination?: string, page = 1, limit = 50) => {
|
||||
const params: Record<string, unknown> = { page, limit };
|
||||
if (destination) params.destination = destination;
|
||||
return api.get<PaginatedResponse<Flight>>(`/scans/${id}/flights`, { params });
|
||||
},
|
||||
};
|
||||
|
||||
export const airportApi = {
|
||||
search: (query: string, page = 1, limit = 20) => {
|
||||
return api.get<PaginatedResponse<Airport>>('/airports', {
|
||||
params: { q: query, page, limit }
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export const logApi = {
|
||||
list: (page = 1, limit = 50, level?: string, search?: string) => {
|
||||
const params: any = { page, limit };
|
||||
if (level) params.level = level;
|
||||
if (search) params.search = search;
|
||||
return api.get<PaginatedResponse<LogEntry>>('/logs', { params });
|
||||
},
|
||||
};
|
||||
|
||||
export default api;
|
||||
1
flight-comparator/frontend/src/assets/react.svg
Normal file
1
flight-comparator/frontend/src/assets/react.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 4.0 KiB |
132
flight-comparator/frontend/src/components/AirportSearch.tsx
Normal file
132
flight-comparator/frontend/src/components/AirportSearch.tsx
Normal file
@@ -0,0 +1,132 @@
|
||||
import { useState, useEffect, useRef } from 'react';
|
||||
import { airportApi } from '../api';
|
||||
import type { Airport } from '../api';
|
||||
|
||||
interface AirportSearchProps {
|
||||
value: string;
|
||||
onChange: (value: string) => void;
|
||||
placeholder?: string;
|
||||
clearAfterSelect?: boolean;
|
||||
required?: boolean;
|
||||
}
|
||||
|
||||
export default function AirportSearch({ value, onChange, placeholder, clearAfterSelect, required = true }: AirportSearchProps) {
|
||||
const [query, setQuery] = useState(value);
|
||||
const [airports, setAirports] = useState<Airport[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [showDropdown, setShowDropdown] = useState(false);
|
||||
const debounceTimer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
setQuery(value);
|
||||
}, [value]);
|
||||
|
||||
useEffect(() => {
|
||||
// Close dropdown when clicking outside
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (containerRef.current && !containerRef.current.contains(event.target as Node)) {
|
||||
setShowDropdown(false);
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||
}, []);
|
||||
|
||||
const searchAirports = async (searchQuery: string) => {
|
||||
if (searchQuery.length < 2) {
|
||||
setAirports([]);
|
||||
setShowDropdown(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await airportApi.search(searchQuery, 1, 10);
|
||||
setAirports(response.data.data);
|
||||
setShowDropdown(true);
|
||||
} catch (error) {
|
||||
console.error('Failed to search airports:', error);
|
||||
setAirports([]);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const newQuery = e.target.value.toUpperCase();
|
||||
setQuery(newQuery);
|
||||
onChange(newQuery);
|
||||
|
||||
// Debounce search
|
||||
if (debounceTimer.current) {
|
||||
clearTimeout(debounceTimer.current);
|
||||
}
|
||||
|
||||
debounceTimer.current = setTimeout(() => {
|
||||
searchAirports(newQuery);
|
||||
}, 300);
|
||||
};
|
||||
|
||||
const handleSelectAirport = (airport: Airport) => {
|
||||
onChange(airport.iata);
|
||||
if (clearAfterSelect) {
|
||||
setQuery('');
|
||||
setAirports([]);
|
||||
} else {
|
||||
setQuery(airport.iata);
|
||||
}
|
||||
setShowDropdown(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<div ref={containerRef} className="relative">
|
||||
<input
|
||||
type="text"
|
||||
value={query}
|
||||
onChange={handleInputChange}
|
||||
onFocus={() => {
|
||||
if (airports.length > 0) {
|
||||
setShowDropdown(true);
|
||||
}
|
||||
}}
|
||||
maxLength={3}
|
||||
required={required}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
placeholder={placeholder || 'Search airports...'}
|
||||
/>
|
||||
|
||||
{/* Dropdown */}
|
||||
{showDropdown && airports.length > 0 && (
|
||||
<div className="absolute z-10 w-full mt-1 bg-white border border-gray-300 rounded-md shadow-lg max-h-60 overflow-y-auto">
|
||||
{loading && (
|
||||
<div className="px-4 py-2 text-sm text-gray-500">
|
||||
Searching...
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!loading && airports.map((airport) => (
|
||||
<div
|
||||
key={airport.iata}
|
||||
onClick={() => handleSelectAirport(airport)}
|
||||
className="px-4 py-2 hover:bg-gray-100 cursor-pointer"
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<span className="font-medium text-gray-900">{airport.iata}</span>
|
||||
<span className="ml-2 text-sm text-gray-600">
|
||||
{airport.name}
|
||||
</span>
|
||||
</div>
|
||||
<span className="text-sm text-gray-500">
|
||||
{airport.city}, {airport.country}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
73
flight-comparator/frontend/src/components/ErrorBoundary.tsx
Normal file
73
flight-comparator/frontend/src/components/ErrorBoundary.tsx
Normal file
@@ -0,0 +1,73 @@
|
||||
import React from 'react';
|
||||
|
||||
interface Props {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
interface State {
|
||||
hasError: boolean;
|
||||
error: Error | null;
|
||||
}
|
||||
|
||||
export default class ErrorBoundary extends React.Component<Props, State> {
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
this.state = { hasError: false, error: null };
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error: Error): State {
|
||||
return { hasError: true, error };
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
|
||||
console.error('Error caught by boundary:', error, errorInfo);
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
||||
<div className="max-w-md w-full bg-white shadow-lg rounded-lg p-6">
|
||||
<div className="flex items-center justify-center w-12 h-12 mx-auto bg-red-100 rounded-full">
|
||||
<svg
|
||||
className="w-6 h-6 text-red-600"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M6 18L18 6M6 6l12 12"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
<h2 className="mt-4 text-xl font-bold text-center text-gray-900">
|
||||
Something went wrong
|
||||
</h2>
|
||||
<p className="mt-2 text-sm text-center text-gray-600">
|
||||
{this.state.error?.message || 'An unexpected error occurred'}
|
||||
</p>
|
||||
<div className="mt-6 flex justify-center space-x-3">
|
||||
<button
|
||||
onClick={() => window.location.href = '/'}
|
||||
className="px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-md text-sm font-medium"
|
||||
>
|
||||
Go to Dashboard
|
||||
</button>
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
className="px-4 py-2 border border-gray-300 hover:bg-gray-50 text-gray-700 rounded-md text-sm font-medium"
|
||||
>
|
||||
Reload Page
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
72
flight-comparator/frontend/src/components/Layout.tsx
Normal file
72
flight-comparator/frontend/src/components/Layout.tsx
Normal file
@@ -0,0 +1,72 @@
|
||||
import { Link, Outlet, useLocation } from 'react-router-dom';
|
||||
|
||||
export default function Layout() {
|
||||
const location = useLocation();
|
||||
|
||||
const isActive = (path: string) => {
|
||||
return location.pathname === path;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
{/* Header */}
|
||||
<header className="bg-white shadow-sm border-b border-gray-200">
|
||||
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
|
||||
<div className="flex flex-col sm:flex-row justify-between items-center gap-4">
|
||||
<h1 className="text-2xl font-bold text-blue-600 flex items-center">
|
||||
<span className="text-3xl mr-2">✈️</span>
|
||||
Flight Radar
|
||||
</h1>
|
||||
<nav className="flex flex-wrap justify-center gap-2">
|
||||
<Link
|
||||
to="/"
|
||||
className={`px-3 py-2 rounded-md text-sm font-medium ${
|
||||
isActive('/')
|
||||
? 'bg-blue-500 text-white'
|
||||
: 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
>
|
||||
Dashboard
|
||||
</Link>
|
||||
<Link
|
||||
to="/scans"
|
||||
className={`px-3 py-2 rounded-md text-sm font-medium ${
|
||||
isActive('/scans')
|
||||
? 'bg-blue-500 text-white'
|
||||
: 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
>
|
||||
Scans
|
||||
</Link>
|
||||
<Link
|
||||
to="/airports"
|
||||
className={`px-3 py-2 rounded-md text-sm font-medium ${
|
||||
isActive('/airports')
|
||||
? 'bg-blue-500 text-white'
|
||||
: 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
>
|
||||
Airports
|
||||
</Link>
|
||||
<Link
|
||||
to="/logs"
|
||||
className={`px-3 py-2 rounded-md text-sm font-medium ${
|
||||
isActive('/logs')
|
||||
? 'bg-blue-500 text-white'
|
||||
: 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
>
|
||||
Logs
|
||||
</Link>
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{/* Main Content */}
|
||||
<main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||
<Outlet />
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
15
flight-comparator/frontend/src/components/LoadingSpinner.tsx
Normal file
15
flight-comparator/frontend/src/components/LoadingSpinner.tsx
Normal file
@@ -0,0 +1,15 @@
|
||||
export default function LoadingSpinner({ size = 'md' }: { size?: 'sm' | 'md' | 'lg' }) {
|
||||
const sizeClasses = {
|
||||
sm: 'w-4 h-4',
|
||||
md: 'w-8 h-8',
|
||||
lg: 'w-12 h-12',
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex justify-center items-center">
|
||||
<div
|
||||
className={`${sizeClasses[size]} border-4 border-blue-200 border-t-blue-600 rounded-full animate-spin`}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
97
flight-comparator/frontend/src/components/Toast.tsx
Normal file
97
flight-comparator/frontend/src/components/Toast.tsx
Normal file
@@ -0,0 +1,97 @@
|
||||
import { useEffect } from 'react';
|
||||
|
||||
export type ToastType = 'success' | 'error' | 'info' | 'warning';
|
||||
|
||||
interface ToastProps {
|
||||
message: string;
|
||||
type: ToastType;
|
||||
onClose: () => void;
|
||||
duration?: number;
|
||||
}
|
||||
|
||||
export default function Toast({ message, type, onClose, duration = 5000 }: ToastProps) {
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(onClose, duration);
|
||||
return () => clearTimeout(timer);
|
||||
}, [duration, onClose]);
|
||||
|
||||
const getColors = () => {
|
||||
switch (type) {
|
||||
case 'success':
|
||||
return 'bg-green-50 border-green-200 text-green-800';
|
||||
case 'error':
|
||||
return 'bg-red-50 border-red-200 text-red-800';
|
||||
case 'warning':
|
||||
return 'bg-yellow-50 border-yellow-200 text-yellow-800';
|
||||
case 'info':
|
||||
return 'bg-blue-50 border-blue-200 text-blue-800';
|
||||
}
|
||||
};
|
||||
|
||||
const getIcon = () => {
|
||||
switch (type) {
|
||||
case 'success':
|
||||
return (
|
||||
<svg className="w-5 h-5 text-green-600" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
case 'error':
|
||||
return (
|
||||
<svg className="w-5 h-5 text-red-600" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
case 'warning':
|
||||
return (
|
||||
<svg className="w-5 h-5 text-yellow-600" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
case 'info':
|
||||
return (
|
||||
<svg className="w-5 h-5 text-blue-600" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`fixed bottom-4 right-4 flex items-center p-4 border rounded-lg shadow-lg ${getColors()} animate-slide-up`}
|
||||
style={{ minWidth: '300px', maxWidth: '500px' }}
|
||||
>
|
||||
<div className="flex-shrink-0">{getIcon()}</div>
|
||||
<p className="ml-3 text-sm font-medium flex-1">{message}</p>
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="ml-4 flex-shrink-0 text-gray-400 hover:text-gray-600"
|
||||
>
|
||||
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
18
flight-comparator/frontend/src/index.css
Normal file
18
flight-comparator/frontend/src/index.css
Normal file
@@ -0,0 +1,18 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
@keyframes slide-up {
|
||||
from {
|
||||
transform: translateY(100%);
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
transform: translateY(0);
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.animate-slide-up {
|
||||
animation: slide-up 0.3s ease-out;
|
||||
}
|
||||
10
flight-comparator/frontend/src/main.tsx
Normal file
10
flight-comparator/frontend/src/main.tsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import { StrictMode } from 'react'
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import './index.css'
|
||||
import App from './App.tsx'
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
)
|
||||
144
flight-comparator/frontend/src/pages/Airports.tsx
Normal file
144
flight-comparator/frontend/src/pages/Airports.tsx
Normal file
@@ -0,0 +1,144 @@
|
||||
import { useState } from 'react';
|
||||
import { airportApi } from '../api';
|
||||
import type { Airport } from '../api';
|
||||
|
||||
export default function Airports() {
|
||||
const [query, setQuery] = useState('');
|
||||
const [airports, setAirports] = useState<Airport[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [page, setPage] = useState(1);
|
||||
const [totalPages, setTotalPages] = useState(0);
|
||||
const [total, setTotal] = useState(0);
|
||||
|
||||
const handleSearch = async (searchQuery: string, searchPage = 1) => {
|
||||
if (searchQuery.length < 2) {
|
||||
setAirports([]);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await airportApi.search(searchQuery, searchPage, 20);
|
||||
setAirports(response.data.data);
|
||||
setTotalPages(response.data.pagination.pages);
|
||||
setTotal(response.data.pagination.total);
|
||||
setPage(searchPage);
|
||||
} catch (error) {
|
||||
console.error('Failed to search airports:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
handleSearch(query, 1);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-900 mb-6">Airport Search</h2>
|
||||
|
||||
{/* Search Form */}
|
||||
<div className="bg-white rounded-lg shadow p-6 mb-6">
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="flex space-x-4">
|
||||
<input
|
||||
type="text"
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
placeholder="Search by IATA code, city, or airport name..."
|
||||
className="flex-1 px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading || query.length < 2}
|
||||
className="px-6 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-md font-medium disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
{loading ? 'Searching...' : 'Search'}
|
||||
</button>
|
||||
</div>
|
||||
<p className="mt-2 text-sm text-gray-500">
|
||||
Enter at least 2 characters to search
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{/* Results */}
|
||||
{airports.length > 0 && (
|
||||
<div className="bg-white rounded-lg shadow overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-gray-200 flex justify-between items-center">
|
||||
<h3 className="text-lg font-semibold text-gray-900">
|
||||
Search Results
|
||||
</h3>
|
||||
<span className="text-sm text-gray-500">
|
||||
{total} airport{total !== 1 ? 's' : ''} found
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="divide-y divide-gray-200">
|
||||
{airports.map((airport) => (
|
||||
<div key={airport.iata} className="px-6 py-4 hover:bg-gray-50">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex-1">
|
||||
<div className="flex items-center space-x-3">
|
||||
<span className="font-bold text-lg text-blue-600">
|
||||
{airport.iata}
|
||||
</span>
|
||||
<span className="font-medium text-gray-900">
|
||||
{airport.name}
|
||||
</span>
|
||||
</div>
|
||||
<div className="mt-1 text-sm text-gray-500">
|
||||
{airport.city}, {airport.country}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => {
|
||||
navigator.clipboard.writeText(airport.iata);
|
||||
}}
|
||||
className="px-3 py-1 text-sm text-blue-600 hover:bg-blue-50 rounded"
|
||||
>
|
||||
Copy Code
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Pagination */}
|
||||
{totalPages > 1 && (
|
||||
<div className="px-6 py-4 border-t border-gray-200 flex justify-between items-center">
|
||||
<div className="text-sm text-gray-500">
|
||||
Page {page} of {totalPages}
|
||||
</div>
|
||||
<div className="flex space-x-2">
|
||||
<button
|
||||
onClick={() => handleSearch(query, page - 1)}
|
||||
disabled={page === 1 || loading}
|
||||
className="px-3 py-1 border border-gray-300 rounded text-sm disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50"
|
||||
>
|
||||
Previous
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleSearch(query, page + 1)}
|
||||
disabled={page === totalPages || loading}
|
||||
className="px-3 py-1 border border-gray-300 rounded text-sm disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50"
|
||||
>
|
||||
Next
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Empty State */}
|
||||
{!loading && airports.length === 0 && query.length >= 2 && (
|
||||
<div className="bg-white rounded-lg shadow p-12 text-center">
|
||||
<p className="text-gray-500">No airports found for "{query}"</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
157
flight-comparator/frontend/src/pages/Dashboard.tsx
Normal file
157
flight-comparator/frontend/src/pages/Dashboard.tsx
Normal file
@@ -0,0 +1,157 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { scanApi } from '../api';
|
||||
import type { Scan } from '../api';
|
||||
|
||||
export default function Dashboard() {
|
||||
const [scans, setScans] = useState<Scan[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [stats, setStats] = useState({
|
||||
total: 0,
|
||||
pending: 0,
|
||||
running: 0,
|
||||
completed: 0,
|
||||
failed: 0,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
loadScans();
|
||||
}, []);
|
||||
|
||||
const loadScans = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await scanApi.list(1, 10);
|
||||
const scanList = response.data.data;
|
||||
setScans(scanList);
|
||||
|
||||
// Calculate stats
|
||||
setStats({
|
||||
total: response.data.pagination.total,
|
||||
pending: scanList.filter(s => s.status === 'pending').length,
|
||||
running: scanList.filter(s => s.status === 'running').length,
|
||||
completed: scanList.filter(s => s.status === 'completed').length,
|
||||
failed: scanList.filter(s => s.status === 'failed').length,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to load scans:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getStatusColor = (status: string) => {
|
||||
switch (status) {
|
||||
case 'completed':
|
||||
return 'bg-green-100 text-green-800';
|
||||
case 'running':
|
||||
return 'bg-blue-100 text-blue-800';
|
||||
case 'pending':
|
||||
return 'bg-yellow-100 text-yellow-800';
|
||||
case 'failed':
|
||||
return 'bg-red-100 text-red-800';
|
||||
default:
|
||||
return 'bg-gray-100 text-gray-800';
|
||||
}
|
||||
};
|
||||
|
||||
const formatDate = (dateString: string) => {
|
||||
return new Date(dateString).toLocaleString();
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex justify-center items-center h-64">
|
||||
<div className="text-gray-500">Loading...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<h2 className="text-2xl font-bold text-gray-900">Dashboard</h2>
|
||||
<Link
|
||||
to="/scans"
|
||||
className="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-md text-sm font-medium"
|
||||
>
|
||||
+ New Scan
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{/* Stats Cards */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-5 gap-4 mb-8">
|
||||
<div className="bg-white p-6 rounded-lg shadow">
|
||||
<div className="text-sm font-medium text-gray-500">Total Scans</div>
|
||||
<div className="text-3xl font-bold text-gray-900 mt-2">{stats.total}</div>
|
||||
</div>
|
||||
<div className="bg-white p-6 rounded-lg shadow">
|
||||
<div className="text-sm font-medium text-gray-500">Pending</div>
|
||||
<div className="text-3xl font-bold text-yellow-600 mt-2">{stats.pending}</div>
|
||||
</div>
|
||||
<div className="bg-white p-6 rounded-lg shadow">
|
||||
<div className="text-sm font-medium text-gray-500">Running</div>
|
||||
<div className="text-3xl font-bold text-blue-600 mt-2">{stats.running}</div>
|
||||
</div>
|
||||
<div className="bg-white p-6 rounded-lg shadow">
|
||||
<div className="text-sm font-medium text-gray-500">Completed</div>
|
||||
<div className="text-3xl font-bold text-green-600 mt-2">{stats.completed}</div>
|
||||
</div>
|
||||
<div className="bg-white p-6 rounded-lg shadow">
|
||||
<div className="text-sm font-medium text-gray-500">Failed</div>
|
||||
<div className="text-3xl font-bold text-red-600 mt-2">{stats.failed}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Recent Scans */}
|
||||
<div className="bg-white rounded-lg shadow">
|
||||
<div className="px-6 py-4 border-b border-gray-200">
|
||||
<h3 className="text-lg font-semibold text-gray-900">Recent Scans</h3>
|
||||
</div>
|
||||
<div className="divide-y divide-gray-200">
|
||||
{scans.length === 0 ? (
|
||||
<div className="px-6 py-12 text-center text-gray-500">
|
||||
No scans yet. Create your first scan to get started!
|
||||
</div>
|
||||
) : (
|
||||
scans.map((scan) => (
|
||||
<Link
|
||||
key={scan.id}
|
||||
to={`/scans/${scan.id}`}
|
||||
className="block px-6 py-4 hover:bg-gray-50 cursor-pointer"
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex-1">
|
||||
<div className="flex items-center space-x-3">
|
||||
<span className="font-medium text-gray-900">
|
||||
{scan.origin} → {scan.country}
|
||||
</span>
|
||||
<span
|
||||
className={`px-2 py-1 text-xs font-medium rounded-full ${getStatusColor(
|
||||
scan.status
|
||||
)}`}
|
||||
>
|
||||
{scan.status}
|
||||
</span>
|
||||
</div>
|
||||
<div className="mt-1 text-sm text-gray-500">
|
||||
{scan.start_date} to {scan.end_date} • {scan.adults} adult(s) • {scan.seat_class}
|
||||
</div>
|
||||
{scan.total_routes > 0 && (
|
||||
<div className="mt-1 text-sm text-gray-500">
|
||||
{scan.total_routes} routes • {scan.total_flights} flights found
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-sm text-gray-500">
|
||||
{formatDate(scan.created_at)}
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
194
flight-comparator/frontend/src/pages/Logs.tsx
Normal file
194
flight-comparator/frontend/src/pages/Logs.tsx
Normal file
@@ -0,0 +1,194 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { logApi } from '../api';
|
||||
import type { LogEntry } from '../api';
|
||||
|
||||
export default function Logs() {
|
||||
const [logs, setLogs] = useState<LogEntry[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [page, setPage] = useState(1);
|
||||
const [totalPages, setTotalPages] = useState(1);
|
||||
const [level, setLevel] = useState<string>('');
|
||||
const [search, setSearch] = useState('');
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
loadLogs();
|
||||
}, [page, level, searchQuery]);
|
||||
|
||||
const loadLogs = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await logApi.list(page, 50, level || undefined, searchQuery || undefined);
|
||||
setLogs(response.data.data);
|
||||
setTotalPages(response.data.pagination.pages);
|
||||
} catch (error) {
|
||||
console.error('Failed to load logs:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSearch = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setSearchQuery(search);
|
||||
setPage(1);
|
||||
};
|
||||
|
||||
const getLevelColor = (logLevel: string) => {
|
||||
switch (logLevel) {
|
||||
case 'DEBUG': return 'bg-gray-100 text-gray-700';
|
||||
case 'INFO': return 'bg-blue-100 text-blue-700';
|
||||
case 'WARNING': return 'bg-yellow-100 text-yellow-700';
|
||||
case 'ERROR': return 'bg-red-100 text-red-700';
|
||||
case 'CRITICAL': return 'bg-red-200 text-red-900';
|
||||
default: return 'bg-gray-100 text-gray-700';
|
||||
}
|
||||
};
|
||||
|
||||
const formatTimestamp = (timestamp: string) => {
|
||||
return new Date(timestamp).toLocaleString();
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-900 mb-6">Logs</h2>
|
||||
|
||||
{/* Filters */}
|
||||
<div className="bg-white rounded-lg shadow p-6 mb-6">
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{/* Level Filter */}
|
||||
<div>
|
||||
<label htmlFor="level" className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Log Level
|
||||
</label>
|
||||
<select
|
||||
id="level"
|
||||
value={level}
|
||||
onChange={(e) => {
|
||||
setLevel(e.target.value);
|
||||
setPage(1);
|
||||
}}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
<option value="">All Levels</option>
|
||||
<option value="DEBUG">DEBUG</option>
|
||||
<option value="INFO">INFO</option>
|
||||
<option value="WARNING">WARNING</option>
|
||||
<option value="ERROR">ERROR</option>
|
||||
<option value="CRITICAL">CRITICAL</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{/* Search */}
|
||||
<div>
|
||||
<label htmlFor="search" className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Search Messages
|
||||
</label>
|
||||
<form onSubmit={handleSearch} className="flex space-x-2">
|
||||
<input
|
||||
type="text"
|
||||
id="search"
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
placeholder="Search log messages..."
|
||||
className="flex-1 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
className="px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-md text-sm font-medium"
|
||||
>
|
||||
Search
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Clear Filters */}
|
||||
{(level || searchQuery) && (
|
||||
<div className="mt-4">
|
||||
<button
|
||||
onClick={() => {
|
||||
setLevel('');
|
||||
setSearch('');
|
||||
setSearchQuery('');
|
||||
setPage(1);
|
||||
}}
|
||||
className="text-sm text-blue-600 hover:text-blue-700"
|
||||
>
|
||||
Clear Filters
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Logs List */}
|
||||
{loading ? (
|
||||
<div className="flex justify-center items-center h-64">
|
||||
<div className="text-gray-500">Loading logs...</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="bg-white rounded-lg shadow overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-gray-200">
|
||||
<h3 className="text-lg font-semibold text-gray-900">Log Entries</h3>
|
||||
</div>
|
||||
|
||||
{logs.length === 0 ? (
|
||||
<div className="px-6 py-12 text-center text-gray-500">
|
||||
No logs found
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<div className="divide-y divide-gray-200">
|
||||
{logs.map((log, index) => (
|
||||
<div key={index} className="px-6 py-4">
|
||||
<div className="flex items-start space-x-3">
|
||||
<span className={`px-2 py-1 text-xs font-medium rounded ${getLevelColor(log.level)}`}>
|
||||
{log.level}
|
||||
</span>
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-sm text-gray-900 break-words">
|
||||
{log.message}
|
||||
</p>
|
||||
<div className="mt-1 flex items-center space-x-4 text-xs text-gray-500">
|
||||
<span>{formatTimestamp(log.timestamp)}</span>
|
||||
{log.module && <span>Module: {log.module}</span>}
|
||||
{log.function && <span>Function: {log.function}</span>}
|
||||
{log.line && <span>Line: {log.line}</span>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Pagination */}
|
||||
{totalPages > 1 && (
|
||||
<div className="px-6 py-4 border-t border-gray-200 flex justify-between items-center">
|
||||
<div className="text-sm text-gray-500">
|
||||
Page {page} of {totalPages}
|
||||
</div>
|
||||
<div className="flex space-x-2">
|
||||
<button
|
||||
onClick={() => setPage(page - 1)}
|
||||
disabled={page === 1}
|
||||
className="px-3 py-1 border border-gray-300 rounded text-sm disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50"
|
||||
>
|
||||
Previous
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setPage(page + 1)}
|
||||
disabled={page === totalPages}
|
||||
className="px-3 py-1 border border-gray-300 rounded text-sm disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50"
|
||||
>
|
||||
Next
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
384
flight-comparator/frontend/src/pages/ScanDetails.tsx
Normal file
384
flight-comparator/frontend/src/pages/ScanDetails.tsx
Normal file
@@ -0,0 +1,384 @@
|
||||
import { Fragment, useEffect, useState } from 'react';
|
||||
import { useParams, useNavigate } from 'react-router-dom';
|
||||
import { scanApi } from '../api';
|
||||
import type { Scan, Route, Flight } from '../api';
|
||||
|
||||
export default function ScanDetails() {
|
||||
const { id } = useParams<{ id: string }>();
|
||||
const navigate = useNavigate();
|
||||
const [scan, setScan] = useState<Scan | null>(null);
|
||||
const [routes, setRoutes] = useState<Route[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [page, setPage] = useState(1);
|
||||
const [totalPages, setTotalPages] = useState(1);
|
||||
const [sortField, setSortField] = useState<'min_price' | 'destination' | 'flight_count'>('min_price');
|
||||
const [sortDirection, setSortDirection] = useState<'asc' | 'desc'>('asc');
|
||||
const [expandedRoute, setExpandedRoute] = useState<string | null>(null);
|
||||
const [flightsByDest, setFlightsByDest] = useState<Record<string, Flight[]>>({});
|
||||
const [loadingFlights, setLoadingFlights] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (id) {
|
||||
loadScanDetails();
|
||||
}
|
||||
}, [id, page]);
|
||||
|
||||
// Auto-refresh while scan is running
|
||||
useEffect(() => {
|
||||
if (!scan || (scan.status !== 'pending' && scan.status !== 'running')) {
|
||||
return;
|
||||
}
|
||||
|
||||
const interval = setInterval(() => {
|
||||
loadScanDetails();
|
||||
}, 3000); // Poll every 3 seconds
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [scan?.status, id]);
|
||||
|
||||
useEffect(() => {
|
||||
// Sort routes when sort field or direction changes
|
||||
const sorted = [...routes].sort((a, b) => {
|
||||
let aVal: any = a[sortField];
|
||||
let bVal: any = b[sortField];
|
||||
|
||||
if (sortField === 'min_price') {
|
||||
aVal = aVal ?? Infinity;
|
||||
bVal = bVal ?? Infinity;
|
||||
}
|
||||
|
||||
if (aVal < bVal) return sortDirection === 'asc' ? -1 : 1;
|
||||
if (aVal > bVal) return sortDirection === 'asc' ? 1 : -1;
|
||||
return 0;
|
||||
});
|
||||
setRoutes(sorted);
|
||||
}, [sortField, sortDirection]);
|
||||
|
||||
const loadScanDetails = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const [scanResponse, routesResponse] = await Promise.all([
|
||||
scanApi.get(Number(id)),
|
||||
scanApi.getRoutes(Number(id), page, 20),
|
||||
]);
|
||||
|
||||
setScan(scanResponse.data);
|
||||
setRoutes(routesResponse.data.data);
|
||||
setTotalPages(routesResponse.data.pagination.pages);
|
||||
} catch (error) {
|
||||
console.error('Failed to load scan details:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSort = (field: typeof sortField) => {
|
||||
if (sortField === field) {
|
||||
setSortDirection(sortDirection === 'asc' ? 'desc' : 'asc');
|
||||
} else {
|
||||
setSortField(field);
|
||||
setSortDirection('asc');
|
||||
}
|
||||
};
|
||||
|
||||
const toggleFlights = async (destination: string) => {
|
||||
if (expandedRoute === destination) {
|
||||
setExpandedRoute(null);
|
||||
return;
|
||||
}
|
||||
setExpandedRoute(destination);
|
||||
if (flightsByDest[destination]) return; // already loaded
|
||||
setLoadingFlights(destination);
|
||||
try {
|
||||
const resp = await scanApi.getFlights(Number(id), destination, 1, 200);
|
||||
setFlightsByDest((prev) => ({ ...prev, [destination]: resp.data.data }));
|
||||
} catch {
|
||||
setFlightsByDest((prev) => ({ ...prev, [destination]: [] }));
|
||||
} finally {
|
||||
setLoadingFlights(null);
|
||||
}
|
||||
};
|
||||
|
||||
const getStatusColor = (status: string) => {
|
||||
switch (status) {
|
||||
case 'completed': return 'bg-green-100 text-green-800';
|
||||
case 'running': return 'bg-blue-100 text-blue-800';
|
||||
case 'pending': return 'bg-yellow-100 text-yellow-800';
|
||||
case 'failed': return 'bg-red-100 text-red-800';
|
||||
default: return 'bg-gray-100 text-gray-800';
|
||||
}
|
||||
};
|
||||
|
||||
const formatPrice = (price?: number) => {
|
||||
return price ? `€${price.toFixed(2)}` : 'N/A';
|
||||
};
|
||||
|
||||
if (loading && !scan) {
|
||||
return (
|
||||
<div className="flex justify-center items-center h-64">
|
||||
<div className="text-gray-500">Loading...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!scan) {
|
||||
return (
|
||||
<div className="text-center py-12">
|
||||
<p className="text-gray-500">Scan not found</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
{/* Header */}
|
||||
<div className="mb-6">
|
||||
<button
|
||||
onClick={() => navigate('/')}
|
||||
className="text-blue-500 hover:text-blue-700 mb-4"
|
||||
>
|
||||
← Back to Dashboard
|
||||
</button>
|
||||
<div className="flex justify-between items-start">
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-900">
|
||||
{scan.origin} → {scan.country}
|
||||
</h2>
|
||||
<p className="text-gray-600 mt-1">
|
||||
{scan.start_date} to {scan.end_date} • {scan.adults} adult(s) • {scan.seat_class}
|
||||
</p>
|
||||
</div>
|
||||
<span className={`px-3 py-1 text-sm font-medium rounded-full ${getStatusColor(scan.status)}`}>
|
||||
{scan.status}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Progress Bar (for running scans) */}
|
||||
{(scan.status === 'pending' || scan.status === 'running') && (
|
||||
<div className="bg-white p-4 rounded-lg shadow mb-6">
|
||||
<div className="flex justify-between items-center mb-2">
|
||||
<span className="text-sm font-medium text-gray-700">
|
||||
{scan.status === 'pending' ? 'Initializing...' : 'Scanning in progress...'}
|
||||
</span>
|
||||
<span className="text-sm text-gray-600">
|
||||
{scan.routes_scanned} / {scan.total_routes > 0 ? scan.total_routes : '?'} routes
|
||||
</span>
|
||||
</div>
|
||||
<div className="w-full bg-gray-200 rounded-full h-2.5">
|
||||
<div
|
||||
className="bg-blue-600 h-2.5 rounded-full transition-all duration-300"
|
||||
style={{
|
||||
width: scan.total_routes > 0
|
||||
? `${Math.min((scan.routes_scanned / scan.total_routes) * 100, 100)}%`
|
||||
: '0%'
|
||||
}}
|
||||
></div>
|
||||
</div>
|
||||
<p className="text-xs text-gray-500 mt-2">
|
||||
Auto-refreshing every 3 seconds...
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Stats */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
|
||||
<div className="bg-white p-4 rounded-lg shadow">
|
||||
<div className="text-sm text-gray-500">Total Routes</div>
|
||||
<div className="text-2xl font-bold text-gray-900 mt-1">{scan.total_routes}</div>
|
||||
</div>
|
||||
<div className="bg-white p-4 rounded-lg shadow">
|
||||
<div className="text-sm text-gray-500">Routes Scanned</div>
|
||||
<div className="text-2xl font-bold text-gray-900 mt-1">{scan.routes_scanned}</div>
|
||||
</div>
|
||||
<div className="bg-white p-4 rounded-lg shadow">
|
||||
<div className="text-sm text-gray-500">Total Flights</div>
|
||||
<div className="text-2xl font-bold text-gray-900 mt-1">{scan.total_flights}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Routes Table */}
|
||||
<div className="bg-white rounded-lg shadow overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-gray-200">
|
||||
<h3 className="text-lg font-semibold text-gray-900">Routes Found</h3>
|
||||
</div>
|
||||
|
||||
{routes.length === 0 ? (
|
||||
<div className="px-6 py-12 text-center">
|
||||
{scan.status === 'completed' ? (
|
||||
<div>
|
||||
<p className="text-gray-500 text-lg">No routes found</p>
|
||||
<p className="text-gray-400 text-sm mt-2">No flights available for the selected route and dates.</p>
|
||||
</div>
|
||||
) : scan.status === 'failed' ? (
|
||||
<div>
|
||||
<p className="text-red-500 text-lg">Scan failed</p>
|
||||
{scan.error_message && (
|
||||
<p className="text-gray-500 text-sm mt-2">{scan.error_message}</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div>
|
||||
<div className="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500 mb-4"></div>
|
||||
<p className="text-gray-500 text-lg">Scanning in progress...</p>
|
||||
<p className="text-gray-400 text-sm mt-2">
|
||||
Routes will appear here as they are discovered.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead className="bg-gray-50">
|
||||
<tr>
|
||||
<th
|
||||
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:bg-gray-100"
|
||||
onClick={() => handleSort('destination')}
|
||||
>
|
||||
Destination {sortField === 'destination' && (sortDirection === 'asc' ? '↑' : '↓')}
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
City
|
||||
</th>
|
||||
<th
|
||||
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:bg-gray-100"
|
||||
onClick={() => handleSort('flight_count')}
|
||||
>
|
||||
Flights {sortField === 'flight_count' && (sortDirection === 'asc' ? '↑' : '↓')}
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Airlines
|
||||
</th>
|
||||
<th
|
||||
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:bg-gray-100"
|
||||
onClick={() => handleSort('min_price')}
|
||||
>
|
||||
Min Price {sortField === 'min_price' && (sortDirection === 'asc' ? '↑' : '↓')}
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Avg Price
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Max Price
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="bg-white divide-y divide-gray-200">
|
||||
{routes.map((route) => (
|
||||
<Fragment key={route.id}>
|
||||
<tr
|
||||
key={route.id}
|
||||
className="hover:bg-gray-50 cursor-pointer"
|
||||
onClick={() => toggleFlights(route.destination)}
|
||||
>
|
||||
<td className="px-6 py-4 whitespace-nowrap">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-gray-400 text-xs">
|
||||
{expandedRoute === route.destination ? '▼' : '▶'}
|
||||
</span>
|
||||
<div>
|
||||
<div className="font-medium text-gray-900">{route.destination}</div>
|
||||
<div className="text-sm text-gray-500">{route.destination_name}</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
||||
{route.destination_city || 'N/A'}
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
|
||||
{route.flight_count}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-500">
|
||||
<div className="max-w-xs truncate">
|
||||
{route.airlines.join(', ')}
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-green-600">
|
||||
{formatPrice(route.min_price)}
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
||||
{formatPrice(route.avg_price)}
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
||||
{formatPrice(route.max_price)}
|
||||
</td>
|
||||
</tr>
|
||||
{expandedRoute === route.destination && (
|
||||
<tr key={`${route.id}-flights`}>
|
||||
<td colSpan={7} className="px-0 py-0 bg-gray-50">
|
||||
{loadingFlights === route.destination ? (
|
||||
<div className="px-8 py-4 text-sm text-gray-500">Loading flights...</div>
|
||||
) : (
|
||||
<table className="w-full text-sm">
|
||||
<thead>
|
||||
<tr className="bg-gray-100 text-xs text-gray-500 uppercase">
|
||||
<th className="px-8 py-2 text-left">Date</th>
|
||||
<th className="px-4 py-2 text-left">Airline</th>
|
||||
<th className="px-4 py-2 text-left">Departure</th>
|
||||
<th className="px-4 py-2 text-left">Arrival</th>
|
||||
<th className="px-4 py-2 text-left font-semibold text-green-700">Price</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{(flightsByDest[route.destination] || []).map((f) => (
|
||||
<tr key={f.id} className="border-t border-gray-200 hover:bg-white">
|
||||
<td className="px-8 py-2 text-gray-700">{f.date}</td>
|
||||
<td className="px-4 py-2 text-gray-600">{f.airline || '—'}</td>
|
||||
<td className="px-4 py-2 text-gray-600">{f.departure_time || '—'}</td>
|
||||
<td className="px-4 py-2 text-gray-600">{f.arrival_time || '—'}</td>
|
||||
<td className="px-4 py-2 font-medium text-green-600">
|
||||
{f.price != null ? `€${f.price.toFixed(2)}` : '—'}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{(flightsByDest[route.destination] || []).length === 0 && (
|
||||
<tr>
|
||||
<td colSpan={5} className="px-8 py-3 text-gray-400 text-center">
|
||||
No flight details available
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</Fragment>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{/* Pagination */}
|
||||
{totalPages > 1 && (
|
||||
<div className="px-6 py-4 border-t border-gray-200 flex justify-between items-center">
|
||||
<div className="text-sm text-gray-500">
|
||||
Page {page} of {totalPages}
|
||||
</div>
|
||||
<div className="flex space-x-2">
|
||||
<button
|
||||
onClick={() => setPage(page - 1)}
|
||||
disabled={page === 1}
|
||||
className="px-3 py-1 border border-gray-300 rounded text-sm disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50"
|
||||
>
|
||||
Previous
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setPage(page + 1)}
|
||||
disabled={page === totalPages}
|
||||
className="px-3 py-1 border border-gray-300 rounded text-sm disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50"
|
||||
>
|
||||
Next
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
299
flight-comparator/frontend/src/pages/Scans.tsx
Normal file
299
flight-comparator/frontend/src/pages/Scans.tsx
Normal file
@@ -0,0 +1,299 @@
|
||||
import { useState } from 'react';
|
||||
import { scanApi } from '../api';
|
||||
import type { CreateScanRequest } from '../api';
|
||||
import AirportSearch from '../components/AirportSearch';
|
||||
|
||||
export default function Scans() {
|
||||
const [destinationMode, setDestinationMode] = useState<'country' | 'airports'>('country');
|
||||
const [formData, setFormData] = useState<CreateScanRequest>({
|
||||
origin: '',
|
||||
country: '',
|
||||
window_months: 3,
|
||||
seat_class: 'economy',
|
||||
adults: 1,
|
||||
});
|
||||
const [selectedAirports, setSelectedAirports] = useState<string[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [success, setSuccess] = useState<string | null>(null);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
setSuccess(null);
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
// Validate airports mode has at least one airport selected
|
||||
if (destinationMode === 'airports' && selectedAirports.length === 0) {
|
||||
setError('Please add at least one destination airport');
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Build request based on destination mode
|
||||
const requestData: any = {
|
||||
origin: formData.origin,
|
||||
window_months: formData.window_months,
|
||||
seat_class: formData.seat_class,
|
||||
adults: formData.adults,
|
||||
};
|
||||
|
||||
if (destinationMode === 'country') {
|
||||
requestData.country = formData.country;
|
||||
} else {
|
||||
requestData.destinations = selectedAirports;
|
||||
}
|
||||
|
||||
const response = await scanApi.create(requestData);
|
||||
setSuccess(`Scan created successfully! ID: ${response.data.id}`);
|
||||
|
||||
// Reset form
|
||||
setFormData({
|
||||
origin: '',
|
||||
country: '',
|
||||
window_months: 3,
|
||||
seat_class: 'economy',
|
||||
adults: 1,
|
||||
});
|
||||
setSelectedAirports([]);
|
||||
|
||||
// Redirect to dashboard after 2 seconds
|
||||
setTimeout(() => {
|
||||
window.location.href = '/';
|
||||
}, 2000);
|
||||
} catch (err: any) {
|
||||
const errorMessage = err.response?.data?.message || 'Failed to create scan';
|
||||
setError(errorMessage);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleChange = (
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>
|
||||
) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
[name]: name === 'adults' || name === 'window_months' ? parseInt(value) : value,
|
||||
}));
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-900 mb-6">Create New Scan</h2>
|
||||
|
||||
<div className="bg-white rounded-lg shadow p-6 max-w-2xl">
|
||||
<form onSubmit={handleSubmit} className="space-y-6">
|
||||
{/* Origin Airport */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Origin Airport (IATA Code)
|
||||
</label>
|
||||
<AirportSearch
|
||||
value={formData.origin}
|
||||
onChange={(value) => setFormData((prev) => ({ ...prev, origin: value }))}
|
||||
placeholder="e.g., BDS, MUC, FRA"
|
||||
/>
|
||||
<p className="mt-1 text-sm text-gray-500">
|
||||
Enter 3-letter IATA code (e.g., BDS for Brindisi)
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Destination Mode Toggle */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-3">
|
||||
Destination Mode
|
||||
</label>
|
||||
<div className="flex space-x-2 mb-4">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDestinationMode('country')}
|
||||
className={`flex-1 px-4 py-2 text-sm font-medium rounded-md ${
|
||||
destinationMode === 'country'
|
||||
? 'bg-blue-500 text-white'
|
||||
: 'bg-gray-100 text-gray-700 hover:bg-gray-200'
|
||||
}`}
|
||||
>
|
||||
Search by Country
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDestinationMode('airports')}
|
||||
className={`flex-1 px-4 py-2 text-sm font-medium rounded-md ${
|
||||
destinationMode === 'airports'
|
||||
? 'bg-blue-500 text-white'
|
||||
: 'bg-gray-100 text-gray-700 hover:bg-gray-200'
|
||||
}`}
|
||||
>
|
||||
Search by Airports
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Country Mode */}
|
||||
{destinationMode === 'country' ? (
|
||||
<div>
|
||||
<label htmlFor="country" className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Destination Country (2-letter code)
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
id="country"
|
||||
name="country"
|
||||
value={formData.country}
|
||||
onChange={handleChange}
|
||||
maxLength={2}
|
||||
required
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
placeholder="e.g., DE, IT, ES"
|
||||
/>
|
||||
<p className="mt-1 text-sm text-gray-500">
|
||||
ISO 2-letter country code (e.g., DE for Germany)
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
/* Airports Mode */
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Destination Airports
|
||||
</label>
|
||||
<div className="space-y-2">
|
||||
<AirportSearch
|
||||
value=""
|
||||
onChange={(code) => {
|
||||
if (code && code.length === 3 && !selectedAirports.includes(code)) {
|
||||
setSelectedAirports([...selectedAirports, code]);
|
||||
}
|
||||
}}
|
||||
clearAfterSelect
|
||||
required={false}
|
||||
placeholder="Search and add airports..."
|
||||
/>
|
||||
{/* Selected airports list */}
|
||||
{selectedAirports.length > 0 && (
|
||||
<div className="flex flex-wrap gap-2 mt-2">
|
||||
{selectedAirports.map((code) => (
|
||||
<div
|
||||
key={code}
|
||||
className="inline-flex items-center px-3 py-1 bg-blue-100 text-blue-800 rounded-full text-sm"
|
||||
>
|
||||
<span className="font-medium">{code}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSelectedAirports(selectedAirports.filter((c) => c !== code))}
|
||||
className="ml-2 text-blue-600 hover:text-blue-800"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<p className="text-sm text-gray-500">
|
||||
{selectedAirports.length === 0
|
||||
? 'Search and add destination airports (up to 50)'
|
||||
: `${selectedAirports.length} airport(s) selected`}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Search Window */}
|
||||
<div>
|
||||
<label htmlFor="window_months" className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Search Window (months)
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
id="window_months"
|
||||
name="window_months"
|
||||
value={formData.window_months}
|
||||
onChange={handleChange}
|
||||
min={1}
|
||||
max={12}
|
||||
required
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
<p className="mt-1 text-sm text-gray-500">
|
||||
Number of months to search (1-12)
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Seat Class */}
|
||||
<div>
|
||||
<label htmlFor="seat_class" className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Seat Class
|
||||
</label>
|
||||
<select
|
||||
id="seat_class"
|
||||
name="seat_class"
|
||||
value={formData.seat_class}
|
||||
onChange={handleChange}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
<option value="economy">Economy</option>
|
||||
<option value="premium">Premium Economy</option>
|
||||
<option value="business">Business</option>
|
||||
<option value="first">First Class</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{/* Number of Adults */}
|
||||
<div>
|
||||
<label htmlFor="adults" className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Number of Adults
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
id="adults"
|
||||
name="adults"
|
||||
value={formData.adults}
|
||||
onChange={handleChange}
|
||||
min={1}
|
||||
max={9}
|
||||
required
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
<p className="mt-1 text-sm text-gray-500">
|
||||
Number of adult passengers (1-9)
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Error Message */}
|
||||
{error && (
|
||||
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Success Message */}
|
||||
{success && (
|
||||
<div className="bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded">
|
||||
{success}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Submit Button */}
|
||||
<div className="flex justify-end space-x-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => window.location.href = '/'}
|
||||
className="px-4 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 hover:bg-gray-50"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-md text-sm font-medium disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
{loading ? 'Creating...' : 'Create Scan'}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
11
flight-comparator/frontend/tailwind.config.js
Normal file
11
flight-comparator/frontend/tailwind.config.js
Normal file
@@ -0,0 +1,11 @@
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
export default {
|
||||
content: [
|
||||
"./index.html",
|
||||
"./src/**/*.{js,ts,jsx,tsx}",
|
||||
],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
}
|
||||
20
flight-comparator/frontend/vite.config.ts
Normal file
20
flight-comparator/frontend/vite.config.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import react from '@vitejs/plugin-react'
|
||||
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: {
|
||||
port: 5173,
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: 'http://localhost:8000',
|
||||
changeOrigin: true,
|
||||
},
|
||||
'/health': {
|
||||
target: 'http://localhost:8000',
|
||||
changeOrigin: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user