Skip to content

Instantly share code, notes, and snippets.

@jonathanwoahn
Created June 4, 2019 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanwoahn/f4e3a9904a3ea8d0044c5216535ce7da to your computer and use it in GitHub Desktop.
Save jonathanwoahn/f4e3a9904a3ea8d0044c5216535ce7da to your computer and use it in GitHub Desktop.
import { Component, ViewChild, ElementRef } from '@angular/core';
import { v4 as uuid } from 'uuid';
import { Todo } from './app.module';
import { DynamicFacadeService, DynamicFacade } from 'dynamic-ngrx';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
@ViewChild('todoInput') todoInput: ElementRef;
todos$: Observable<Todo[]>;
private todoFacade: DynamicFacade<Todo>;
constructor(
private dynamicFacadeService: DynamicFacadeService,
private store: Store<any>,
) {
this.todoFacade = this.dynamicFacadeService.getFacade<Todo>('Todo');
this.todos$ = this.todoFacade.entities$;
}
addTodo(event: KeyboardEvent): void {
if (event.keyCode !== 13) { return; }
const todo: Todo = {
id: uuid(),
text: this.todoInput.nativeElement.value,
};
this.store.dispatch(this.todoFacade.actions.addOne(todo));
this.todoInput.nativeElement.value = '';
}
removeTodo(todo: Todo): void {
this.store.dispatch(this.todoFacade.actions.removeOne(todo.id));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment