fabiobiondi

Maggioli: esempio weather (reactive forms + lifecycle)

Nov 2nd, 2020
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // App.tsx
  2.  
  3. import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
  4. import { FormControl, NgForm } from '@angular/forms';
  5. import { debounceTime, distinctUntilChanged, filter, map } from 'rxjs/operators';
  6.  
  7. @Component({
  8.   selector: 'app-root',
  9.   template: `
  10.     <input type="text" [formControl]="textInput" #textRef>
  11.     <hr>
  12.     <app-weather
  13.       [unit]="unit"
  14.       [color]="fontColor"
  15.       [city]="searchText"
  16.     ></app-weather>
  17.  
  18.     <hr>
  19.     <button (click)="unit = 'metric'">metric</button>
  20.     <button (click)="unit = 'imperial'">imperial</button>
  21.     <button (click)="fontColor = 'blue'">blue</button>
  22.     <button (click)="fontColor = 'red'">red</button>
  23.   `,
  24. })
  25. export class AppComponent implements OnInit, AfterViewInit {
  26.   @ViewChild('textRef') textRef: ElementRef<HTMLInputElement>;
  27.  
  28.   textInput: FormControl = new FormControl();
  29.   searchText = 'Milan';
  30.   fontColor = 'red';
  31.   unit = 'metric';
  32.  
  33.   constructor() {
  34.     this.textInput.valueChanges
  35.       .pipe(
  36.         filter((text: string) => text.length > 3),
  37.         debounceTime(1000),
  38.         distinctUntilChanged()
  39.       )
  40.       .subscribe(text => {
  41.         this.searchText = text;
  42.       });
  43.     // popolate text
  44.     this.textInput.setValue(this.searchText);
  45.     // focus text
  46.     console.log('App: constructor', this.textRef)
  47.   }
  48.  
  49.   ngOnInit(): void {
  50.     console.log('App: init', this.textRef)
  51.   }
  52.  
  53.   ngAfterViewInit(): void {
  54.     console.log('App: after view', this.textRef)
  55.     this.textRef.nativeElement.focus();
  56.   }
  57.  
  58.   search(): void {
  59.     console.log(this.textInput.value)
  60.   }
  61. }
  62.  
  63.  
  64.  
  65. // ============================
  66. // WeatherComponents
  67. import { AfterViewInit, Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
  68. import { HttpClient } from '@angular/common/http';
  69. import { Meteo } from '../model/meteo';
  70.  
  71. @Component({
  72.   selector: 'app-weather',
  73.   template: `
  74.     <h1 [style.color]="color">{{meteo?.main.temp}} °</h1>
  75.     <img [src]="'http://openweathermap.org/img/w/' + meteo?.weather[0].icon + '.png'" alt="">
  76.   `,
  77. })
  78. export class WeatherComponent implements AfterViewInit, OnChanges {
  79.   @Input() city: string;
  80.   @Input() unit: string;
  81.   @Input() color: string;
  82.  
  83.   meteo: Meteo;
  84.  
  85.   constructor(private http: HttpClient) {}
  86.  
  87.   init(): void {
  88.     console.log('WeatherComponent: init')
  89.   }
  90.  
  91.   ngOnChanges(changes: SimpleChanges): void {
  92.  
  93.     if (changes.color && changes.color.firstChange) {
  94.       this.init();
  95.     }
  96.     if (changes.color) {
  97.       console.log('WeatherComponent: changes')
  98.     }
  99.  
  100.     if (changes.city || changes.unit) {
  101.       this.http.get<Meteo>(`http://api.openweathermap.org/data/2.5/weather?q=${changes.city.currentValue}&units=${this.unit}&APPID=eb03b1f5e5afb5f4a4edb40c1ef2f534`)
  102.         .subscribe(res => this.meteo = res)
  103.     }
  104.   }
  105.  
  106.   ngAfterViewInit(): void {
  107.     console.log('WeatherComponent: after view')
  108.   }
  109.  
  110. }
  111.  
Add Comment
Please, Sign In to add comment