11import { useState , useEffect } from 'react'
22import { Button } from '@/components/ui/button'
33import LoadingSkeleton from './components/LoadingSkeleton'
4+ import AlertMsg from './components/AlertMsg'
45import Mapper from './components/Mapper'
56import TruckCard from './components/TruckCard'
67
@@ -29,6 +30,7 @@ function App() {
2930 } )
3031 const [ foodTrucks , setFoodTrucks ] = useState < FoodTruck [ ] > ( [ ] )
3132 const [ isLoading , setIsLoading ] = useState < boolean > ( false )
33+ const [ error , setError ] = useState < string > ( '' )
3234 const [ selectedTruckId , setSelectedTruckId ] = useState < string > ( '' )
3335
3436 const getLocation = ( ) => {
@@ -54,9 +56,8 @@ function App() {
5456 } , [ ] )
5557
5658 const handleClick = async ( ) => {
57- // handle location not there
5859 if ( ! location . latitude || ! location . longitude ) {
59- // show error on screen!
60+ setError ( `Unable to get your location.` )
6061 return
6162 }
6263 setIsLoading ( true )
@@ -72,23 +73,25 @@ function App() {
7273 lon : location . longitude ?. toString ( ) || ''
7374 } )
7475 const paramsString = newParams . toString ( )
75- // adjust proxy to get rid of localhost
76+ // TODO: Adjust proxy to get rid of localhost
7677 const response = await fetch ( `http://localhost:8080/api/trucks?${ paramsString } ` )
7778 const json = await response . json ( )
7879 const data : FoodTruck [ ] = json . data
7980 setFoodTrucks ( data )
81+ if ( data . length === 0 ) {
82+ setError ( `Nothing nearby or open right now! Get moving!` )
83+ }
8084 setIsLoading ( false )
8185 } catch ( error ) {
82- // show error on screen!
83- console . log ( 'error' , error )
86+ setError ( `Unable to get any trucks right now.` )
87+ // TODO: Show error to user on screen
8488 setIsLoading ( false )
8589 }
8690 }
8791
8892 const handleTruckClick = ( id : string ) => {
8993 const truck = foodTrucks . find ( ( truck ) => truck . id === id )
9094 if ( truck ) {
91- // console.log("truck is ", truck.id)
9295 setSelectedTruckId ( truck . id )
9396 }
9497 }
@@ -114,6 +117,7 @@ function App() {
114117 />
115118 < div style = { { zIndex : 10000 } } >
116119 < div className = "flex flex-col items-center justify-center space-y-4" >
120+ { error && < AlertMsg error = { error } /> }
117121 < Button onClick = { handleClick } > Find Food Trucks Open Now</ Button >
118122 { isLoading ? (
119123 < div className = "flex flex-col align-left space-y-4" >
0 commit comments