import { Component, OnInit, Input } from '@angular/core'; import { MainPageService } from '../../main-page.service'; import { FormGroup, FormControl, Validators } from '@angular/forms'; import { PipeGroupModel } from '../../../models/dictionaries/pipe-group.model'; declare var $: any; @Component({ selector: 'app-pipe-groups-grid', templateUrl: './pipe-groups-grid.component.html', styleUrls: ['./pipe-groups-grid.component.scss'] }) export class PipeGroupsGridComponent implements OnInit { isPipeGroupsHidden = true; pipeGroupsGridData: PipeGroupModel[]; editedPipeGroup = new PipeGroupModel(); rowNumber: number; public formGroup: FormGroup; headers: string[] = ['Name', 'Code', '']; constructor(private mainPageService: MainPageService) {} public ngOnInit(): void { this.loadGridDataFromDb(); this.mainPageService.deletePipeGroupSubject.subscribe( (value) => { this.pipeGroupsGridData = this.pipeGroupsGridData.filter(x => x._id !== value); }); this.createForm(); } loadGridDataFromDb() { this.mainPageService.loadAllPipeGroups().subscribe((response) => { this.pipeGroupsGridData = response.sort(function (a, b) { return (a.name < b.name) ? -1 : 1; }); }); } toggle() { const actionButtons = $('.mat-icon-button'); $(actionButtons[1]).css('visibility', 'hidden'); for (let i = 2; i < actionButtons.length; i += 2) { $(actionButtons[i]).css('visibility', 'hidden'); } this.isPipeGroupsHidden = !this.isPipeGroupsHidden; } saveNewPipeGroup() { this.mainPageService.addNewPipeGroupIntoDb(JSON.stringify(this.editedPipeGroup)).subscribe((response) => { this.editedPipeGroup._id = response._id; this.editedPipeGroup.id = response.id; this.pipeGroupsGridData.push(this.editedPipeGroup); this.editedPipeGroup = new PipeGroupModel(); }); } setChoosenElement(element, row) { this.editedPipeGroup = { ...element, }; this.rowNumber = row; this.createForm(); } editPipeGroup() { console.log(this.pipeGroupsGridData[this.rowNumber]); Object.assign(this.pipeGroupsGridData[this.rowNumber], this.editedPipeGroup); this.mainPageService.updatePipeGroupInDb(JSON.stringify(this.editedPipeGroup)).subscribe((response) => {}); } deletePipeGroup(element) { this.mainPageService.removePipeGroup(element._id).subscribe((res) => {}, (error: any) => {}); this.mainPageService.deletePipeGroupSubject.next(element._id); } public prepareCreate() { this.editedPipeGroup = new PipeGroupModel(); this.createForm(); } private createForm() { this.formGroup = new FormGroup({ name: new FormControl(this.editedPipeGroup.name, Validators.required), code: new FormControl(this.editedPipeGroup.code), description: new FormControl(this.editedPipeGroup.description) }); } }