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(''); | |
... |
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
... | |
return ( | |
<div className="container"> | |
<h1>Weather App</h1> | |
<LocationSearch onSearch={addLocation}/> | |
{ | |
error | |
? <div className={`alert alert-danger`}>{error}</div> | |
: null |
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 resetAlerts = () => { | |
setError(''); | |
setWarning(''); | |
} | |
const addLocation = async (term: string) => { | |
resetAlerts(); | |
const location = await searchLocation(term); | |
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 LocationTableProps { | |
locations: WeatherLocation[]; | |
} | |
export const LocationTable: FC<LocationTableProps> = ({locations}) => | |
<div> | |
... | |
<tbody> | |
{locations.map(location => | |
<tr key={location.id}><td>{location.name}</td></tr> |
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 [currentLocation, setCurrentLocation] = useState<WeatherLocation | null>(null); | |
... | |
return ( | |
... | |
<LocationTable locations={locations} | |
current={currentLocation} | |
onSelect={location => setCurrentLocation(location)}/> |
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 React, {FC} from "react"; | |
import {WeatherLocation} from "../model/Weather"; | |
interface LocationTableProps { | |
locations: WeatherLocation[]; | |
current: WeatherLocation | null; | |
onSelect: (location: WeatherLocation) => void; | |
} | |
export const LocationTable: FC<LocationTableProps> = ({locations, onSelect, current}) => |
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 WeatherConditions { | |
id: number; | |
main: string; | |
description: string; | |
icon: string; | |
} | |
export interface MainWeatherData { | |
temp: number; | |
feels_like: number; |
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 readWeather(locationId: number): Promise<Weather> { | |
const current = await fetch(`${server}/weather?id=${locationId}&${keyQuery}&units=metric`); | |
if (current.status !== 200) throw new Error('Failed to read location data'); | |
return await current.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
export function getIconUrl(code: string): string { | |
return `http://openweathermap.org/img/wn/${code}.png`; | |
} |
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 React, {FC} from "react"; | |
import {Weather} from "../model/Weather"; | |
import {getIconUrl} from "../services/WeatherService"; | |
interface WeatherEntryProps { | |
weather: Weather; | |
} | |
function convertUnixTimeToDate(unixUtc: number): Date { | |
return new Date(unixUtc * 1000); |