diff --git a/frontend/package-lock.json b/frontend/package-lock.json index e31d6c3..cc5e270 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -2686,6 +2686,7 @@ "version": "16.4.5", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "license": "BSD-2-Clause", "engines": { "node": ">=12" }, diff --git a/frontend/src/Pages/schedule.jsx b/frontend/src/Pages/schedule.jsx index d45ea4c..67ec79d 100644 --- a/frontend/src/Pages/schedule.jsx +++ b/frontend/src/Pages/schedule.jsx @@ -1,40 +1,41 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState } from 'react'; import { IoArrowBack, IoSearchOutline } from 'react-icons/io5'; import { useNavigate } from 'react-router-dom'; - -// Placeholder API URL (replace with the actual API endpoint) -const API_URL = 'http://localhost:3000/train'; +import axios from 'axios'; const SchedulePage = () => { - useEffect(() => { - document.title = 'Station Saarthi | Schedule'; - }, []); - const [searchQuery, setSearchQuery] = useState(''); const [trainDetails, setTrainDetails] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const navigate = useNavigate(); - // Function to search train by either number or name const searchTrain = async () => { if (!searchQuery) { - setError('Please enter a train number or name to search'); + setError('Please enter a train number to search'); return; } setLoading(true); setError(null); + const apiKey = ""; // Enter your Indian Rail API key + const options = { + method: 'GET', + url: `https://indianrailapi.com/api/v2/TrainSchedule/apikey/${apiKey}/TrainNumber/${searchQuery}`, + }; + try { - const response = await fetch(`${API_URL}/${searchQuery}`); - if (!response.ok) { - throw new Error('Train not found'); - } + const response = await axios.request(options); + console.log("API Response Data:", response.data); - const data = await response.json(); - setTrainDetails(data); // Assuming the API returns a train object + if (response.data.ResponseCode === "200") { + setTrainDetails(response.data.Route); + } else { + setError('No train details found'); + } } catch (error) { + console.error(error); setError(error.message || 'An error occurred while fetching the train details'); } finally { setLoading(false); @@ -43,130 +44,51 @@ const SchedulePage = () => { return (
-
- -
-
-
-
-

- Train Schedule -

- -
- {/* Search input */} -
- -
- setSearchQuery(e.target.value)} - className="w-full px-4 py-2 pr-10 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-300" - /> - -
- {error &&

{error}

} +
+

Train Schedule

+ +
+ +
+ setSearchQuery(e.target.value)} + className="w-full px-4 py-2 pr-10 border border-gray-300 rounded-lg" + /> +
+ {error &&

{error}

} +
- {/* Display train details */} - {loading &&

Loading...

} - {trainDetails && ( -
-
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - `${coach.coachNumber} (${coach.coachType})`) - .join(', ') || 'N/A' - } - readOnly - className="w-full px-4 py-2 border border-gray-300 rounded-lg" - /> + {loading &&

Loading...

} + {trainDetails && ( +
+ {trainDetails.map((stop) => ( +
+

{stop.StationName} ({stop.StationCode})

+

Arrival Time: {stop.ArrivalTime}

+

Departure Time: {stop.DepartureTime}

+

Distance: {stop.Distance} km

-
- )} + ))} +
+ )} - {/* Button to search */} - -
+