1717 * - [ ] Track path trail (polyline) on map over session
1818 * - [ ] Extract map component & custom hook (useIssPosition)
1919 */
20- import { useEffect , useState } from 'react' ;
20+ import { useEffect , useState , useRef } from 'react' ;
2121import Loading from '../components/Loading.jsx' ;
2222import ErrorMessage from '../components/ErrorMessage.jsx' ;
2323import Card from '../components/Card.jsx' ;
24+ import IssMap from '../components/IssMap.jsx' ;
2425
2526export default function Space ( ) {
2627 const [ iss , setIss ] = useState ( null ) ;
2728 const [ crew , setCrew ] = useState ( [ ] ) ;
2829 const [ error , setError ] = useState ( null ) ;
2930 const [ loading , setLoading ] = useState ( false ) ;
31+ const [ lastUpdated , setLastUpdated ] = useState ( null ) ;
32+ const intervalRef = useRef ( null ) ;
3033
31- useEffect ( ( ) => { fetchData ( ) ; } , [ ] ) ;
34+ useEffect ( ( ) => {
35+ fetchData ( ) ;
36+ // Poll every 5s for updated ISS position only
37+ intervalRef . current = setInterval ( ( ) => {
38+ refreshIssOnly ( ) ;
39+ } , 5000 ) ;
40+ return ( ) => { if ( intervalRef . current ) clearInterval ( intervalRef . current ) ; } ;
41+ } , [ ] ) ;
3242
3343 async function fetchData ( ) {
3444 try {
@@ -42,9 +52,23 @@ export default function Space() {
4252 const crewJson = await crewRes . json ( ) ;
4353 setIss ( issJson ) ;
4454 setCrew ( crewJson . people || [ ] ) ;
55+ setLastUpdated ( new Date ( ) ) ;
4556 } catch ( e ) { setError ( e ) ; } finally { setLoading ( false ) ; }
4657 }
4758
59+ async function refreshIssOnly ( ) {
60+ try {
61+ const res = await fetch ( 'http://api.open-notify.org/iss-now.json' ) ;
62+ if ( ! res . ok ) throw new Error ( 'Failed to refresh ISS' ) ;
63+ const issJson = await res . json ( ) ;
64+ setIss ( issJson ) ;
65+ setLastUpdated ( new Date ( ) ) ;
66+ } catch ( e ) {
67+ // don't clobber existing data, but surface error
68+ setError ( e ) ;
69+ }
70+ }
71+ //leaflet map component
4872 return (
4973 < div >
5074 < h2 > Space & Astronomy </ h2 >
@@ -54,7 +78,8 @@ export default function Space() {
5478 < Card title = "ISS Current Location" >
5579 < p > Latitude: { iss . iss_position . latitude } </ p >
5680 < p > Longitude: { iss . iss_position . longitude } </ p >
57- { /* TODO: Render map (Leaflet) with marker for ISS position */ }
81+ { lastUpdated && < p style = { { fontSize : '0.8rem' , color : '#666' } } > Last updated: { lastUpdated . toLocaleTimeString ( ) } </ p > }
82+ < IssMap latitude = { iss . iss_position . latitude } longitude = { iss . iss_position . longitude } />
5883 </ Card >
5984 ) }
6085 < Card title = { `Astronauts in Space (${ crew . length } )` } >
0 commit comments