diff --git a/src/main/webapp/src/app/component/abstract-list.component.ts b/src/main/webapp/src/app/component/abstract-list.component.ts new file mode 100644 index 0000000..18739b1 --- /dev/null +++ b/src/main/webapp/src/app/component/abstract-list.component.ts @@ -0,0 +1,37 @@ +import {AfterViewInit, Component, OnInit} from '@angular/core'; +import {Subject, tap} from 'rxjs'; +import {BasicDataSource} from '../dataSource/basic-data-source'; + +@Component({template: ''}) +export abstract class AbstractListComponent implements AfterViewInit, OnInit { + + refreshSubject = new Subject(); + + abstract dataSource: BasicDataSource; + + abstract displayedColumns: Map; + + getElementNumber(t: T): number { + return (this.dataSource.page?.indexOf(t) ?? 0)+1; + } + + getDisplayedColumns(): string[] { + return [...this.displayedColumns.entries()] + .filter(column => column[1]) + .map(column => column[0]); + } + + getPage() { + this.dataSource.loadElements(); + } + + ngOnInit() { + this.getPage(); + } + + ngAfterViewInit(): void { + this.refreshSubject.pipe( + tap(() => this.getPage()) + ).subscribe(); + } +} diff --git a/src/main/webapp/src/app/component/person-list/person-list.component.html b/src/main/webapp/src/app/component/person-list/person-list.component.html new file mode 100644 index 0000000..2b86a88 --- /dev/null +++ b/src/main/webapp/src/app/component/person-list/person-list.component.html @@ -0,0 +1,59 @@ +
+
+ +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
L.p. {{getElementNumber(element)}} Id {{element.id}} Imię {{element.name}} Nazwisko {{element.surname}} Indeks {{element?.student?.index}} Edytuj + + Usuń + +
+
+
diff --git a/src/main/webapp/src/app/component/person-list/person-list.component.sass b/src/main/webapp/src/app/component/person-list/person-list.component.sass new file mode 100644 index 0000000..e69de29 diff --git a/src/main/webapp/src/app/component/person-list/person-list.component.spec.ts b/src/main/webapp/src/app/component/person-list/person-list.component.spec.ts new file mode 100644 index 0000000..6584cc9 --- /dev/null +++ b/src/main/webapp/src/app/component/person-list/person-list.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { PersonListComponent } from './person-list.component'; + +describe('PersonListComponent', () => { + let component: PersonListComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ PersonListComponent ] + }) + .compileComponents(); + + fixture = TestBed.createComponent(PersonListComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/main/webapp/src/app/component/person-list/person-list.component.ts b/src/main/webapp/src/app/component/person-list/person-list.component.ts new file mode 100644 index 0000000..73a2471 --- /dev/null +++ b/src/main/webapp/src/app/component/person-list/person-list.component.ts @@ -0,0 +1,49 @@ +import { Component } from '@angular/core'; +import {AbstractListComponent} from "../abstract-list.component"; +import {Person} from "../../model/person"; +import {BasicDataSource} from "../../dataSource/basic-data-source"; +import {PersonService} from "../../service/person.service"; +import {MatDialog} from "@angular/material/dialog"; +import {PersonFromComponent} from "../form/person-from/person-from.component"; +import {switchMap, tap} from "rxjs"; + +@Component({ + selector: 'app-person-list', + templateUrl: './person-list.component.html', + styleUrls: ['./person-list.component.sass'] +}) +export class PersonListComponent extends AbstractListComponent{ + dataSource: BasicDataSource; + displayedColumns: Map = new Map([ + ['lp', true], + ['id', true], + ['name', true], + ['surname', true], + ['index', true], + ['edit', true], + ['delete', true], + ]); + + constructor( + private personService: PersonService, + private dialog: MatDialog + ) { + super(); + this.dataSource = new BasicDataSource(personService); + } + + openFormDialog(person: Person | null = null) { + const dialogRef = this.dialog.open(PersonFromComponent, { + data: { person: person } + }); + dialogRef.afterClosed().pipe( + tap(() => this.getPage()) + ).subscribe() + } + + deletePerson(id: number) { + this.personService.delete(id).pipe( + tap(() => this.getPage()) + ).subscribe() + } +} diff --git a/src/main/webapp/src/app/dataSource/abstract-data-source.spec.ts b/src/main/webapp/src/app/dataSource/abstract-data-source.spec.ts new file mode 100644 index 0000000..952ad43 --- /dev/null +++ b/src/main/webapp/src/app/dataSource/abstract-data-source.spec.ts @@ -0,0 +1,7 @@ +import { BasicDataSource } from './basic-data-source'; + +describe('AbstractDataSource', () => { + it('should create an instance', () => { + expect(new BasicDataSource()).toBeTruthy(); + }); +}); diff --git a/src/main/webapp/src/app/dataSource/basic-data-source.ts b/src/main/webapp/src/app/dataSource/basic-data-source.ts new file mode 100644 index 0000000..ec4ad43 --- /dev/null +++ b/src/main/webapp/src/app/dataSource/basic-data-source.ts @@ -0,0 +1,46 @@ +import {CollectionViewer, DataSource} from '@angular/cdk/collections'; +import {BehaviorSubject, finalize, Observable} from 'rxjs'; +import {BasicService} from '../service/basic-service'; + +export class BasicDataSource implements DataSource { + private elementsSubject = new BehaviorSubject([]); + private loadingSubject = new BehaviorSubject(false); + + public loading$ = this.loadingSubject.asObservable(); + + public page: T[] | null = null; + + constructor(private service: BasicService) { + } + + connect(collectionViewer: CollectionViewer): Observable { + return this.elementsSubject.asObservable(); + } + + disconnect(collectionViewer: CollectionViewer): void { + this.elementsSubject.complete(); + this.loadingSubject.complete(); + } + + updateElement(newElement: T, comparator: (element: T, newElement: T) => boolean) { + const newList = this.elementsSubject.getValue().map(oldElement => { + if (comparator(oldElement, newElement)) { + return newElement; + } + return oldElement; + }); + this.elementsSubject.next(newList); + } + + filterElements(comparator: (element: T, index: number, array: T[]) => boolean) { + const newList = this.elementsSubject.getValue().filter(comparator); + this.elementsSubject.next(newList); + } + + loadElements() { + this.loadingSubject.next(true); + this.service.findAll().pipe( + finalize(() => this.loadingSubject.next(false)) + ).subscribe(elements => this.elementsSubject.next(elements)); + } +}