import { Component, OnInit, ViewChild, Output, EventEmitter } from '@angular/core'; import { SBGArmyList, UserArmyListFilter } from 'tournee.businesslogic'; import { ListBuilderFormComponent } from '../../forms/list-builder-form/list-builder-form.component'; import { ScrollToService } from '@nicky-lenaers/ngx-scroll-to'; import { UtilityService } from 'src/app/shared/services/utility.service'; import { AuthService } from 'src/app/shared/services/auth.service'; import { UserListsService } from 'src/app/shared/services/user.lists.service'; @Component({ selector: 'app-user-selectable-listing-army-lists', templateUrl: './user-selectable-listing-army-lists.component.html', styleUrls: ['./user-selectable-listing-army-lists.component.css'], providers: [ UserListsService ] }) export class UserSelectableListingArmyListsComponent implements OnInit { @ViewChild('Form') Form:ListBuilderFormComponent; @Output() OnShown = new EventEmitter(); @Output() OnHidden = new EventEmitter(); Filter: UserArmyListFilter = UserArmyListFilter.create(); Selected: SBGArmyList; ArmyLists: any = []; constructor(private _scrollToService: ScrollToService, private _auth: AuthService, private _lists: UserListsService) { } ngOnInit() { this._lists.get(this.Filter) .then((result) => { this.ArmyLists = result.result.map((al) => { return new SBGArmyList(al); }); }) .catch((err) => { console.log(err); }); } add() { this.edit(SBGArmyList.create(this._auth.User), true); } store(list: Array) { this.ArmyLists = list; this.hide(); } hide() { this.Selected = null; this.Form.reset(); this.OnHidden.emit(); } edit(list: SBGArmyList, isNew = false) { this.Selected = new SBGArmyList(JSON.parse(JSON.stringify(list))); this.Form.initiate(this.Selected, isNew); this.OnShown.emit(); this._scrollToService.scrollTo(UtilityService.scroll_to_head); } delete(list: SBGArmyList) { this._lists.delete(list.id) .then((result) => { this.ArmyLists = result.result.map((al) => { return new SBGArmyList(al); }); }) .catch((err) => { console.log(err); }); } }