Advertisement
fabiobiondi

Twitch-Demo-2021-02-01-rxjs-reactive-forms

Jan 31st, 2021
2,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // utils/config.ts
  2. export const WEATHER_API_KEY = 'xxxx';  // get from http://openweathermap.org/
  3. export const WEATHER_API_URL = 'https://api.openweathermap.org/data/2.5/weather?q=';
  4.  
  5. export const GMAP_API_URL = 'https://maps.googleapis.com/maps/api/staticmap?center=';
  6. export const GMAP_API_KEY = 'xxxx';     // get from https://developers.google.com/maps/documentation/javascript/overview
  7.  
  8. // app.component.ts
  9. import { Component, OnInit } from "@angular/core";
  10. import { FormControl } from "@angular/forms";
  11. import { HttpClient } from "@angular/common/http";
  12. import {
  13.   catchError,
  14.   debounceTime,
  15.   distinctUntilChanged,
  16.   filter,
  17.   map,
  18.   startWith,
  19.   switchMap
  20. } from "rxjs/operators";
  21. import { of } from "rxjs";
  22. import { Meteo, ShortMeteo } from "./model/meteo";
  23. import {
  24.   WEATHER_API_URL,
  25.   WEATHER_API_KEY,
  26.   GMAP_API_URL,
  27.   GMAP_API_KEY
  28. } from "./utils/config";
  29.  
  30. @Component({
  31.   selector: "my-app",
  32.   template: `
  33.     <div class="card">
  34.       <!--Search city input -->
  35.       <input type="text" placeholder="Search City" />
  36.  
  37.       <!--Temp + icon-->
  38.       <div class="title-bar">
  39.         30° <img src="http://openweathermap.org/img/w/01n.png" alt="icon" />
  40.       </div>
  41.  
  42.       <!--Map-->
  43.       <img
  44.         src="https://maps.googleapis.com/maps/api/staticmap?center=Italy&zoom=5&size=200x100&key=AIzaSyDSBmiSWV4-AfzaTPNSJhu-awtfBNi9o2k"
  45.         width="100%"
  46.         alt="map"
  47.       />
  48.     </div>
  49.   `,
  50.   styles: [``]
  51. })
  52. export class AppComponent implements OnInit {
  53.   cityInput: FormControl = new FormControl();
  54.   meteo: ShortMeteo;
  55.  
  56.   constructor(private http: HttpClient) {}
  57.  
  58.   ngOnInit(): void {
  59.     this.cityInput.valueChanges
  60.       .pipe(
  61.         debounceTime(1000),
  62.         filter(city => city.length >= 3),
  63.         distinctUntilChanged(),
  64.         switchMap(city =>
  65.           this.http
  66.             .get<Meteo>(
  67.               WEATHER_API_URL + city + "&units=metric&APPID=" + WEATHER_API_KEY
  68.             )
  69.             .pipe(
  70.               map((result: Meteo) => ({
  71.                 temp: result.main.temp,
  72.                 icon: `http://openweathermap.org/img/w/${
  73.                   result.weather[0].icon
  74.                 }.png`,
  75.                 gmap: `${GMAP_API_URL}Italy&zoom=5&size=200x100&key=${GMAP_API_KEY}`,
  76.                 error: false
  77.               })),
  78.               catchError(() => of({ error: true }))
  79.             )
  80.         )
  81.       )
  82.       .subscribe(obj => (this.meteo = obj));
  83.  
  84.     this.cityInput.setValue("Milan");
  85.   }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement