This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface LocationSearchProps { | |
onSearch: (search: string) => void; | |
} | |
export const LocationSearch: FC<LocationSearchProps> = (props) => { | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const LocationSearch: FC<LocationSearchProps> = (props) => {} | |
// Simplify using destructuring | |
export const LocationSearch: FC<LocationSearchProps> = ({onSearch}) => {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const LocationSearch: FC<LocationSearchProps> = ({onSearch}) => { | |
const [locationSearch, setLocationSearch] = useState(''); | |
const disableSearch = locationSearch.trim() === ''; | |
const addLocation = () => { | |
onSearch(locationSearch); // Prop callback invoked - string passed | |
setLocationSearch(''); | |
}; | |
return ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import {LocationSearch} from "./LocationSearch"; | |
function App() { | |
const [locations, setLocations] = useState<string[]>([]); | |
const addLocation = (location: string) => setLocations([location, ...locations]); | |
return ( | |
<div className="container"> | |
<h1>Weather App</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import {LocationSearch} from "./LocationSearch"; | |
import {LocationTable} from "./LocationTable"; | |
function App() { | |
... | |
return ( | |
<div className="container"> | |
<h1>Weather App</h1> | |
<LocationSearch onSearch={addLocation}/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const key: string = process.env.REACT_APP_OPEN_WEATHER_API_KEY as string; | |
if (key === undefined) { | |
throw new Error('No Open Weather API Key defined - ensure you set a variable called REACT_APP_OPEN_WEATHER_API_KEY') | |
} | |
const keyQuery = `appid=${key}` | |
const server = 'http://api.openweathermap.org/data/2.5'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`${server}/weather?q=${term}&${keyQuery}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface Coordinates { | |
lon: number; | |
lat: number; | |
} | |
export interface WeatherLocation { | |
coord: Coordinates; | |
id: number; | |
name: string; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function searchLocation(term: string): Promise<WeatherLocation | undefined> { | |
const result = await fetch(`${server}/weather?q=${term}&${keyQuery}`); | |
if (result.status === 404) return undefined; | |
if (result.status !== 200) throw new Error('Failed to read location data'); | |
return await result.json(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
const App: FC = () => { | |
const [locations, setLocations] = useState<WeatherLocation[]>([]); | |
const [error, setError] = useState(''); | |
const [warning, setWarning] = useState(''); | |
... |