73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import {
|
|
ChangeDetectionStrategy,
|
|
Component,
|
|
OnInit,
|
|
computed,
|
|
inject,
|
|
signal,
|
|
} from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import { ActivatedRoute, RouterModule } from '@angular/router';
|
|
import { map, switchMap } from 'rxjs/operators';
|
|
import { RoomLayout } from '../../config/rooms-layout.config';
|
|
import { getCO2Color, getCO2Level } from '../../config/co2-levels.config';
|
|
import { SensorReading } from '../../models/sensor-reading.model';
|
|
import { RoomService } from '../../services/room.service';
|
|
import { SensorService } from '../../services/sensor.service';
|
|
|
|
const PAGE_SIZE = 8;
|
|
|
|
@Component({
|
|
selector: 'app-room-details-panel',
|
|
standalone: true,
|
|
imports: [CommonModule, RouterModule],
|
|
templateUrl: './room-details-panel.component.html',
|
|
styleUrl: './room-details-panel.component.scss',
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
})
|
|
export class RoomDetailsPanelComponent implements OnInit {
|
|
private route = inject(ActivatedRoute);
|
|
private roomService = inject(RoomService);
|
|
private sensorService = inject(SensorService);
|
|
|
|
room = signal<RoomLayout | undefined>(undefined);
|
|
latestReading = signal<SensorReading | undefined>(undefined);
|
|
history = signal<SensorReading[]>([]);
|
|
|
|
currentPage = signal(0);
|
|
|
|
totalPages = computed(() => Math.ceil(this.history().length / PAGE_SIZE));
|
|
pagedHistory = computed(() => {
|
|
const start = this.currentPage() * PAGE_SIZE;
|
|
return this.history().slice(start, start + PAGE_SIZE);
|
|
});
|
|
|
|
getCO2Color = getCO2Color;
|
|
getCO2Level = getCO2Level;
|
|
|
|
ngOnInit(): void {
|
|
const id$ = this.route.paramMap.pipe(map(params => params.get('id') ?? ''));
|
|
|
|
id$
|
|
.pipe(switchMap(id => this.roomService.getRoomById(id)))
|
|
.subscribe(room => this.room.set(room));
|
|
|
|
id$
|
|
.pipe(switchMap(id => this.sensorService.getLatestReadingForRoom(id)))
|
|
.subscribe(reading => this.latestReading.set(reading));
|
|
|
|
id$.pipe(switchMap(id => this.sensorService.getHistoryForRoom(id))).subscribe(history => {
|
|
this.history.set(history);
|
|
this.currentPage.set(0);
|
|
});
|
|
}
|
|
|
|
prevPage(): void {
|
|
if (this.currentPage() > 0) this.currentPage.update(p => p - 1);
|
|
}
|
|
|
|
nextPage(): void {
|
|
if (this.currentPage() < this.totalPages() - 1) this.currentPage.update(p => p + 1);
|
|
}
|
|
}
|