Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Created March 5, 2019 00:43
Show Gist options
  • Save alexytiger/f09b65df453ae5cc75b51c96ba0defa3 to your computer and use it in GitHub Desktop.
Save alexytiger/f09b65df453ae5cc75b51c96ba0defa3 to your computer and use it in GitHub Desktop.
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Store, select } from '@ngrx/store';
import * as fromAttackChange from '../../index';
import { Observable, Subject } from 'rxjs';
import { takeUntil, tap } from 'rxjs/operators';
@Component({
selector: 'app-attack-change',
templateUrl: './attack-change.component.html',
styleUrls: ['./attack-change.component.css']
})
export class AttackChangeComponent implements OnInit, OnDestroy {
public attack$: Observable<string>;
constructor(
private formBuilder: FormBuilder,
private store: Store<fromAttackChange.AppState>
) {}
private unsubscribe$: Subject<void> = new Subject<void>();
quickMoves: string[] = ['Mud Shot', 'Dragon Tail'];
frmGroup: FormGroup = this.formBuilder.group({
attack: ''
});
ngOnInit() {
this.store.dispatch(new fromAttackChange.GetAttack());
this.attack$ = this.store.pipe(select(fromAttackChange.getAttack));
this.store
.pipe(
select(fromAttackChange.getAttackChangeState),
takeUntil(this.unsubscribe$)
)
.subscribe(state => this.frmGroup.setValue(state));
}
ngOnDestroy(): void {
this.unsubscribe$.next();
this.unsubscribe$.complete();
}
onConfirm(): void {
const model = this.frmGroup.value;
this.store.dispatch(new fromAttackChange.SetAttack(model.attack));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment