78 lines
2.7 KiB
TypeScript
78 lines
2.7 KiB
TypeScript
import {Component, OnDestroy, OnInit} from '@angular/core';
|
|
import {AngularFireDatabase, SnapshotAction} from '@angular/fire/compat/database';
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
import {Observable, Subject, Subscription} from 'rxjs';
|
|
import {first} from 'rxjs/operators';
|
|
|
|
import {Body} from '../administration/bodies/body';
|
|
import {Session} from './session';
|
|
import {SessionService} from '../shared/session.service';
|
|
|
|
@Component({
|
|
selector: 'app-edit',
|
|
templateUrl: './edit.component.html',
|
|
styleUrls: ['./edit.component.scss']
|
|
})
|
|
export class EditComponent implements OnInit, OnDestroy {
|
|
public bodies: Observable<SnapshotAction<Body>[]> = new Observable<SnapshotAction<Body>[]>();
|
|
public sessions: Subject<SnapshotAction<Session>[]> = new Subject<SnapshotAction<Session>[]>();
|
|
public currentPage: string | undefined;
|
|
|
|
private fullSessions: Observable<SnapshotAction<Session>[]> = new Observable<SnapshotAction<Session>[]>();
|
|
private sessionsMap: Map<string, SnapshotAction<Session>[]> = new Map<string, SnapshotAction<Session>[]>();
|
|
private subscription: Subscription | undefined;
|
|
|
|
constructor(private database: AngularFireDatabase,
|
|
private router: Router,
|
|
private activeRoute: ActivatedRoute,
|
|
public sessionService: SessionService) {
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
const bodyRef = this.database.list<Body>('bodies');
|
|
this.bodies = bodyRef.snapshotChanges();
|
|
const sessionRef = this.database.list<Session>('sessions');
|
|
this.fullSessions = sessionRef.snapshotChanges();
|
|
this.subscription = this.fullSessions.subscribe(sessions => {
|
|
this.sessionsMap = new Map<string, SnapshotAction<Session>[]>();
|
|
for (const session of sessions) {
|
|
const payload = session.payload.val();
|
|
if (payload == null) {
|
|
continue;
|
|
}
|
|
let sessionsPerBody = this.sessionsMap.get(payload.body);
|
|
if (sessionsPerBody == null) {
|
|
sessionsPerBody = [];
|
|
this.sessionsMap.set(payload.body, sessionsPerBody);
|
|
}
|
|
|
|
sessionsPerBody.push(session);
|
|
}
|
|
this.changeBody(this.currentPage);
|
|
});
|
|
this.bodies.pipe(first()).subscribe(bodies => {
|
|
if (bodies.length > 0) {
|
|
this.changeBody(bodies[0].payload.val()?.name);
|
|
}
|
|
});
|
|
}
|
|
|
|
public ngOnDestroy(): void {
|
|
this.subscription?.unsubscribe();
|
|
}
|
|
|
|
public changeBody(newBody: string | undefined): void {
|
|
if (newBody == null) {
|
|
return;
|
|
}
|
|
this.currentPage = newBody;
|
|
this.sessions.next(this.sessionsMap.get(newBody));
|
|
this.router.navigate(['edit']);
|
|
}
|
|
|
|
public editSession(key: string | null): void {
|
|
this.router.navigate([key], {relativeTo: this.activeRoute});
|
|
}
|
|
|
|
}
|