import { Component, OnInit } from '@angular/core'; import { MainPageService } from '../../main-page.service'; import { FormGroup, FormControl, Validators } from '@angular/forms'; import { BasicDictionaryModel } from '../../../models/dictionaries/basic-dictionary.model'; declare var $: any; @Component({ selector: 'app-pipe-fastening-groups-grid', templateUrl: './pipe-fastening-groups-grid.component.html', styleUrls: ['./pipe-fastening-groups-grid.component.scss'] }) export class PipeFasteningGroupsGridComponent implements OnInit { isPipeFasteningGroupsHidden = true; pipeFasteningGroupsGridData: BasicDictionaryModel[] = []; editedPipeFasteningGroup = new BasicDictionaryModel(); rowNumber: number; public formGroup: FormGroup; headers: string[] = ['Name', 'Code', '']; constructor(private mainPageService: MainPageService) {} public ngOnInit(): void { this.loadGridDataFromDb(); this.mainPageService.deletePipeFasteningGroupSubject.subscribe( (value) => { this.pipeFasteningGroupsGridData = this.pipeFasteningGroupsGridData.filter(x => x._id !== value); }); this.createForm(); } loadGridDataFromDb() { this.mainPageService.loadAllPipeFasteningGroups().subscribe((response) => { this.pipeFasteningGroupsGridData = 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.isPipeFasteningGroupsHidden = !this.isPipeFasteningGroupsHidden; } saveNewPipeFasteningGroup() { this.mainPageService.addNewPipeFasteningGroupIntoDb(JSON.stringify(this.editedPipeFasteningGroup)).subscribe((response) => { this.editedPipeFasteningGroup._id = response._id; this.editedPipeFasteningGroup.id = response.id; this.pipeFasteningGroupsGridData.push(this.editedPipeFasteningGroup); this.editedPipeFasteningGroup = new BasicDictionaryModel(); }); } setChoosenElement(element, row) { this.editedPipeFasteningGroup = { ...element, }; this.rowNumber = row; this.createForm(); } editPipeFasteningGroup() { console.log(this.pipeFasteningGroupsGridData[this.rowNumber]); Object.assign(this.pipeFasteningGroupsGridData[this.rowNumber], this.editedPipeFasteningGroup); this.mainPageService.updatePipeFasteningGroupInDb(JSON.stringify(this.editedPipeFasteningGroup)).subscribe(() => { }); } deletePipeFasteningGroup(element) { this.mainPageService.removePipeFasteningGroup(element._id).subscribe(() => { }, () => { }); this.mainPageService.deletePipeFasteningGroupSubject.next(element._id); } public prepareCreate() { this.editedPipeFasteningGroup = new BasicDictionaryModel(); this.createForm(); } private createForm() { this.formGroup = new FormGroup({ name: new FormControl(this.editedPipeFasteningGroup.name, Validators.required), code: new FormControl(this.editedPipeFasteningGroup.code), }); } }