Skip to content

Instantly share code, notes, and snippets.

@eamonnboyle
eamonnboyle / App.tsx
Created February 23, 2021 10:26
Weather App Tutorial - State Updates
...
const App: FC = () => {
const [locations, setLocations] = useState<WeatherLocation[]>([]);
const [error, setError] = useState('');
const [warning, setWarning] = useState('');
...
@eamonnboyle
eamonnboyle / App.tsx
Created February 23, 2021 10:27
Weather App Tutorial - View Updates
...
return (
<div className="container">
<h1>Weather App</h1>
<LocationSearch onSearch={addLocation}/>
{
error
? <div className={`alert alert-danger`}>{error}</div>
: null
@eamonnboyle
eamonnboyle / App.tsx
Last active February 25, 2021 22:10
Weather App Tutorial - Call search service
...
const resetAlerts = () => {
setError('');
setWarning('');
}
const addLocation = async (term: string) => {
resetAlerts();
const location = await searchLocation(term);
@eamonnboyle
eamonnboyle / LocationTable.tsx
Last active February 23, 2021 16:46
Weather App Tutorial - Typed Location Table
interface LocationTableProps {
locations: WeatherLocation[];
}
export const LocationTable: FC<LocationTableProps> = ({locations}) =>
<div>
...
<tbody>
{locations.map(location =>
<tr key={location.id}><td>{location.name}</td></tr>
@eamonnboyle
eamonnboyle / App.tsx
Created February 23, 2021 10:35
Weather App Tutorial - Add Current Location
const App: FC = () => {
const [currentLocation, setCurrentLocation] = useState<WeatherLocation | null>(null);
...
return (
...
<LocationTable locations={locations}
current={currentLocation}
onSelect={location => setCurrentLocation(location)}/>
@eamonnboyle
eamonnboyle / LocationTable.tsx
Created February 23, 2021 10:37
Weather App Tutorial - Highlight Current Location
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}) =>
@eamonnboyle
eamonnboyle / Weather.ts
Created February 23, 2021 10:38
Weather App Tutorial - Extending Model
export interface WeatherConditions {
id: number;
main: string;
description: string;
icon: string;
}
export interface MainWeatherData {
temp: number;
feels_like: number;
@eamonnboyle
eamonnboyle / WeatherService.ts
Created February 23, 2021 10:39
Weather App Tutorial - Reading Weather
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();
}
@eamonnboyle
eamonnboyle / WeatherService.ts
Created February 23, 2021 10:40
Weather App Tutorial - Icon Links
export function getIconUrl(code: string): string {
return `http://openweathermap.org/img/wn/${code}.png`;
}
@eamonnboyle
eamonnboyle / WeatherEntry.tsx
Created February 23, 2021 10:41
Weather App Tutorial - Weather Entry Component
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);