|
|
@@ -2,8 +2,9 @@ import { Component, Input } from '@angular/core';
|
|
|
import { EventsApiService } from 'src/app/events-api.service';
|
|
|
import { GroupEvent } from '../group-event';
|
|
|
import { RegistrationState, RegistrationStateItems } from '../registration-state';
|
|
|
-import { combineLatest, take } from 'rxjs';
|
|
|
+import { Observable, Subject, combineLatest, map, take } from 'rxjs';
|
|
|
import { PermissionService } from 'src/app/users/permission.service';
|
|
|
+import { Registration } from '../registration';
|
|
|
|
|
|
interface RegistrationModel {
|
|
|
readonly name: string;
|
|
|
@@ -22,16 +23,28 @@ export class EventViewComponent {
|
|
|
public eventId: string = '';
|
|
|
|
|
|
public event: GroupEvent | null = null;
|
|
|
- public registrations: Array<RegistrationModel> = [];
|
|
|
+ private readonly registrationsSubject = new Subject<Array<Registration>>();
|
|
|
+ public readonly registrations: Observable<Array<RegistrationModel>>;
|
|
|
|
|
|
public registrationStates = RegistrationStateItems;
|
|
|
|
|
|
- public currentState = RegistrationState.Unknown;
|
|
|
+ public currentRegistration: Registration = new Registration();
|
|
|
|
|
|
public constructor(
|
|
|
private readonly permissionService: PermissionService,
|
|
|
private readonly eventsApi: EventsApiService
|
|
|
- ) { }
|
|
|
+ ) {
|
|
|
+ this.registrations = this.registrationsSubject.pipe(map(registrations => {
|
|
|
+ return registrations
|
|
|
+ .filter(r => r.status !== RegistrationState.Unknown)
|
|
|
+ .map(r => ({
|
|
|
+ name: r.owner?.displayName ?? '',
|
|
|
+ yes: r.status === RegistrationState.Yes,
|
|
|
+ no: r.status === RegistrationState.No,
|
|
|
+ maybe: r.status === RegistrationState.Maybe,
|
|
|
+ }));
|
|
|
+ }));
|
|
|
+ }
|
|
|
|
|
|
public ngOnInit(): void {
|
|
|
this.eventsApi.getEvent(this.eventId).then(event => {
|
|
|
@@ -40,24 +53,16 @@ export class EventViewComponent {
|
|
|
combineLatest([this.permissionService.userProfile, this.eventsApi.getRegistrations(this.eventId)])
|
|
|
.pipe(take(1))
|
|
|
.subscribe(([userProfile, registrations]) => {
|
|
|
- const currentRegistrationIndex = registrations.findIndex(r => r.owner?.userId === userProfile?.userId);
|
|
|
-
|
|
|
- this.currentState = (currentRegistrationIndex >= 0 ? registrations.splice(currentRegistrationIndex, 1)[0] : null)?.status ?? RegistrationState.Unknown;
|
|
|
- this.registrations = registrations
|
|
|
- .filter(r => r.status !== RegistrationState.Unknown)
|
|
|
- .map(r => ({
|
|
|
- name: r.owner?.displayName ?? '',
|
|
|
- yes: r.status === RegistrationState.Yes,
|
|
|
- no: r.status === RegistrationState.No,
|
|
|
- maybe: r.status === RegistrationState.Maybe,
|
|
|
- }));
|
|
|
+ this.currentRegistration = registrations.find(r => r.owner?.userId === userProfile?.userId) ?? this.currentRegistration;
|
|
|
+ this.registrationsSubject.next(registrations);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
public updateRegistration(evt: any): void {
|
|
|
if (this.event) {
|
|
|
- console.log(evt, this.currentState);
|
|
|
- this.eventsApi.register(this.event, this.currentState);
|
|
|
+ this.eventsApi.register(this.event, this.currentRegistration.status)
|
|
|
+ .then(() => this.eventsApi.getRegistrations(this.eventId))
|
|
|
+ .then(registrations => this.registrationsSubject.next(registrations));
|
|
|
}
|
|
|
}
|
|
|
}
|